py-spam-hunter-client 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Makar Mikhalchenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ include LICENSE
2
+ include README.md
@@ -0,0 +1,96 @@
1
+ Metadata-Version: 2.1
2
+ Name: py-spam-hunter-client
3
+ Version: 1.0.0
4
+ Summary: A client for SpamHunter API
5
+ Home-page: https://github.com/MakarMS/py-spam-hunter-client
6
+ Author: Makar
7
+ Author-email: makar.mikhalchenko@bk.ru
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: aiohttp
15
+ Requires-Dist: requests
16
+ Requires-Dist: langdetect
17
+
18
+ # SpamHunter Client
19
+
20
+ A Python client for SpamHunter API to check messages for spam probability.<br>This package supports both synchronous and asynchronous usage.
21
+
22
+ Documentation: https://spam-hunter.ru/documentation
23
+
24
+ ## Installation
25
+
26
+ You can install the library via pip:
27
+
28
+ `pip install spam-hunter-client`
29
+
30
+ ## Usage
31
+
32
+ ### Asynchronous Example
33
+
34
+ To use the asynchronous version of the API, you can create an `AsyncSpamHunter` instance and call `check` in an asynchronous context. Below is an example of how to use it with `asyncio`:
35
+
36
+ ```python
37
+ import asyncio
38
+ from py_spam_hunter_client import AsyncSpamHunter, Message
39
+
40
+
41
+ async def check_message():
42
+ spam_hunter = AsyncSpamHunter('Your API key')
43
+
44
+ checked_messages = await spam_hunter.check(
45
+ [
46
+ Message('Кто хочет заработать? В ЛС!', 'ru'),
47
+ Message('Кто хочет заработать? В ЛС!')
48
+ ]
49
+ )
50
+
51
+ for checked_message in checked_messages:
52
+ print(checked_message.get_spam_probability())
53
+
54
+
55
+ asyncio.run(check_message())
56
+ ```
57
+
58
+ ### Synchronous Example
59
+ To use the synchronous version of the API, you can use SyncSpamHunter. Here's an example of how to use it in a normal Python function:
60
+
61
+ ```python
62
+ from py_spam_hunter_client import SyncSpamHunter, Message
63
+
64
+ spam_hunter = SyncSpamHunter('Your API key')
65
+
66
+ checked_messages = spam_hunter.check(
67
+ [
68
+ Message('Кто хочет заработать? В ЛС!', 'ru'),
69
+ Message('Кто хочет заработать? В ЛС!')
70
+ ]
71
+ )
72
+
73
+ for checked_message in checked_messages:
74
+ print(checked_message.get_spam_probability())
75
+ ```
76
+
77
+
78
+ ### Methods
79
+ `AsyncSpamHunter.check(messages: List[Message]) -> List[CheckedMessage]`<br>`SyncSpamHunter.check(messages: List[Message]) -> List[CheckedMessage]`
80
+
81
+ **CheckException** if the request fails or if the API returns an error.
82
+
83
+ ### Message Object
84
+
85
+ - **id** (`str`): (Optional) A custom ID for the message.
86
+ - **text** (`str`): The content of the message.
87
+ - **language** (`str`): The language of the message. It can be either:
88
+ - `'ru'` (Russian)
89
+ - `'en'` (English)
90
+ - Left empty for auto-detection.
91
+
92
+ ### CheckedMessage Object
93
+
94
+ - **id** (`str`): (Optional) The custom ID of the checked message.
95
+ - **spam_probability** (`float`): The spam probability of the message, a value between 0 and 1.
96
+
@@ -0,0 +1,79 @@
1
+ # SpamHunter Client
2
+
3
+ A Python client for SpamHunter API to check messages for spam probability.<br>This package supports both synchronous and asynchronous usage.
4
+
5
+ Documentation: https://spam-hunter.ru/documentation
6
+
7
+ ## Installation
8
+
9
+ You can install the library via pip:
10
+
11
+ `pip install spam-hunter-client`
12
+
13
+ ## Usage
14
+
15
+ ### Asynchronous Example
16
+
17
+ To use the asynchronous version of the API, you can create an `AsyncSpamHunter` instance and call `check` in an asynchronous context. Below is an example of how to use it with `asyncio`:
18
+
19
+ ```python
20
+ import asyncio
21
+ from py_spam_hunter_client import AsyncSpamHunter, Message
22
+
23
+
24
+ async def check_message():
25
+ spam_hunter = AsyncSpamHunter('Your API key')
26
+
27
+ checked_messages = await spam_hunter.check(
28
+ [
29
+ Message('Кто хочет заработать? В ЛС!', 'ru'),
30
+ Message('Кто хочет заработать? В ЛС!')
31
+ ]
32
+ )
33
+
34
+ for checked_message in checked_messages:
35
+ print(checked_message.get_spam_probability())
36
+
37
+
38
+ asyncio.run(check_message())
39
+ ```
40
+
41
+ ### Synchronous Example
42
+ To use the synchronous version of the API, you can use SyncSpamHunter. Here's an example of how to use it in a normal Python function:
43
+
44
+ ```python
45
+ from py_spam_hunter_client import SyncSpamHunter, Message
46
+
47
+ spam_hunter = SyncSpamHunter('Your API key')
48
+
49
+ checked_messages = spam_hunter.check(
50
+ [
51
+ Message('Кто хочет заработать? В ЛС!', 'ru'),
52
+ Message('Кто хочет заработать? В ЛС!')
53
+ ]
54
+ )
55
+
56
+ for checked_message in checked_messages:
57
+ print(checked_message.get_spam_probability())
58
+ ```
59
+
60
+
61
+ ### Methods
62
+ `AsyncSpamHunter.check(messages: List[Message]) -> List[CheckedMessage]`<br>`SyncSpamHunter.check(messages: List[Message]) -> List[CheckedMessage]`
63
+
64
+ **CheckException** if the request fails or if the API returns an error.
65
+
66
+ ### Message Object
67
+
68
+ - **id** (`str`): (Optional) A custom ID for the message.
69
+ - **text** (`str`): The content of the message.
70
+ - **language** (`str`): The language of the message. It can be either:
71
+ - `'ru'` (Russian)
72
+ - `'en'` (English)
73
+ - Left empty for auto-detection.
74
+
75
+ ### CheckedMessage Object
76
+
77
+ - **id** (`str`): (Optional) The custom ID of the checked message.
78
+ - **spam_probability** (`float`): The spam probability of the message, a value between 0 and 1.
79
+
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,25 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name="py-spam-hunter-client",
5
+ version="1.0.0",
6
+ packages=find_packages(where="src"),
7
+ package_dir={"": "src"},
8
+ install_requires=[
9
+ 'aiohttp',
10
+ 'requests',
11
+ 'langdetect'
12
+ ],
13
+ classifiers=[
14
+ 'Programming Language :: Python :: 3',
15
+ 'License :: OSI Approved :: MIT License',
16
+ 'Operating System :: OS Independent',
17
+ ],
18
+ python_requires='>=3.6',
19
+ author="Makar",
20
+ author_email="makar.mikhalchenko@bk.ru",
21
+ description="A client for SpamHunter API",
22
+ long_description=open('README.md').read(),
23
+ long_description_content_type='text/markdown',
24
+ url="https://github.com/MakarMS/py-spam-hunter-client",
25
+ )
@@ -0,0 +1,5 @@
1
+ from .async_spam_hunter import AsyncSpamHunter
2
+ from .sync_spam_hunter import SyncSpamHunter
3
+ from .messages import Message
4
+ from .messages import CheckedMessage
5
+ from .exceptions import CheckException
@@ -0,0 +1,51 @@
1
+ from typing import List
2
+
3
+ import aiohttp
4
+
5
+ from src.py_spam_hunter_client.exceptions.check_exception import CheckException
6
+ from src.py_spam_hunter_client.messages.checked_message import CheckedMessage
7
+ from src.py_spam_hunter_client.messages.message import Message
8
+
9
+
10
+ class AsyncSpamHunter:
11
+ BASE_URL = 'https://backend.spam-hunter.ru/api/v1/check'
12
+
13
+ def __init__(self, api_key: str):
14
+ self.__api_key = api_key
15
+
16
+ async def check(self, messages: List[Message]) -> List[CheckedMessage]:
17
+ """
18
+ Checks a list of messages for spam probability
19
+ :param messages: A list of Message objects to be checked.
20
+ :return: A list of CheckedMessage objects with spam probability and IDs.
21
+ :raises CheckException: If the request fails or the API returns an error.
22
+ """
23
+ data = {'messages': [], 'api_key': self.__api_key}
24
+
25
+ for message in messages:
26
+ data['messages'].append(
27
+ {
28
+ 'id': message.get_id(),
29
+ 'message': message.get_text(),
30
+ 'language': message.get_language()
31
+ }
32
+ )
33
+
34
+ async with aiohttp.ClientSession() as session:
35
+ async with session.post(self.BASE_URL, json=data) as response:
36
+ json = await response.json()
37
+
38
+ if response.status == 200:
39
+ checked_messages = []
40
+
41
+ for message in json['messages']:
42
+ checked_messages.append(
43
+ CheckedMessage(
44
+ message['spam_probability'],
45
+ message['id'] if 'id' in message else ''
46
+ )
47
+ )
48
+
49
+ return checked_messages
50
+ else:
51
+ raise CheckException(json['errors'][0])
@@ -0,0 +1 @@
1
+ from .check_exception import CheckException
@@ -0,0 +1,3 @@
1
+ class CheckException(Exception):
2
+ def __init__(self, message: str):
3
+ super().__init__(message)
@@ -0,0 +1,2 @@
1
+ from .message import Message
2
+ from .checked_message import CheckedMessage
@@ -0,0 +1,10 @@
1
+ class CheckedMessage:
2
+ def __init__(self, spam_probability: float, id: str = ''):
3
+ self.__id = id
4
+ self.__spam_probability = spam_probability
5
+
6
+ def get_id(self):
7
+ return self.__id
8
+
9
+ def get_spam_probability(self):
10
+ return self.__spam_probability
@@ -0,0 +1,34 @@
1
+ from langdetect import detect, LangDetectException
2
+ from langdetect import DetectorFactory
3
+
4
+ DetectorFactory.seed = 0
5
+
6
+
7
+ class Message:
8
+ """
9
+ Class representing a message with text, language, and an optional ID.
10
+ If language is not provided, it will be detected automatically.
11
+ """
12
+ def __init__(self, text: str, language: str = '', id: str = ''):
13
+ self.__text = text
14
+ self.__language = language if language else self.detect_language(text)
15
+ self.__id = id
16
+
17
+ def get_id(self):
18
+ return self.__id
19
+
20
+ def get_text(self):
21
+ return self.__text
22
+
23
+ def get_language(self):
24
+ return self.__language
25
+
26
+ @staticmethod
27
+ def detect_language(text: str):
28
+ try:
29
+ lang = detect(text)
30
+ if lang not in ['en', 'ru']:
31
+ return 'xx'
32
+ return lang
33
+ except LangDetectException:
34
+ return 'xx'
@@ -0,0 +1,49 @@
1
+ import requests
2
+ from typing import List
3
+
4
+ from src.py_spam_hunter_client.exceptions.check_exception import CheckException
5
+ from src.py_spam_hunter_client.messages.checked_message import CheckedMessage
6
+ from src.py_spam_hunter_client.messages.message import Message
7
+
8
+
9
+ class SyncSpamHunter:
10
+ BASE_URL = 'https://backend.spam-hunter.ru/api/v1/check'
11
+
12
+ def __init__(self, api_key: str):
13
+ self.__api_key = api_key
14
+
15
+ def check(self, messages: List[Message]) -> List[CheckedMessage]:
16
+ """
17
+ Checks a list of messages for spam probability
18
+ :param messages: A list of Message objects to be checked.
19
+ :return: A list of CheckedMessage objects with spam probability and IDs.
20
+ :raises CheckException: If the request fails or the API returns an error.
21
+ """
22
+ data = {'messages': [], 'api_key': self.__api_key}
23
+
24
+ for message in messages:
25
+ data['messages'].append(
26
+ {
27
+ 'id': message.get_id(),
28
+ 'message': message.get_text(),
29
+ 'language': message.get_language()
30
+ }
31
+ )
32
+
33
+ response = requests.post(self.BASE_URL, json=data)
34
+
35
+ if response.status_code == 200:
36
+ checked_messages = []
37
+
38
+ json = response.json()
39
+ for message in json['messages']:
40
+ checked_messages.append(
41
+ CheckedMessage(
42
+ message['spam_probability'],
43
+ message['id'] if 'id' in message else ''
44
+ )
45
+ )
46
+
47
+ return checked_messages
48
+ else:
49
+ raise CheckException(response.json()['errors'][0])
@@ -0,0 +1,96 @@
1
+ Metadata-Version: 2.1
2
+ Name: py-spam-hunter-client
3
+ Version: 1.0.0
4
+ Summary: A client for SpamHunter API
5
+ Home-page: https://github.com/MakarMS/py-spam-hunter-client
6
+ Author: Makar
7
+ Author-email: makar.mikhalchenko@bk.ru
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.6
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: aiohttp
15
+ Requires-Dist: requests
16
+ Requires-Dist: langdetect
17
+
18
+ # SpamHunter Client
19
+
20
+ A Python client for SpamHunter API to check messages for spam probability.<br>This package supports both synchronous and asynchronous usage.
21
+
22
+ Documentation: https://spam-hunter.ru/documentation
23
+
24
+ ## Installation
25
+
26
+ You can install the library via pip:
27
+
28
+ `pip install spam-hunter-client`
29
+
30
+ ## Usage
31
+
32
+ ### Asynchronous Example
33
+
34
+ To use the asynchronous version of the API, you can create an `AsyncSpamHunter` instance and call `check` in an asynchronous context. Below is an example of how to use it with `asyncio`:
35
+
36
+ ```python
37
+ import asyncio
38
+ from py_spam_hunter_client import AsyncSpamHunter, Message
39
+
40
+
41
+ async def check_message():
42
+ spam_hunter = AsyncSpamHunter('Your API key')
43
+
44
+ checked_messages = await spam_hunter.check(
45
+ [
46
+ Message('Кто хочет заработать? В ЛС!', 'ru'),
47
+ Message('Кто хочет заработать? В ЛС!')
48
+ ]
49
+ )
50
+
51
+ for checked_message in checked_messages:
52
+ print(checked_message.get_spam_probability())
53
+
54
+
55
+ asyncio.run(check_message())
56
+ ```
57
+
58
+ ### Synchronous Example
59
+ To use the synchronous version of the API, you can use SyncSpamHunter. Here's an example of how to use it in a normal Python function:
60
+
61
+ ```python
62
+ from py_spam_hunter_client import SyncSpamHunter, Message
63
+
64
+ spam_hunter = SyncSpamHunter('Your API key')
65
+
66
+ checked_messages = spam_hunter.check(
67
+ [
68
+ Message('Кто хочет заработать? В ЛС!', 'ru'),
69
+ Message('Кто хочет заработать? В ЛС!')
70
+ ]
71
+ )
72
+
73
+ for checked_message in checked_messages:
74
+ print(checked_message.get_spam_probability())
75
+ ```
76
+
77
+
78
+ ### Methods
79
+ `AsyncSpamHunter.check(messages: List[Message]) -> List[CheckedMessage]`<br>`SyncSpamHunter.check(messages: List[Message]) -> List[CheckedMessage]`
80
+
81
+ **CheckException** if the request fails or if the API returns an error.
82
+
83
+ ### Message Object
84
+
85
+ - **id** (`str`): (Optional) A custom ID for the message.
86
+ - **text** (`str`): The content of the message.
87
+ - **language** (`str`): The language of the message. It can be either:
88
+ - `'ru'` (Russian)
89
+ - `'en'` (English)
90
+ - Left empty for auto-detection.
91
+
92
+ ### CheckedMessage Object
93
+
94
+ - **id** (`str`): (Optional) The custom ID of the checked message.
95
+ - **spam_probability** (`float`): The spam probability of the message, a value between 0 and 1.
96
+
@@ -0,0 +1,17 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ setup.py
5
+ src/py_spam_hunter_client/__init__.py
6
+ src/py_spam_hunter_client/async_spam_hunter.py
7
+ src/py_spam_hunter_client/sync_spam_hunter.py
8
+ src/py_spam_hunter_client.egg-info/PKG-INFO
9
+ src/py_spam_hunter_client.egg-info/SOURCES.txt
10
+ src/py_spam_hunter_client.egg-info/dependency_links.txt
11
+ src/py_spam_hunter_client.egg-info/requires.txt
12
+ src/py_spam_hunter_client.egg-info/top_level.txt
13
+ src/py_spam_hunter_client/exceptions/__init__.py
14
+ src/py_spam_hunter_client/exceptions/check_exception.py
15
+ src/py_spam_hunter_client/messages/__init__.py
16
+ src/py_spam_hunter_client/messages/checked_message.py
17
+ src/py_spam_hunter_client/messages/message.py
@@ -0,0 +1,3 @@
1
+ aiohttp
2
+ requests
3
+ langdetect