Skip to content

Commit

Permalink
Add GetModlog functionality (#109)
Browse files Browse the repository at this point in the history
* Add GetModlog functionality

* Add a simple example
  • Loading branch information
chris-y authored Jan 23, 2025
1 parent 957d2b0 commit 5a094eb
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
49 changes: 49 additions & 0 deletions examples/modlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

## python examples/emoji.py db0

import os
import argparse
import json
from pythorhead import Lemmy


arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-c', '--lemmy_community', action="store", required=False, type=str, help="A community to rerieve the modlog for")
arg_parser.add_argument('-d', '--lemmy_domain', action='store', required=False, type=str, help="the domain in which to look for this user")
arg_parser.add_argument('-u', '--lemmy_username', action='store', required=False, type=str, help="Which user to authenticate as")
arg_parser.add_argument('-p', '--lemmy_password', action='store', required=False, type=str, help="Which password to authenticate with")
args = arg_parser.parse_args()



lemmy_domain = args.lemmy_domain
if not lemmy_domain:
lemmy_domain = os.getenv('LEMMY_DOMAIN', "lemmy.dbzer0.com")
if not lemmy_domain:
raise Exception("You need to provide a lemmy domain via env var or arg")

lemmy_username = args.lemmy_username
if not lemmy_username:
lemmy_username = os.getenv("LEMMY_USERNAME")

lemmy_password = args.lemmy_password
if not lemmy_password:
lemmy_password = os.getenv("LEMMY_PASSWORD")

lemmy = Lemmy(f"https://{lemmy_domain}", raise_exceptions=True)
if lemmy_username and lemmy_password:
login = lemmy.log_in(lemmy_username, lemmy_password)

lemmy_community = args.lemmy_community
if lemmy_community:
community_id = lemmy.discover_community(community_name=lemmy_community)
if not community_id:
raise Exception("Provided Lemmy community not found")
else:
community_id=None

modlog = lemmy.modlog.get(community_id=community_id)
if modlog:
print(json.dumps(modlog, indent=4))
else:
print("Retrieval of modlog failed")
3 changes: 3 additions & 0 deletions pythorhead/lemmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pythorhead.user import User
from pythorhead.admin import Admin
from pythorhead.emoji import Emoji
from pythorhead.modlog import Modlog
from pythorhead.classes.user import LemmyUser
from pythorhead import class_methods

Expand All @@ -33,6 +34,7 @@ class Lemmy:
mention: Mention
admin: Admin
emoji: Emoji
modlog: Modlog
# imported class methods
get_user = class_methods.get_user
get_registration_applications = class_methods.get_applications
Expand All @@ -50,6 +52,7 @@ def __init__(self, api_base_url: str, raise_exceptions = False, request_timeout=
self.mention = Mention(self._requestor)
self.admin = Admin(self._requestor)
self.emoji = Emoji(self._requestor)
self.modlog = Modlog(self._requestor)

@property
def nodeinfo(self):
Expand Down
43 changes: 43 additions & 0 deletions pythorhead/modlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Any, List, Optional, Union

from pythorhead.requestor import Request, Requestor
from pythorhead.types import ModlogActionType


class Modlog:
def __init__(self, _requestor: Requestor):
self._requestor = _requestor

def get(
self,
comment_id: Optional[int] = None,
community_id: Optional[int] = None,
limit: Optional[int] = None,
mod_person_id: Optional[int] = None,
other_person_id: Optional[int] = None,
page: Optional[int] = None,
post_id: Optional[int] = None,
type_: Optional[ModlogActionType] = None
) -> Optional[dict]:
"""
Get Modlog
Args:
comment_id (int, optional): Defaults to None.
community_id (int, optional): Defaults to None.
limit (int, optional): Defaults to None.
mod_person_id (int, optional): Defaults to None.
other_person_id (int, optional): Defaults to None.
page (int, optional): Defaults to None.
post_id (int, optional): Defaults to None.
type_ (ModlogActionType, optional): Defaults to None.
Returns:
Optional[dict]: post data if successful
"""

params: dict[str, Any] = {
key: value for key, value in locals().items() if value is not None and key != "self"
}

return self._requestor.api(Request.GET, "/modlog", params=params)
1 change: 1 addition & 0 deletions pythorhead/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from .sort import CommentSortType, SortType
from .language import LanguageType
from .search import SearchType, SearchOption
from .modlog import ModlogActionType
20 changes: 20 additions & 0 deletions pythorhead/types/modlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from enum import Enum


class ModlogActionType(str, Enum):
All = "All"
ModRemovePost = "ModRemovePost"
ModLockPost = "ModLockPost"
ModFeaturePost = "ModFeaturePost"
ModRemoveComment = "ModRemoveComment"
ModRemoveCommunity = "ModRemoveCommunity"
ModBanFromCommunity = "ModBanFromCommunity"
ModAddCommunity = "ModAddCommunity"
ModTransferCommunity = "ModTransferCommunity"
ModAdd = "ModAdd"
ModBan = "ModBan"
ModHideCommunity = "ModHideCommunity"
AdminPurgePerson = "AdminPurgePerson"
AdminPurgeCommunity = "AdminPurgeCommunity"
AdminPurgePost = "AdminPurgePost"
AdminPurgeComment = "AdminPurgeComment"

0 comments on commit 5a094eb

Please sign in to comment.