Skip to content

Commit

Permalink
Docstrings
Browse files Browse the repository at this point in the history
- written docstrings for methods
  • Loading branch information
CustomIcon committed Jan 2, 2021
1 parent a083673 commit d7289a2
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 30 deletions.
38 changes: 28 additions & 10 deletions examples/async_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

async def main():
# calling for status
user = input("Enter a Username or UserID to check Spam Prediction on SPB: ")
user = input("Enter a Username or UserID: ")
status = await client.check_blacklist(user)
# check if status got a successful response
if status.success:
Expand All @@ -18,12 +18,20 @@ async def main():


async def text_parser(status: Blacklist):
text = "Private TelegramID: {}\n".format(status.private_telegram_id)
text = "Private TelegramID: {}\n".format(
status.private_telegram_id
)
text += "Entity Type: {}\n".format(status.entity_type)
if status.attributes.is_blacklisted:
text += "Blacklist Flag: {}\n".format(status.attributes.blacklist_flag)
text += "Blacklist Reason: {}\n".format(status.attributes.blacklist_reason)
text += "Original PrivateID: {}\n".format(status.attributes.original_private_id)
text += "Blacklist Flag: {}\n".format(
status.attributes.blacklist_flag
)
text += "Blacklist Reason: {}\n".format(
status.attributes.blacklist_reason
)
text += "Original PrivateID: {}\n".format(
status.attributes.original_private_id
)
if status.attributes.is_potential_spammer:
text += "This user is a Spammer\n"
if status.attributes.is_operator:
Expand All @@ -36,11 +44,21 @@ async def text_parser(status: Blacklist):
text += "This user is an Intellivoid Verified Account\n"
if status.attributes.is_official:
text += "This is an Official Account\n"
text += "Language: {}\n".format(status.language_prediction.language)
text += "Language Probability: {}\n".format(status.language_prediction.probability)
text += "Ham Prediction: {}\n".format(status.spam_prediction.ham_prediction)
text += "Spam Prediction: {}\n".format(status.spam_prediction.spam_prediction)
text += "Last Updated On: {}\n".format(status.last_updated)
text += "Language: {}\n".format(
status.language_prediction.language
)
text += "Language Probability: {}\n".format(
status.language_prediction.probability
)
text += "Ham Prediction: {}\n".format(
status.spam_prediction.ham_prediction
)
text += "Spam Prediction: {}\n".format(
status.spam_prediction.spam_prediction
)
text += "Last Updated On: {}\n".format(
status.last_updated
)
return text


Expand Down
42 changes: 31 additions & 11 deletions examples/sync_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def main():
# calling for status
user = input("Enter a Username or UserID to check Spam Prediction on SPB: ")
user = input("Enter a Username or UserID: ")
status = client.check_blacklist(user)
# check if status got a successful response
if status.success:
Expand All @@ -17,12 +17,22 @@ def main():


def text_parser(status: Blacklist):
text = "Private TelegramID: {}\n".format(status.private_telegram_id)
text += "Entity Type: {}\n".format(status.entity_type)
text = "Private TelegramID: {}\n".format(
status.private_telegram_id
)
text += "Entity Type: {}\n".format(
status.entity_type
)
if status.attributes.is_blacklisted:
text += "Blacklist Flag: {}\n".format(status.attributes.blacklist_flag)
text += "Blacklist Reason: {}\n".format(status.attributes.blacklist_reason)
text += "Original PrivateID: {}\n".format(status.attributes.original_private_id)
text += "Blacklist Flag: {}\n".format(
status.attributes.blacklist_flag
)
text += "Blacklist Reason: {}\n".format(
status.attributes.blacklist_reason
)
text += "Original PrivateID: {}\n".format(
status.attributes.original_private_id
)
if status.attributes.is_potential_spammer:
text += "This user is a Spammer\n"
if status.attributes.is_operator:
Expand All @@ -35,11 +45,21 @@ def text_parser(status: Blacklist):
text += "This user is an Intellivoid Verified Account\n"
if status.attributes.is_official:
text += "This is an Official Account\n"
text += "Language: {}\n".format(status.language_prediction.language)
text += "Language Probability: {}\n".format(status.language_prediction.probability)
text += "Ham Prediction: {}\n".format(status.spam_prediction.ham_prediction)
text += "Spam Prediction: {}\n".format(status.spam_prediction.spam_prediction)
text += "Last Updated On: {}\n".format(status.last_updated)
text += "Language: {}\n".format(
status.language_prediction.language
)
text += "Language Probability: {}\n".format(
status.language_prediction.probability
)
text += "Ham Prediction: {}\n".format(
status.spam_prediction.ham_prediction
)
text += "Spam Prediction: {}\n".format(
status.spam_prediction.spam_prediction
)
text += "Last Updated On: {}\n".format(
status.last_updated
)
return text


Expand Down
1 change: 1 addition & 0 deletions spamprotection/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Initial Directory."""
from .client import SPBClient # noqa: F401

__version__ = "0.0.4"
43 changes: 39 additions & 4 deletions spamprotection/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,61 @@

class SPBClient:
def __init__(
self, *, host: str = "https://api.intellivoid.net/spamprotection/v1/lookup"
self,
*,
host: str = "https://api.intellivoid.net/spamprotection/v1/lookup"
) -> None:
self._host = host

async def do_request(self, user_id: str, method: str = "get"):
async def do_request(
self,
user_id: str,
):
"""[Requests to the url]
Args:
user_id (str): [username or user_id can be passed into the arg]
Returns:
[json]: [json response of the output]
"""
async with aiohttp.ClientSession() as ses:
request = await ses.get(f"{self._host}?query={user_id}")
try:
return await request.json(), request
except JSONDecodeError:
return await request.text(), request

async def raw_output(self, user_id: Union[int, str]) -> Union[Blacklist, bool]:
async def raw_output(
self,
user_id: Union[int, str]
):
"""[raw json output]
Args:
user_id (Union[int, str]): [can pass user_id or username]
Returns:
[json]: [returns json response]
"""
try:
data, _ = await self.do_request(user_id)
return data
except UnknownError:
return False

async def check_blacklist(self, user_id: Union[int, str]) -> Union[Blacklist, bool]:
async def check_blacklist(
self,
user_id: Union[int, str]
) -> Union[Blacklist, bool]:
"""[checks spb for blacklist]
Args:
user_id (Union[int, str]): [can pass user_id or username]
Returns:
Union[Blacklist, bool]: [Blacklist type]
"""
try:
data, _ = await self.do_request(user_id)
return Blacklist(**data)
Expand Down
43 changes: 39 additions & 4 deletions spamprotection/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,60 @@

class SPBClient:
def __init__(
self, *, host: str = "https://api.intellivoid.net/spamprotection/v1/lookup"
self,
*,
host: str = "https://api.intellivoid.net/spamprotection/v1/lookup"
) -> None:
self._host = host

def do_request(self, user_id: str, method: str = "get"):
def do_request(
self,
user_id: str,
):
"""[Requests to the url]
Args:
user_id (str): [username or user_id can be passed into the arg]
Returns:
[json]: [json response of the output]
"""
request = requests.get(f"{self._host}?query={user_id}")
try:
return request.json(), request
except JSONDecodeError:
return request.text(), request

def raw_output(self, user_id: Union[int, str]) -> Union[Blacklist, bool]:
def raw_output(
self,
user_id: Union[int, str]
):
"""[raw json output]
Args:
user_id (Union[int, str]): [can pass user_id or username]
Returns:
[json]: [returns json response]
"""
try:
data, _ = self.do_request(user_id)
return data
except UnknownError:
return False

def check_blacklist(self, user_id: Union[int, str]) -> Union[Blacklist, bool]:
def check_blacklist(
self,
user_id: Union[int, str]
) -> Union[Blacklist, bool]:
"""[checks spb for blacklist]
Args:
user_id (Union[int, str]): [can pass user_id or username]
Returns:
Union[Blacklist, bool]: [Blacklist type]
"""
try:
data, _ = self.do_request(user_id)
return Blacklist(**data)
Expand Down
8 changes: 7 additions & 1 deletion spamprotection/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ def __repr__(self) -> str:


class Blacklist(SPB):
def __init__(self, success: bool, response_code: int, results=dict, **kwargs):
def __init__(
self,
success: bool,
response_code: int,
results=dict,
**kwargs
):
self.success = success
self.response_code = response_code
try:
Expand Down

0 comments on commit d7289a2

Please sign in to comment.