Updated to 0.0.3

- introduce to sync
This commit is contained in:
ポキ 2021-01-02 14:09:20 +05:00
parent e97840b00d
commit 30833d0717
13 changed files with 122 additions and 13 deletions

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
install:
python3 setup.py install
build:
python3 setup.py sdist
upload:
twine upload dist/*

0
examples/__init__.py Normal file
View File

View File

@ -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()

47
examples/sync_example.py Normal file
View File

@ -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()

View File

@ -1,3 +1,3 @@
aiohttp==3.7.3
asyncio==3.4.3
attrdict==2.0.1
requests==2.20

4
requirements_dev.txt Normal file
View File

@ -0,0 +1,4 @@
aiohttp==3.7.3
asyncio==3.4.3
requests==2.20
twine==3.3.0

2
setup.cfg Normal file
View File

@ -0,0 +1,2 @@
[metadata]
description-file = README.md

View File

@ -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,

View File

@ -1,3 +1,3 @@
from .client import SPBClient # noqa: F401
__version__ = "0.0.2"
__version__ = "0.0.3"

View File

@ -34,4 +34,4 @@ class SPBClient:
except UnknownError:
return False
except aiohttp.client_exceptions.ClientConnectorError:
return "Api is down at the moment"
return "Api is down at the moment"

35
spamprotection/sync.py Normal file
View File

@ -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

View File

@ -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

6
spamprotection/utils.py Normal file
View File

@ -0,0 +1,6 @@
class attrdict(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
def __getattr__(self, name):
return self[name]