Skip to content

Commit 5a094eb

Browse files
authored
Add GetModlog functionality (#109)
* Add GetModlog functionality * Add a simple example
1 parent 957d2b0 commit 5a094eb

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

examples/modlog.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
## python examples/emoji.py db0
3+
4+
import os
5+
import argparse
6+
import json
7+
from pythorhead import Lemmy
8+
9+
10+
arg_parser = argparse.ArgumentParser()
11+
arg_parser.add_argument('-c', '--lemmy_community', action="store", required=False, type=str, help="A community to rerieve the modlog for")
12+
arg_parser.add_argument('-d', '--lemmy_domain', action='store', required=False, type=str, help="the domain in which to look for this user")
13+
arg_parser.add_argument('-u', '--lemmy_username', action='store', required=False, type=str, help="Which user to authenticate as")
14+
arg_parser.add_argument('-p', '--lemmy_password', action='store', required=False, type=str, help="Which password to authenticate with")
15+
args = arg_parser.parse_args()
16+
17+
18+
19+
lemmy_domain = args.lemmy_domain
20+
if not lemmy_domain:
21+
lemmy_domain = os.getenv('LEMMY_DOMAIN', "lemmy.dbzer0.com")
22+
if not lemmy_domain:
23+
raise Exception("You need to provide a lemmy domain via env var or arg")
24+
25+
lemmy_username = args.lemmy_username
26+
if not lemmy_username:
27+
lemmy_username = os.getenv("LEMMY_USERNAME")
28+
29+
lemmy_password = args.lemmy_password
30+
if not lemmy_password:
31+
lemmy_password = os.getenv("LEMMY_PASSWORD")
32+
33+
lemmy = Lemmy(f"https://{lemmy_domain}", raise_exceptions=True)
34+
if lemmy_username and lemmy_password:
35+
login = lemmy.log_in(lemmy_username, lemmy_password)
36+
37+
lemmy_community = args.lemmy_community
38+
if lemmy_community:
39+
community_id = lemmy.discover_community(community_name=lemmy_community)
40+
if not community_id:
41+
raise Exception("Provided Lemmy community not found")
42+
else:
43+
community_id=None
44+
45+
modlog = lemmy.modlog.get(community_id=community_id)
46+
if modlog:
47+
print(json.dumps(modlog, indent=4))
48+
else:
49+
print("Retrieval of modlog failed")

pythorhead/lemmy.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pythorhead.user import User
1616
from pythorhead.admin import Admin
1717
from pythorhead.emoji import Emoji
18+
from pythorhead.modlog import Modlog
1819
from pythorhead.classes.user import LemmyUser
1920
from pythorhead import class_methods
2021

@@ -33,6 +34,7 @@ class Lemmy:
3334
mention: Mention
3435
admin: Admin
3536
emoji: Emoji
37+
modlog: Modlog
3638
# imported class methods
3739
get_user = class_methods.get_user
3840
get_registration_applications = class_methods.get_applications
@@ -50,6 +52,7 @@ def __init__(self, api_base_url: str, raise_exceptions = False, request_timeout=
5052
self.mention = Mention(self._requestor)
5153
self.admin = Admin(self._requestor)
5254
self.emoji = Emoji(self._requestor)
55+
self.modlog = Modlog(self._requestor)
5356

5457
@property
5558
def nodeinfo(self):

pythorhead/modlog.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import Any, List, Optional, Union
2+
3+
from pythorhead.requestor import Request, Requestor
4+
from pythorhead.types import ModlogActionType
5+
6+
7+
class Modlog:
8+
def __init__(self, _requestor: Requestor):
9+
self._requestor = _requestor
10+
11+
def get(
12+
self,
13+
comment_id: Optional[int] = None,
14+
community_id: Optional[int] = None,
15+
limit: Optional[int] = None,
16+
mod_person_id: Optional[int] = None,
17+
other_person_id: Optional[int] = None,
18+
page: Optional[int] = None,
19+
post_id: Optional[int] = None,
20+
type_: Optional[ModlogActionType] = None
21+
) -> Optional[dict]:
22+
"""
23+
24+
Get Modlog
25+
26+
Args:
27+
comment_id (int, optional): Defaults to None.
28+
community_id (int, optional): Defaults to None.
29+
limit (int, optional): Defaults to None.
30+
mod_person_id (int, optional): Defaults to None.
31+
other_person_id (int, optional): Defaults to None.
32+
page (int, optional): Defaults to None.
33+
post_id (int, optional): Defaults to None.
34+
type_ (ModlogActionType, optional): Defaults to None.
35+
Returns:
36+
Optional[dict]: post data if successful
37+
"""
38+
39+
params: dict[str, Any] = {
40+
key: value for key, value in locals().items() if value is not None and key != "self"
41+
}
42+
43+
return self._requestor.api(Request.GET, "/modlog", params=params)

pythorhead/types/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
from .sort import CommentSortType, SortType
44
from .language import LanguageType
55
from .search import SearchType, SearchOption
6+
from .modlog import ModlogActionType

pythorhead/types/modlog.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from enum import Enum
2+
3+
4+
class ModlogActionType(str, Enum):
5+
All = "All"
6+
ModRemovePost = "ModRemovePost"
7+
ModLockPost = "ModLockPost"
8+
ModFeaturePost = "ModFeaturePost"
9+
ModRemoveComment = "ModRemoveComment"
10+
ModRemoveCommunity = "ModRemoveCommunity"
11+
ModBanFromCommunity = "ModBanFromCommunity"
12+
ModAddCommunity = "ModAddCommunity"
13+
ModTransferCommunity = "ModTransferCommunity"
14+
ModAdd = "ModAdd"
15+
ModBan = "ModBan"
16+
ModHideCommunity = "ModHideCommunity"
17+
AdminPurgePerson = "AdminPurgePerson"
18+
AdminPurgeCommunity = "AdminPurgeCommunity"
19+
AdminPurgePost = "AdminPurgePost"
20+
AdminPurgeComment = "AdminPurgeComment"

0 commit comments

Comments
 (0)