Skip to content

Commit

Permalink
Merge pull request #15 from amarpersaud/dev_0.1.6
Browse files Browse the repository at this point in the history
v0.1.6 - Fix booleans, update API
  • Loading branch information
amarpersaud authored Jul 8, 2023
2 parents e7f1f37 + a8e5f39 commit ba391a5
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 334 deletions.
2 changes: 1 addition & 1 deletion jplaw/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
The jplaw library is designed to allow interactions with the API of Lemmy instances. For function access, see the submodules. For More information on the API, see the Lemmy API documentation.
"""
__version__ = "0.1.5"
__version__ = "0.1.6"
"""Current package version"""

__package__ = "jplaw"
Expand Down
96 changes: 40 additions & 56 deletions jplaw/comment.py

Large diffs are not rendered by default.

80 changes: 37 additions & 43 deletions jplaw/community.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,74 @@ class Community():
def __init__(self, _req: Requestor):
self._req = _req

def get(self, name:str, instance:str=None, auth:bool=True, auth_token:str=None):
def get(self, name:str, instance:str=None, auth:bool=True):
"""
Get community information
Args:
name (str): Name of the community. Federated communities can be accessed by [community]@[instance]
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth (str): If true, authenticates using auth_token if given or internal token from login. Optional. Default True
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
auth (str): If true, authenticates using internal token from login. Optional. Default True
Returns:
Community Information. Throws an error if community_not_found (happens if community has not been federated or does not exist)
"""
form = {
"name": name
}
res = self._req.lemmyRequest("getCommunity", instance=instance, form=form, auth=auth, auth_token=auth_token)
res = self._req.lemmyRequest("getCommunity", instance=instance, form=form, auth=auth)
return res["community_view"]

def list(self, type: ListingType=None, sort:SortType=None, page:int=None, limit:int=None, instance:str=None, auth:bool=True, auth_token:str=None):
def list(self,
type: ListingType=None,
sort:SortType=None,
show_nsfw: bool=None,
page:int=None,
limit:int=None,
instance:str=None,
auth:bool=True):
"""
Get a list of communities (federated or local)
Args:
type (ListingType): Type of community (all or local). Optional
sort (SortType): Sorting Mode. Optional
show_nsfw (bool): Whether or not to show nsfw communities. Optional.
page (int): Page number. Optional
limit (int): Limit for number of posts. Optional
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth (str): If true, authenticates using auth_token if given or internal token from login. Optional. Default True
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
auth (str): If true, authenticates using internal token from login. Optional. Default True
Returns:
List of communities
"""
form={}
if(type):
form["type"]=type.value
if(sort):
form["sort"]=sort.value
optional={
"page": page,
"limit": limit
"sort" : sort,
"type" : type,
"show_nsfw" : show_nsfw,
"page" : page,
"limit" : limit
}
res = self._req.lemmyRequest("listCommunities", instance=instance, form=form, optional=optional, auth=auth, auth_token=auth_token)
res = self._req.lemmyRequest("listCommunities", instance=instance, form=form, optional=optional, auth=auth)
return res["communities"]

def follow(self, community_id:int, follow:bool=True, instance:str=None, auth_token:str=None):
def follow(self, community_id:int, follow:bool=True, instance:str=None):
"""
Follow or subscribe to a community
Args:
community_id (int): ID of the community
follow (bool): If true, subscribed to or following the community. Optional. Defaults to True
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
List of communities
"""
form={
"community_id": community_id,
"follow": follow
}
res = self._req.lemmyRequest("followCommunity", instance=instance, form=form, auth=True, auth_token=auth_token)
res = self._req.lemmyRequest("followCommunity", instance=instance, form=form, auth=True)
return res["community_view"]
def addMod(self, community_id:int, person_id:int, added:bool=True, instance:str=None, auth_token:str=None):
def addMod(self, community_id:int, person_id:int, added:bool=True, instance:str=None):
"""
Add or remove a moderator from a community
Expand All @@ -83,7 +87,6 @@ def addMod(self, community_id:int, person_id:int, added:bool=True, instance:str=
person_id (int): User ID of the person to add as a moderator
added (bool): If true, person is added as a moderator. If false, person is removed as a moderator. Optonal. Default True
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Added moderator response
"""
Expand All @@ -92,10 +95,10 @@ def addMod(self, community_id:int, person_id:int, added:bool=True, instance:str=
"person_id": person_id,
"added": added,
}
res = self._req.lemmyRequest("addModToCommunity", instance=instance, form=form, auth=True, auth_token=auth_token)
res = self._req.lemmyRequest("addModToCommunity", instance=instance, form=form, auth=True)
return res

def banPerson(self, community_id:int, person_id:int, ban:bool, remove_data:bool=None, reason:str=None, expires:int=None, instance:str=None, auth_token:str=None):
def banPerson(self, community_id:int, person_id:int, ban:bool, remove_data:bool=None, reason:str=None, expires:int=None, instance:str=None):
"""
Ban a person from a community
Expand All @@ -107,7 +110,6 @@ def banPerson(self, community_id:int, person_id:int, ban:bool, remove_data:bool=
reason (str): Reason for banning user. Optional
expires (int): Unix timestamp of ban expiry (seconds)
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Banned community response
"""
Expand All @@ -121,26 +123,25 @@ def banPerson(self, community_id:int, person_id:int, ban:bool, remove_data:bool=
"reason": reason,
"expires": expires,
}
res = self._req.lemmyRequest("banFromCommunity", instance=instance, form=form, optional=optional, auth=True, auth_token=auth_token)
res = self._req.lemmyRequest("banFromCommunity", instance=instance, form=form, optional=optional, auth=True)
return res

def block(self, community_id:int, block:bool=True, instance:str=None, auth_token:str=None):
def block(self, community_id:int, block:bool=True, instance:str=None):
"""
Block a community
Args:
community_id (int): ID of the community
ban (bool): If true, community blocked. If false, community is unblocked. Optonal. Default True
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Blocked community response
"""
form={
"community_id": community_id,
"block": block,
}
res = self._req.lemmyRequest("blockCommunity", instance=instance, form=form, auth=True, auth_token=auth_token)
res = self._req.lemmyRequest("blockCommunity", instance=instance, form=form, auth=True)
return res["community_view"]

def create(self, name:str,
Expand All @@ -151,8 +152,7 @@ def create(self, name:str,
nsfw:bool=None,
posting_restricted_to_mods:bool=None,
discussion_languages:List[int]=None,
instance:str=None,
auth_token:str=None):
instance:str=None):
"""
Create a community
Expand All @@ -166,7 +166,6 @@ def create(self, name:str,
posting_restricted_to_mods (bool): If true, only moderators can post. Optional.
discussion_languages (List[int]): List of langages in community. Optional.
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Created community response
"""
Expand All @@ -182,26 +181,25 @@ def create(self, name:str,
"posting_restricted_to_mods": posting_restricted_to_mods,
"discussion_languages": discussion_languages,
}
res = self._req.lemmyRequest("createCommunity", instance=instance, form=form, optional=optional, auth=True, auth_token=auth_token)
res = self._req.lemmyRequest("createCommunity", instance=instance, form=form, optional=optional, auth=True)
return res["community_view"]

def delete(self, community_id:int, deleted:bool=True, instance:str=None, auth_token:str=None):
def delete(self, community_id:int, deleted:bool=True, instance:str=None):
"""
Delete a community
Args:
community_id (int): ID of the community
deleted (bool): If true, community is deleted. If false, community is not deleted. Optonal. Default True
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Deleted community response
"""
form={
"community_id": community_id,
"deleted": deleted,
}
res = self._req.lemmyRequest("deleteCommunity", instance=instance, form=form, auth=True, auth_token=auth_token)
res = self._req.lemmyRequest("deleteCommunity", instance=instance, form=form, auth=True)
return res["community_view"]

def edit(self,
Expand All @@ -213,8 +211,7 @@ def edit(self,
nsfw:bool=None,
posting_restricted_to_mods:bool=None,
discussion_languages:List[int]=None,
instance:str=None,
auth_token:str=None):
instance:str=None):
"""
Edit a community. Excluding optional items does not edit those items.
Expand All @@ -228,7 +225,6 @@ def edit(self,
posting_restricted_to_mods (bool): If true, only moderators can post. Optional.
discussion_languages (List[int]): List of langages in community. Optional.
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Edited community response
"""
Expand All @@ -244,10 +240,10 @@ def edit(self,
"posting_restricted_to_mods": posting_restricted_to_mods,
"discussion_languages": discussion_languages,
}
res = self._req.lemmyRequest("createCommunity", instance=instance, form=form, optional=optional, auth=True, auth_token=auth_token)
res = self._req.lemmyRequest("createCommunity", instance=instance, form=form, optional=optional, auth=True)
return res["community_view"]

def remove(self, community_id:int, removed:bool=True, reason:str=None, expires:int=None, instance:str=None, auth_token:str=None):
def remove(self, community_id:int, removed:bool=True, reason:str=None, expires:int=None, instance:str=None):
"""
Remove a community
Expand All @@ -257,7 +253,6 @@ def remove(self, community_id:int, removed:bool=True, reason:str=None, expires:i
reason (str): Reason for community removal. Optional.
expires (int): Unix timestamp (seconds) of expiry.
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Removed community response
"""
Expand All @@ -269,24 +264,23 @@ def remove(self, community_id:int, removed:bool=True, reason:str=None, expires:i
"reason": reason,
"expires": expires,
}
res = self._req.lemmyRequest("removeCommunity", instance=instance, form=form, optional=optional, auth=True, auth_token=auth_token)
res = self._req.lemmyRequest("removeCommunity", instance=instance, form=form, optional=optional, auth=True)
return res["community_view"]

def transfer(self, community_id:int, person_id:int, instance:str=None, auth_token:str=None):
def transfer(self, community_id:int, person_id:int, instance:str=None):
"""
Transfer ownership of a community
Args:
community_id (int): ID of the community
person_id (int): User ID of the person to transfer ownership to
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Removed community response
"""
form={
"community_id": community_id,
"person_id":person_id,
}
res = self._req.lemmyRequest("transferCommunity", instance=instance, form=form, auth=True, auth_token=auth_token)
res = self._req.lemmyRequest("transferCommunity", instance=instance, form=form, auth=True)
return res["community_view"]
15 changes: 6 additions & 9 deletions jplaw/emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Emoji():
def __init__(self, _req: Requestor):
self._req = _req

def create(self, category:str, shortcode:str, image_url:str, alt_text:str, keywords:List[str], instance:str=None, auth_token:str=None):
def create(self, category:str, shortcode:str, image_url:str, alt_text:str, keywords:List[str], instance:str=None):
"""
Create a custom emoji
Expand All @@ -23,7 +23,6 @@ def create(self, category:str, shortcode:str, image_url:str, alt_text:str, keywo
alt_text (str): Alt text (hover or screenreader) of the emoji
keywords (List[str]): Keywords of the emoji.
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Emoji created response
"""
Expand All @@ -34,10 +33,10 @@ def create(self, category:str, shortcode:str, image_url:str, alt_text:str, keywo
"alt_text": alt_text,
"keywords": keywords
}
res = self._req.lemmyRequest("createCustomEmoji", instance=instance, form=form, auth=auth, auth_token=auth_token)
res = self._req.lemmyRequest("createCustomEmoji", instance=instance, form=form, auth=auth)
return res

def edit(self, emoji_id:int, category:str=None, image_url:str=None, alt_text:str=None, keywords:List[str]=None, instance:str=None, auth_token:str=None):
def edit(self, emoji_id:int, category:str=None, image_url:str=None, alt_text:str=None, keywords:List[str]=None, instance:str=None):
"""
Edit a custom emoji. Excluded optional arguments are not modified.
Expand All @@ -48,7 +47,6 @@ def edit(self, emoji_id:int, category:str=None, image_url:str=None, alt_text:str
alt_text (str): Alt text (hover or screenreader) of the emoji. Optional
keywords (List[str]): Keywords of the emoji. Optional
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Emoji edited response
"""
Expand All @@ -59,22 +57,21 @@ def edit(self, emoji_id:int, category:str=None, image_url:str=None, alt_text:str
"alt_text": alt_text,
"keywords": keywords
}
res = self._req.lemmyRequest("editCustomEmoji", instance=instance, form=form, auth=auth, auth_token=auth_token)
res = self._req.lemmyRequest("editCustomEmoji", instance=instance, form=form, auth=auth)
return res

def delete(self, emoji_id:int, instance:str=None, auth_token:str=None):
def delete(self, emoji_id:int, instance:str=None):
"""
Delete a custom emoji from the website
Args:
emoji_id (int): ID of emoji to delete
instance (str): URL of local instance. Optional. Default None uses logged in instance
auth_token (str): Authentication token for local instance. Optional. Default None uses logged in auth_token
Returns:
Emoji deleted response
"""
form={
"id": emoji_id
}
res = self._req.lemmyRequest("deleteCustomEmoji", instance=instance, form=form, auth=auth, auth_token=auth_token)
res = self._req.lemmyRequest("deleteCustomEmoji", instance=instance, form=form, auth=auth)
return res
Loading

0 comments on commit ba391a5

Please sign in to comment.