[!] Глобальные изменения

[+] Блок auth
[>] __version__ > __meta__
[>] errors > exceptions
This commit is contained in:
2025-04-02 15:20:46 +03:00
parent 44e0485f99
commit 82874814e0
14 changed files with 173 additions and 109 deletions

View File

@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
from .__version__ import __license__, __description__
from .__version__ import __version__, __url__, __build__, __title__, __author__, __author_email__, __copyright__
from .__meta__ import *
from .endpoints import *
from .api.api import AnixartUserAccount, AnixartAPI
from . import enums
from . import exceptions

View File

@@ -3,7 +3,7 @@ import logging
import requests
from ..auth import AnixartAuth
from ..errors import AnixartInitError, AnixartAPIRequestError
from ..exceptions import AnixartInitError, AnixartAPIRequestError
from ..request_handler import AnixartRequestsHandler
_log_name = "file:%-29s -> %s" % ("<anixart.api:%-4i>", "%s")

View File

@@ -12,7 +12,7 @@ class AnixartUserAccount:
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage:
>>> user = AnixartUserAccount("login", "password", config_file="anixart_data.json")
>>> print(user.login)
>>> print(user.__login)
Availible params:
~~~~~~~~~~~~~~~~~
* login -> Your anixart nick

1
anixart/auth/__init__.py Normal file
View File

@@ -0,0 +1 @@
from .account import AnixartAccount, AnixartAccountToken

112
anixart/auth/account.py Normal file
View File

@@ -0,0 +1,112 @@
import json
from pathlib import Path
import requests
from anixart.exceptions import AnixartInitError
class AnixartAccount:
def __init__(self, username: str, password: str):
self._username = username
self._password = password
if not isinstance(username, str) or not isinstance(password, str):
raise AnixartInitError("Auth data must be strings.")
self._token = None
self._session = requests.Session()
@property
def username(self):
return self._username
@property
def session(self):
return self._session
@property
def token(self):
return self._token
def to_file(self, filename: str | Path):
"""Save the account information to a file."""
acc = AnixartAccountSaved.from_account(filename, self)
acc.save()
return acc
@classmethod
def from_file(cls, filename: str | Path):
"""Load the account information from a file."""
acc = AnixartAccountSaved(filename)
acc.load()
return acc
def login(self):
"""Login to Anixart and return the token."""
# TODO: Implement login logic here
def __str__(self):
return f'AnixartAccount(login={self._username!r}, password={"*" * len(self._password)!r})'
def __repr__(self):
return f"<{self}>"
class AnixartAccountSaved(AnixartAccount):
def __init__(self, account_file: str | Path = "anixart_account.json"):
super().__init__("", "")
self._file = Path(account_file)
def save(self):
"""Save the account information to a file."""
data = {
"username": self._username,
"password": self._password,
"token": self._token
}
with open(self._file, 'w') as f:
json.dump(data, f, indent=4)
def load(self):
"""Load the account information from a file."""
if not self._file.exists():
raise AnixartInitError(f"Account file {self._file} does not exist.")
with open(self._file, 'r') as f:
data = json.load(f)
self._username = data.get("username")
self._password = data.get("password")
self._token = data.get("token")
if not self._username or not self._password:
raise AnixartInitError("Login and password must be provided in the account file.")
@classmethod
def from_account(cls, account_file: str | Path, account: AnixartAccount):
c = cls(account_file)
c._username = account.username
c._password = account._password
c._token = account.token
return c
def login(self):
"""Login to Anixart using the saved credentials."""
# Проверяем токен, если просрочен, то логинимся
# Если токен валиден, то просто дальше работаем
# TODO: Implement login logic here
pass
def __str__(self):
return f'AnixartAccountSaved(account_file={self._file!r}")'
class AnixartAccountToken(AnixartAccount):
def __init__(self, token):
super().__init__("mradx", "") # Пасхалка)
self._token = token
def login(self):
"""Login to Anixart and return information about the tokens."""
# TODO: Implement login logic here
pass
def __str__(self):
return f'AnixartAccountToken(token={self._token!r}")'

7
anixart/auth/erros.py Normal file
View File

@@ -0,0 +1,7 @@
from enum import IntEnum
class AnixartAuthError(IntEnum):
""" Error codes for AnixartApi authentication."""
INCORRECT_LOGIN = 1
INCORRECT_PASSWORD = 2

View File

@@ -3,7 +3,7 @@ import logging
import os.path
from .endpoints import SING_IN, CHANGE_PASSWORD, PROFILE
from .errors import AnixartAuthError
from .exceptions import AnixartAuthError
from .request_handler import AnixartRequestsHandler
@@ -47,20 +47,20 @@ class AnixartAuth(AnixartRequestsHandler):
uid = config.get("id")
token = config.get("token")
if not self.get(PROFILE.format(uid), {"token": token}).json().get("is_my_profile") or \
self.user.login != config.get("login"):
self.user.__login != config.get("login"):
logging.getLogger("anixart.api.AnixAPI").debug("Invalid config file. Re login.")
else:
self.user.id = uid
self.user.token = token
self.user.__token = token
return config
payload = {"login": self.user.login, "password": self.user.password}
payload = {"login": self.user.__login, "password": self.user.__password}
res = self.post(SING_IN, payload)
ready = _parse_response(res)
uid = ready["profile"]["id"]
token = ready["profileToken"]["token"]
self.user.id = uid
self.user.token = token
self._save_config({"id": uid, "token": token, "login": self.user.login})
self.user.__token = token
self._save_config({"id": uid, "token": token, "login": self.user.__login})
return ready
def change_password(self, old, new):

View File

@@ -1,27 +1,5 @@
# -*- coding: utf-8 -*-
class AnixartComment:
DISLIKE = 1
LIKE = 2
class AnixartProfileVotedSort:
LAST_FIRST = 1
OLD_FIRST = 2
STAR_5 = 3
STAR_4 = 4
STAR_3 = 5
STAR_2 = 6
STAR_1 = 7
class AnixartLists:
WATCHING = 1
IN_PLANS = 2
WATCHED = 3
POSTPONED = 4
DROPPED = 5
API_URL = "https://api.anixart.tv"

24
anixart/enums.py Normal file
View File

@@ -0,0 +1,24 @@
from enum import IntEnum
class AnixartComment(IntEnum):
DISLIKE = 1
LIKE = 2
class AnixartProfileVotedSort(IntEnum):
LAST_FIRST = 1
OLD_FIRST = 2
STAR_5 = 3
STAR_4 = 4
STAR_3 = 5
STAR_2 = 6
STAR_1 = 7
class AnixartLists(IntEnum):
WATCHING = 1
IN_PLANS = 2
WATCHED = 3
POSTPONED = 4
DROPPED = 5

View File

@@ -1,16 +0,0 @@
# -*- coding: utf-8 -*-
class AnixartInitError(Exception):
pass
class AnixartAuthError(Exception):
pass
class AnixartAPIRequestError(Exception):
pass
class AnixartAPIError(Exception):
pass

15
anixart/exceptions.py Normal file
View File

@@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
class AnixartBasError(Exception): ...
# Init errors
class AnixartInitError(AnixartBasError, TypeError): ...
# API errors
class AnixartAPIError(AnixartBasError): ...
class AnixartAuthError(AnixartAPIError): ...
class AnixartAPIRequestError(AnixartAPIError): ...

View File

@@ -4,9 +4,9 @@ import logging
import requests
from .__version__ import __version__, __build__
from .__meta__ import __version__, __build__
from .endpoints import API_URL
from .errors import AnixartAPIRequestError, AnixartAPIError
from .exceptions import AnixartAPIRequestError, AnixartAPIError
_log_name = "file:%-28s -> %s" % ("<Anixart.request_handler:%-3i>", "%s")

View File

@@ -1,58 +0,0 @@
# -*- coding: utf-8 -*-
import os
import sys
from setuptools import setup
here = os.path.abspath(os.path.dirname(__file__))
packages = ['anixart']
requires = ['requests']
# 'setup.py publish' shortcut.
if sys.argv[-1] == 'publish':
os.system('py -m build')
os.system('py -m twine upload --repository testpypi dist/*')
os.system('py -m twine upload --repository pypi dist/*')
sys.exit()
about = {}
with open(os.path.join(here, 'anixart', '__version__.py'), 'r', encoding='utf-8') as f:
exec(f.read(), about)
with open('README.md', 'r', encoding='utf-8') as f:
readme = f.read()
setup(
name=about['__title__'],
version=about['__version__'],
description=about['__description__'],
long_description=readme,
long_description_content_type='text/markdown',
author=about['__author__'],
author_email=about['__author_email__'],
url=about['__url__'],
packages=packages,
package_data={'': ['LICENSE']},
package_dir={'anixart': 'anixart'},
include_package_data=True,
install_requires=requires,
license=about['__license__'],
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"Natural Language :: Russian",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"License :: Other/Proprietary License",
"Operating System :: OS Independent",
],
project_urls={
'Documentation': 'https://anixart.readthedocs.io/',
'Source': 'https://github.com/SantaSpeen/anixart',
},
python_requires=">=3.7",
)