-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support for hesitations and filtering * hesitation example
- Loading branch information
Showing
6 changed files
with
266 additions
and
9 deletions.
There are no files selected for viewing
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
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,33 @@ | ||
|
||
## python examples/censures.py db0 | ||
|
||
import os | ||
import argparse | ||
from pythonseer import Fediseer | ||
from pythonseer.types import FormatType | ||
|
||
|
||
arg_parser = argparse.ArgumentParser() | ||
arg_parser.add_argument('-d', '--fediverse_domain', action='store', required=False, type=str, help="the fediverse instance domain for which to look up censures") | ||
arg_parser.add_argument('-m', '--min_hesitations', action='store', required=False, default=1, type=int, help="The min amount of hesitations to require for each instance") | ||
arg_parser.add_argument('-r', '--reasons', action='store', required=False, type=str, help="A csv of reasons with which to filter intances") | ||
args = arg_parser.parse_args() | ||
|
||
|
||
|
||
fediverse_domain = args.fediverse_domain | ||
if not fediverse_domain: | ||
fediverse_domain = os.getenv('FEDIVERSE_DOMAIN', "lemmy.dbzer0.com,lemmings.world,lemmy.world") | ||
if not fediverse_domain: | ||
raise Exception("You need to provide a fediverse domain via env var or arg") | ||
|
||
fediseer = Fediseer() | ||
hesitations = fediseer.hesitation.get_given( | ||
domain_set=fediverse_domain, | ||
reasons=args.reasons, | ||
min_hesitations=args.min_hesitations, | ||
format=FormatType.CSV) | ||
if hesitations: | ||
print(f"{fediverse_domain} has hesitated against the following instances: {hesitations['csv']}") | ||
else: | ||
print("Retrieval of instance hesitations failed") |
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
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
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
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,141 @@ | ||
from typing import Any, List, Optional, Union | ||
|
||
from pythonseer.requestor import Request, Requestor | ||
from pythonseer.types import FormatType | ||
|
||
|
||
class Hesitation: | ||
def __init__(self, _requestor: Requestor): | ||
self._requestor = _requestor | ||
|
||
def create( | ||
self, | ||
domain: str, | ||
reason: str = None, | ||
evidence: str = None, | ||
) -> Optional[dict]: | ||
""" | ||
Create a hesitation | ||
Requires to be logged-in | ||
Args: | ||
domain (str) | ||
reason (str), optional | ||
evidence (str), optional | ||
Returns: | ||
Optional[dict]: put data if successful | ||
""" | ||
payload = {} | ||
if reason is not None: | ||
payload["reason"] = reason | ||
if evidence is not None: | ||
payload["evidence"] = evidence | ||
return self._requestor.api(Request.PUT, f"/hesitations/{domain}",json = payload) | ||
|
||
def modify( | ||
self, | ||
domain: str, | ||
reason: str = None, | ||
evidence: str = None, | ||
) -> Optional[dict]: | ||
""" | ||
modify a hesitation | ||
Requires to be logged-in | ||
Args: | ||
domain (str) | ||
reason (str), optional | ||
evidence (str), optional | ||
Returns: | ||
Optional[dict]: patch data if successful | ||
""" | ||
payload = {} | ||
if reason is not None: | ||
payload["reason"] = reason | ||
if evidence is not None: | ||
payload["evidence"] = evidence | ||
return self._requestor.api(Request.PATCH, f"/hesitations/{domain}",json = payload) | ||
|
||
def delete( | ||
self, | ||
domain: str, | ||
) -> Optional[dict]: | ||
""" | ||
Withdraw a hesitation | ||
Requires to be logged-in | ||
Args: | ||
domain (str) | ||
Returns: | ||
Optional[dict]: delete data if successful | ||
""" | ||
return self._requestor.api(Request.DELETE, f"/hesitations/{domain}") | ||
|
||
def get_received( | ||
self, | ||
domain: str = None, | ||
format: Optional[FormatType] = FormatType.FULL | ||
) -> Optional[dict]: | ||
""" | ||
Retrieve hesitations received by specific domain | ||
Does not require to be logged-in | ||
If logged-in, defaults to user's home domain | ||
Args: | ||
domain (str) | ||
format (Optional[FormatType], optional): Defaults to FormatType.FULL. | ||
Returns: | ||
Optional[dict]: get data if successful | ||
""" | ||
if not domain: | ||
domain = self._requestor.home_domain | ||
if not domain: | ||
raise Exception("Must provide a domain or login to GET /hesitations/ endpoint") | ||
endpoint = f"/hesitations/{domain}{format.get_query('?')}" | ||
return self._requestor.api(Request.GET, endpoint) | ||
|
||
def get_given( | ||
self, | ||
domain_set: set = None, | ||
reasons: set = None, | ||
min_hesitations: int = 1, | ||
format: Optional[FormatType] = FormatType.FULL | ||
) -> Optional[dict]: | ||
""" | ||
Retrieve hesitations given out by specific domain | ||
Does not require to be logged-in | ||
If logged-in, defaults to user's home domain | ||
Args: | ||
domain_set (set or str), optional | ||
reasons (set or str), optional | ||
min_hesitations (int), optional | ||
format (Optional[FormatType], optional): Defaults to FormatType.FULL. | ||
Returns: | ||
Optional[dict]: get data if successful | ||
""" | ||
if not domain_set: | ||
domain_csv = self._requestor.home_domain | ||
if not domain_csv: | ||
raise Exception("Must provide a domain or login to GET /hesitations_given/ endpoint") | ||
# Handle sending the domain name as str gracefully | ||
elif type(domain_set) is str: | ||
domain_csv = domain_set | ||
elif type(domain_set) is set: | ||
domain_csv = ','.join(domain_set) | ||
else: | ||
raise Exception("'domain' has to be a set or a string") | ||
reasons_query = '' | ||
if reasons is not None: | ||
if type(reasons) is str: | ||
reasons_csv = reasons | ||
else: | ||
reasons_csv = ','.join(reasons) | ||
reasons_query = f"&reasons_csv={reasons_csv}" | ||
endpoint = f"/hesitations_given/{domain_csv}?min_hesitations={min_hesitations}{reasons_query}{format.get_query('&')}" | ||
return self._requestor.api(Request.GET, endpoint) |