Skip to content

Commit

Permalink
feat: Hesitations (#5)
Browse files Browse the repository at this point in the history
* feat: support for hesitations and filtering

* hesitation example
  • Loading branch information
db0 authored Sep 13, 2023
1 parent dd254b4 commit 9a77c77
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 9 deletions.
10 changes: 8 additions & 2 deletions examples/censures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@

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_censures', action='store', required=False, default=1, type=int, help="The min amount of censures 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")
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()
censures = fediseer.censure.get_given(fediverse_domain, FormatType.CSV)
censures = fediseer.censure.get_given(
domain_set=fediverse_domain,
reasons=args.reasons,
min_censures=args.min_censures,
format=FormatType.CSV)
if censures:
print(f"{fediverse_domain} has censured the following instances: {censures['csv']}")
else:
Expand Down
33 changes: 33 additions & 0 deletions examples/hesitations.py
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")
48 changes: 43 additions & 5 deletions pythonseer/censure.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,53 @@ def __init__(self, _requestor: Requestor):

def create(
self,
domain,
domain: str,
reason: str = None,
evidence: str = None,
) -> Optional[dict]:
"""
Create a censure
Requires to be logged-in
Args:
domain (str)
reason (str), optional
evidence (str), optional
Returns:
Optional[dict]: put data if successful
"""
return self._requestor.api(Request.PUT, f"/censures/{domain}")
payload = {}
if reason is not None:
payload["reason"] = reason
if evidence is not None:
payload["evidence"] = evidence
return self._requestor.api(Request.PUT, f"/censures/{domain}",json = payload)

def modify(
self,
domain: str,
reason: str = None,
evidence: str = None,
) -> Optional[dict]:
"""
modify a censure
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"/censures/{domain}",json = payload)

def delete(
self,
Expand Down Expand Up @@ -67,7 +101,7 @@ def get_received(
def get_given(
self,
domain_set: set = None,
reasons: list = None,
reasons: set = None,
min_censures: int = 1,
format: Optional[FormatType] = FormatType.FULL
) -> Optional[dict]:
Expand All @@ -78,7 +112,7 @@ def get_given(
Args:
domain_set (set or str), optional
reasons (list), optional
reasons (set or str), optional
min_censures (int), optional
format (Optional[FormatType], optional): Defaults to FormatType.FULL.
Expand All @@ -98,7 +132,11 @@ def get_given(
raise Exception("'domain' has to be a set or a string")
reasons_query = ''
if reasons is not None:
reasons_csv = ','.join(reasons)
if type(reasons) is str:
reasons_csv = reasons
else:
reasons_csv = ','.join(reasons)
reasons_query = f"&reasons_csv={reasons_csv}"
endpoint = f"/censures_given/{domain_csv}?min_censures={min_censures}{reasons_query}{format.get_query('&')}"
print(endpoint)
return self._requestor.api(Request.GET, endpoint)
41 changes: 39 additions & 2 deletions pythonseer/endorsement.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,44 @@ def __init__(self, _requestor: Requestor):
def create(
self,
domain: str,
reason: str = None,
) -> Optional[dict]:
"""
Create an endorsement
Requires to be logged-in
Args:
domain (str)
reason (str), optional
Returns:
Optional[dict]: put data if successful
"""
return self._requestor.api(Request.PUT, f"/endorsements/{domain}")
payload = {}
if reason is not None:
payload["reason"] = reason
return self._requestor.api(Request.PUT, f"/endorsements/{domain}", json=payload)

def modify(
self,
domain: str,
reason: str = None,
) -> Optional[dict]:
"""
Modify an endorsement
Requires to be logged-in
Args:
domain (str)
reason (str), optional
Returns:
Optional[dict]: put data if successful
"""
payload = {}
if reason is not None:
payload["reason"] = reason
return self._requestor.api(Request.PATCH, f"/endorsements/{domain}", json=payload)

def delete(
self,
Expand Down Expand Up @@ -67,6 +93,8 @@ def get_received(
def get_given(
self,
domain_set: set = None,
reasons: set = None,
min_endorsements: int = 1,
format: Optional[FormatType] = FormatType.FULL
) -> Optional[dict]:
"""
Expand All @@ -77,6 +105,8 @@ def get_given(
Args:
domain_set (set)
domains (str)
reasons (set or str), optional
min_endorsements (int), optional
format (Optional[FormatType], optional): Defaults to FormatType.FULL.
Returns:
Expand All @@ -91,5 +121,12 @@ def get_given(
domain_csv = domain_set
else:
domain_csv = ','.join(domain_set)
endpoint = f"/approvals/{domain_csv}{format.get_query('?')}"
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"/approvals/{domain_csv}?min_endorsements={min_endorsements}{reasons_query}{format.get_query('&')}"
return self._requestor.api(Request.GET, endpoint)
2 changes: 2 additions & 0 deletions pythonseer/fediseer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
from typing import Any, Optional

from pythonseer.hesitation import Hesitation
from pythonseer.censure import Censure
from pythonseer.endorsement import Endorsement
from pythonseer.guarantee import Guarantee
Expand All @@ -18,6 +19,7 @@ def __init__(self, api_base_url: str = "https://fediseer.com") -> None:
self._requestor = Requestor()
self._requestor.set_fediseer_domain(api_base_url)
self.censure = Censure(self._requestor)
self.hesitation = Hesitation(self._requestor)
self.endorsement = Endorsement(self._requestor)
self.guarantee = Guarantee(self._requestor)
self.suspicions = Suspicions(self._requestor)
Expand Down
141 changes: 141 additions & 0 deletions pythonseer/hesitation.py
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)

0 comments on commit 9a77c77

Please sign in to comment.