-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update project structure and dependencies
This update restructures the project significantly and renames 'app_name_here' to 'lemmy-lib'. The Docker and test workflows have been removed, with key methods and functions added to lemmy-lib. Also, test cases and other extra files have been deleted. Ultimately, the dependency files have been updated.
- Loading branch information
Showing
11 changed files
with
650 additions
and
735 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
# Python App Template | ||
# LW Lemmy lib for 0.19 | ||
|
||
# Description | ||
This is a library for Lemmy 0.19. | ||
It is only for Lemmy.world but can be used for other Lemmy instances. | ||
Because of it, it is not a complete library for Lemmy! | ||
|
||
# Build | ||
```bash | ||
sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b /usr/local/bin | ||
task run | ||
``` | ||
|
||
|
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import logging | ||
import os | ||
from enum import Enum | ||
|
||
import requests | ||
|
||
if os.getenv("LOG_LEVEL") is None: | ||
logging.basicConfig(level=logging.DEBUG) | ||
else: | ||
logging.basicConfig(level=int(os.getenv("LOG_LEVEL"))) | ||
|
||
|
||
class LemmyApiMethod(Enum): | ||
GET = 'GET' | ||
POST = 'POST' | ||
PUT = 'PUT' | ||
DELETE = 'DELETE' | ||
|
||
|
||
API_VERSION = "v3" | ||
|
||
|
||
class LemmyLib: | ||
_jwt: str | None = None | ||
_url: str | None = None | ||
_logger: logging.Logger = None | ||
|
||
def __init__(self, url: str = None, jwt: str = None): | ||
self._logger = logging.getLogger(__name__) | ||
self._logger.debug("LemmyLib init") | ||
if url is not None: | ||
if url.endswith("/"): | ||
url = url[:-1] | ||
self.set_url(url) | ||
if jwt is not None: | ||
self.set_jwt(jwt) | ||
|
||
def call_api(self, method: LemmyApiMethod, endpoint: str, params: dict = None, headers: dict = None, | ||
data: dict = None): | ||
self._logger.debug("LemmyLib call_api") | ||
if headers is None: | ||
headers = self.get_headers() | ||
if params is None: | ||
params = {} | ||
if data is None: | ||
data = {} | ||
if self._url is None: | ||
raise Exception("LemmyLib: URL not set") | ||
|
||
url = f'{self._url}{self.get_base_path()}{endpoint}' | ||
|
||
self._logger.debug(f"LemmyLib call_api: {method} {url} {params} {headers} {data}") | ||
|
||
if method == LemmyApiMethod.GET: | ||
response = requests.get(url, params=params, headers=headers) | ||
elif method == LemmyApiMethod.POST: | ||
response = requests.post(url, params=params, headers=headers, json=data) | ||
elif method == LemmyApiMethod.PUT: | ||
response = requests.put(url, params=params, headers=headers, json=data) | ||
elif method == LemmyApiMethod.DELETE: | ||
response = requests.delete(url, params=params, headers=headers) | ||
else: | ||
raise Exception("LemmyLib: Unknown method") | ||
|
||
self._logger.debug(f"LemmyLib call_api: {response.status_code} {response.text}") | ||
|
||
if response.status_code == 200: | ||
return response | ||
else: | ||
raise Exception( | ||
f"LemmyLib: API call failed with status code {response.status_code} and message {response.text}") | ||
|
||
def get_base_path(self): | ||
return f'/api/{API_VERSION}/' | ||
|
||
def set_jwt(self, jwt: str): | ||
self._jwt = jwt | ||
self._logger.debug("LemmyLib set_jwt") | ||
|
||
def set_url(self, url: str): | ||
self._url = url | ||
self._logger.debug("LemmyLib set_url") | ||
|
||
def get_headers(self): | ||
header = { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
'User-Agent': 'lw-lemmy-lib', | ||
} | ||
if self._jwt: | ||
header['Authorization'] = f'Bearer {self._jwt}' | ||
|
||
return header | ||
|
||
def get_url(self): | ||
return self._url | ||
|
||
def get_jwt(self): | ||
return self._jwt | ||
|
||
def get_logger(self): | ||
return self._logger | ||
|
||
def login(self, username: str, password: str, totp: str | None = None, only_jwt: bool = False): | ||
self._logger.debug("LemmyLib login") | ||
response = self.call_api(LemmyApiMethod.POST, 'login', | ||
data={'username_or_email': username, 'password': password, 'totp_2fa_token': totp}) | ||
|
||
if not only_jwt: | ||
self.set_jwt(response.json()["jwt"]) | ||
|
||
return response | ||
|
||
def list_registration_applications(self, page: int = 1, unread_only: bool = False): | ||
self._logger.debug("LemmyLib list_registration_applications") | ||
|
||
return self.call_api(LemmyApiMethod.GET, 'applications', params={'page': page, 'unread_only': unread_only}) | ||
|
||
def approve_registration_application(self, application_id: int, approve: bool = True, | ||
deny_reason: str | None = None): | ||
self._logger.debug("LemmyLib approve_registration_application") | ||
|
||
return self.call_api(LemmyApiMethod.PUT, f'admin/registration_application/approve', | ||
data={'approve': approve, 'id': application_id, 'deny_reason': deny_reason}) | ||
|
||
def purge_person(self, person_id: int, reason: str | None = None): | ||
self._logger.debug("LemmyLib purge_person") | ||
|
||
return self.call_api(LemmyApiMethod.DELETE, f'admin/person/person', | ||
data={'reason': reason, 'person_id': person_id}) | ||
|
||
def purge_community(self, community_id: int, reason: str | None = None): | ||
self._logger.debug("LemmyLib purge_community") | ||
|
||
return self.call_api(LemmyApiMethod.DELETE, f'admin/community/community', | ||
data={'reason': reason, 'community_id': community_id}) | ||
|
||
def purge_post(self, post_id: int, reason: str | None = None): | ||
self._logger.debug("LemmyLib purge_post") | ||
|
||
return self.call_api(LemmyApiMethod.DELETE, f'admin/post/post', | ||
data={'reason': reason, 'post_id': post_id}) | ||
|
||
def purge_comment(self, comment_id: int, reason: str | None = None): | ||
self._logger.debug("LemmyLib purge_comment") | ||
|
||
return self.call_api(LemmyApiMethod.DELETE, f'admin/comment/comment', | ||
data={'reason': reason, 'comment_id': comment_id}) | ||
|
||
def get_post(self, post_id: int): | ||
self._logger.debug("LemmyLib get_post") | ||
|
||
return self.call_api(LemmyApiMethod.GET, f'post', params={'id': post_id}) | ||
|
||
def get_comment(self, comment_id: int): | ||
self._logger.debug("LemmyLib get_comment") | ||
|
||
return self.call_api(LemmyApiMethod.GET, f'comment', params={'id': comment_id}) | ||
|
||
def remove_post(self, post_id: int, removed: bool = True, reason: str | None = None): | ||
self._logger.debug("LemmyLib remove_post") | ||
|
||
return self.call_api(LemmyApiMethod.POST, f'post/remove', | ||
data={'reason': reason, 'post_id': post_id, 'removed': removed}) | ||
|
||
def remove_comment(self, comment_id: int, removed: bool = True, reason: str | None = None): | ||
self._logger.debug("LemmyLib remove_comment") | ||
|
||
return self.call_api(LemmyApiMethod.POST, f'comment/remove', | ||
data={'reason': reason, 'comment_id': comment_id, 'removed': removed}) | ||
|
||
def remove_person(self, person_id: int, removed: bool = True, reason: str | None = None): | ||
self._logger.debug("LemmyLib remove_person") | ||
|
||
return self.call_api(LemmyApiMethod.POST, f'person/remove', | ||
data={'reason': reason, 'person_id': person_id, 'removed': removed}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import re | ||
|
||
# Regex for the url https://example.com/u/username | ||
regex = r"https:\/\/(.*)\/(u|c)\/(.*)" | ||
|
||
def extract_username_from_actor_id(url): | ||
"""Extracts the username from a url.""" | ||
|
||
# Find matches | ||
matches = re.match(regex, url, re.MULTILINE) | ||
|
||
# Get the username | ||
username = matches.group(3) | ||
|
||
return username | ||
|
||
def extract_domain_from_actor_id(url): | ||
"""Extracts the domain from a url.""" | ||
|
||
# Find matches | ||
matches = re.match(regex, url, re.MULTILINE) | ||
|
||
# Get the domain | ||
domain = matches.group(1) | ||
|
||
return domain |
Empty file.
Oops, something went wrong.