From 30833d07170ab3526ecf9ceb1c7b080397c5259d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9D=E3=82=AD?= Date: Sat, 2 Jan 2021 14:09:20 +0500 Subject: [PATCH] Updated to 0.0.3 - introduce to sync --- Makefile | 9 +++++ examples/__init__.py | 0 example.py => examples/async_example.py | 7 ++-- examples/sync_example.py | 47 +++++++++++++++++++++++++ requirements.txt | 2 +- requirements_dev.txt | 4 +++ setup.cfg | 2 ++ setup.py | 1 + spamprotection/__init__.py | 2 +- spamprotection/client.py | 2 +- spamprotection/sync.py | 35 ++++++++++++++++++ spamprotection/types.py | 18 ++++++---- spamprotection/utils.py | 6 ++++ 13 files changed, 122 insertions(+), 13 deletions(-) create mode 100644 Makefile create mode 100644 examples/__init__.py rename example.py => examples/async_example.py (94%) create mode 100644 examples/sync_example.py create mode 100644 requirements_dev.txt create mode 100644 setup.cfg create mode 100644 spamprotection/sync.py create mode 100644 spamprotection/utils.py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e0ba307 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ + +install: + python3 setup.py install + +build: + python3 setup.py sdist + +upload: + twine upload dist/* \ No newline at end of file diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/example.py b/examples/async_example.py similarity index 94% rename from example.py rename to examples/async_example.py index e171649..360750f 100644 --- a/example.py +++ b/examples/async_example.py @@ -44,6 +44,7 @@ async def text_parser(status: Blacklist): return text -loop = asyncio.get_event_loop() -loop.run_until_complete(main()) -loop.close() +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) + loop.close() diff --git a/examples/sync_example.py b/examples/sync_example.py new file mode 100644 index 0000000..cc619eb --- /dev/null +++ b/examples/sync_example.py @@ -0,0 +1,47 @@ +from spamprotection.sync import SPBClient +from spamprotection.types import Blacklist + +# initializing the client +client = SPBClient() + + +def main(): + # calling for status + user = input("Enter a Username or UserID to check Spam Prediction on SPB: ") + status = client.check_blacklist(user) + # check if status got a successful response + if status.success: + print((text_parser(status))) + else: + print("Polish Cow did not Approve this!") + + +def text_parser(status: Blacklist): + 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) + if status.attributes.is_potential_spammer: + text += "This user is a Spammer\n" + if status.attributes.is_operator: + text += "This user is an Operator\n" + if status.attributes.is_agent: + text += "This user is an Agent\n" + if status.attributes.is_whitelisted: + text += "This user is Whitelisted\n" + if status.attributes.intellivoid_accounts_verified: + 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) + return text + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt index b9c0f70..9cbe5e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ aiohttp==3.7.3 asyncio==3.4.3 -attrdict==2.0.1 +requests==2.20 diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..df29eff --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,4 @@ +aiohttp==3.7.3 +asyncio==3.4.3 +requests==2.20 +twine==3.3.0 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..b88034e --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[metadata] +description-file = README.md diff --git a/setup.py b/setup.py index 93611cb..bfd3e52 100644 --- a/setup.py +++ b/setup.py @@ -21,6 +21,7 @@ setuptools.setup( url="https://github.com/pokurt/intellivoid-spam-protection", packages=setuptools.find_packages(), classifiers=[ + "Development Status :: 3 - Alpha", "Programming Language :: Python :: 3.8", ], install_requires=dependencies, diff --git a/spamprotection/__init__.py b/spamprotection/__init__.py index fb1be3b..4d3932b 100644 --- a/spamprotection/__init__.py +++ b/spamprotection/__init__.py @@ -1,3 +1,3 @@ from .client import SPBClient # noqa: F401 -__version__ = "0.0.2" +__version__ = "0.0.3" diff --git a/spamprotection/client.py b/spamprotection/client.py index 1e2630b..b489a3e 100644 --- a/spamprotection/client.py +++ b/spamprotection/client.py @@ -34,4 +34,4 @@ class SPBClient: except UnknownError: return False except aiohttp.client_exceptions.ClientConnectorError: - return "Api is down at the moment" \ No newline at end of file + return "Api is down at the moment" diff --git a/spamprotection/sync.py b/spamprotection/sync.py new file mode 100644 index 0000000..edfe97e --- /dev/null +++ b/spamprotection/sync.py @@ -0,0 +1,35 @@ +import requests + +from .errors import UnknownError +from .types import Blacklist + +from json import JSONDecodeError +from typing import Union + + +class SPBClient: + def __init__( + self, *, host: str = "https://api.intellivoid.net/spamprotection/v1/lookup" + ) -> None: + self._host = host + + def do_request(self, user_id: str, method: str = "get"): + 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]: + 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]: + try: + data, _ = self.do_request(user_id) + return Blacklist(**data) + except UnknownError: + return False diff --git a/spamprotection/types.py b/spamprotection/types.py index fd149b6..52bba60 100644 --- a/spamprotection/types.py +++ b/spamprotection/types.py @@ -1,5 +1,6 @@ from datetime import datetime -from attrdict import AttrDict +from .utils import attrdict + class SPB: def __str__(self) -> str: @@ -13,9 +14,12 @@ class Blacklist(SPB): def __init__(self, success: bool, response_code: int, results=dict, **kwargs): self.success = success self.response_code = response_code - self.private_telegram_id = results["private_telegram_id"] - self.entity_type = results["entity_type"] - self.attributes = AttrDict(results["attributes"]) - self.language_prediction = AttrDict(results["language_prediction"]) - self.spam_prediction = AttrDict(results["spam_prediction"]) - self.last_updated = datetime.fromtimestamp(results["last_updated"]) + try: + self.private_telegram_id = results["private_telegram_id"] + self.entity_type = results["entity_type"] + self.attributes = attrdict(results["attributes"]) + self.language_prediction = attrdict(results["language_prediction"]) + self.spam_prediction = attrdict(results["spam_prediction"]) + self.last_updated = datetime.fromtimestamp(results["last_updated"]) + except TypeError: + pass diff --git a/spamprotection/utils.py b/spamprotection/utils.py new file mode 100644 index 0000000..f8c8333 --- /dev/null +++ b/spamprotection/utils.py @@ -0,0 +1,6 @@ +class attrdict(dict): + def __init__(self, *args, **kwargs): + dict.__init__(self, *args, **kwargs) + + def __getattr__(self, name): + return self[name]