Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c038d17
Copy game_threads bot to lemmy_game_threads
Jun 13, 2023
e5ae8b7
Update community map specifically for fanaticus.social
Jun 13, 2023
33bda53
Hack away to replace reddit with lemmy
benja810 Jun 13, 2023
2455e12
Add plaw to requirements
benja810 Jun 14, 2023
57d7d4f
Get Game Thread's posting and updating
benja810 Jun 14, 2023
7af008d
Fix markdown formatting for lemmy
benja810 Jun 14, 2023
f739d8a
Remove superscript markdown
benja810 Jun 14, 2023
7a24dc6
Implement lemmy config options
benja810 Jun 15, 2023
eb0d2f0
Strip all new line chars from lemmy post titles
benja810 Jun 15, 2023
25cbccd
Create new lemmy game threads config
benja810 Jun 17, 2023
4368873
Add plaw as local lib
benja810 Jun 17, 2023
1618e1f
Add comment functionality
benja810 Jun 17, 2023
441c384
Re-implement off day threads
benja810 Jun 19, 2023
135beb1
Re-implement sticky/unsticky
benja810 Jun 19, 2023
b5283b9
Fix formatting in more templates
benja810 Jun 20, 2023
47f3c32
HOTFIX: truncate post body length over 10k
benja810 Jun 20, 2023
09f29e3
Add .idea to .gitignore for PyCharm IDE
jspayne Jun 20, 2023
3f1d9c3
Version 3.0 of tzlocal doesn't have the localize method.
jspayne Jun 20, 2023
db268bd
Change the team URLs so they work when viewing from other instances.
jspayne Jun 20, 2023
f46f26a
* Truncation was using a , instead of a : in the string slicing
jspayne Jun 21, 2023
b639153
* Rename Lemmy MLB bot
jspayne Jun 21, 2023
ccbbe35
Fix gamePks loop and warning when collecting data
benja810 Jun 21, 2023
0067daa
Update bots/lemmy_mlb_game_threads/__init__.py
benja810 Jun 22, 2023
2c89233
Update bots/lemmy_mlb_game_threads/__init__.py
benja810 Jun 22, 2023
5be8d68
Merge branch 'lemmy-game-threads' of github.com:jspayne/redball into …
benja810 Jun 22, 2023
89a4461
Update __init__.py to fix time check for post game thread
jspayne Jun 23, 2023
4973a2d
Merge pull request #6 from jspayne/patch-1
jspayne Jun 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ certs/
*.cer
*custom*.mako

.idea/
.vscode/

# Byte-compiled / optimized / DLL files
Expand Down
6,340 changes: 6,340 additions & 0 deletions bots/lemmy_mlb_game_threads/__init__.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bots/lemmy_mlb_game_threads/plaw/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .lemmy import Lemmy
145 changes: 145 additions & 0 deletions bots/lemmy_mlb_game_threads/plaw/lemmy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
from .requestor import Requestor, HttpType


class Lemmy:
def __enter__(self):
"""Handle the context manager open."""
return self

def __exit__(self, *_args):
"""Handle the context manager close."""

def __init__(self, instance, username, password):
self.instance = instance
self.username = username

# Login, get token, and set as header for future
self._req = Requestor({})
self.auth_token = self.login(username, password)
self._req.headers.update({"Authorization": "Bearer " + self.auth_token})
# print(self._req.headers.get("Authorization"))

def login(self, username, password):
url = self.instance + "/api/v3/user/login"
res_data = self._req.request(
HttpType.POST, url, {"username_or_email": username, "password": password}
)
return res_data["jwt"]

def getCommunity(self, name):
url = self.instance + "/api/v3/community"
res = self._req.request(HttpType.GET, url, {"name": name})

self.community = res["community_view"]["community"]
return self.community

def listPosts(self, sort=None):
url = self.instance + "/api/v3/post/list"
res = self._req.request(
HttpType.GET,
url,
{"sort": sort or "New", "community_id": self.community["id"]},
)

return res["posts"]

def getPost(self, id):
url = self.instance + "/api/v3/post"
res = self._req.request(
HttpType.GET,
url,
{"id": id},
)

return res["post_view"]

def submitPost(self, title=None, body=None, url=None):
api_url = self.instance + "/api/v3/post"
res = self._req.request(
HttpType.POST,
api_url,
{
"auth": self.auth_token,
"community_id": self.community["id"],
"name": title,
"body": body,
"url": url,
},
)

return res["post_view"]

def editPost(self, post_id, title=None, body=None, url=None):
api_url = self.instance + "/api/v3/post"
data = {
"auth": self.auth_token,
"post_id": post_id,
}
if title:
data["name"] = title
if body:
data["body"] = body
if url:
data["url"] = url

res = self._req.request(HttpType.PUT, api_url, data)

return res["post_view"]

def submitComment(self, post_id, content, language_id=None, parent_id=None):
api_url = self.instance + "/api/v3/comment"

data = {
"auth": self.auth_token,
"content": content,
"post_id": post_id,
}

if language_id:
data["language_id"] = language_id
if parent_id:
data["parent_id"] = parent_id

res = self._req.request(
HttpType.POST,
api_url,
data,
)

return res["comment_view"]

def stickyPost(self, post_id):
api_url = self.instance + "/api/v3/post/feature"

data = {
"auth": self.auth_token,
"post_id": post_id,
"feature_type": "Community",
"featured": True
}

res = self._req.request(
HttpType.POST,
api_url,
data,
)

return res["post_view"]

def unStickyPost(self, post_id):
api_url = self.instance + "/api/v3/post/feature"

data = {
"auth": self.auth_token,
"post_id": post_id,
"feature_type": "Community",
"featured": False
}

res = self._req.request(
HttpType.POST,
api_url,
data,
)

return res["post_view"]
32 changes: 32 additions & 0 deletions bots/lemmy_mlb_game_threads/plaw/requestor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from enum import Enum
import requests
import json
from typing import Dict, Any, TypeVar

T = TypeVar("T")

class HttpType(Enum):
GET = "GET"
POST = "POST"
PUT = "PUT"
DELETE = "DELETE"
# ... any other HTTP methods you need

class Requestor:
def __init__(self, headers: Dict[str, str]):
self.headers = headers

def request(self, type_: HttpType, url: str, form: Dict[str, Any]) -> T:
if type_ == HttpType.GET:
response = requests.get(url, params=form, headers=self.headers)
else:
headers = {
"Content-Type": "application/json",
**self.headers,
}
response = requests.request(type_.value, url, data=json.dumps(form), headers=headers)

if response.status_code != 200:
raise Exception(response.text) # Adjust this according to how your API returns errors

return response.json()
Loading