creates file

This commit is contained in:
Dima YaFlay
2023-07-28 04:07:45 +03:00
commit 7341c1b768
5 changed files with 109 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
__pycache__

87
boosty-py/API.py Normal file
View File

@@ -0,0 +1,87 @@
import aiohttp
from utils import target
class API:
URI = "https://api.boosty.to/v1"
async def init(self, token: str, cookie: str, user: str):
"""Standart API class.
Args:
token (str): User`s token, getting from Authorization row in F11 Developer menu -> Network -> /api -> connect
cookie (str): Like token, but a little below in row Cookie
"""
self.bearer = token
self.cookie = cookie
self.header = {
"Authorization": f"Bearer {token}"
}
self.user = user
self.session = aiohttp.ClientSession(headers=self.header,cookie=self.cookie) # creates session
async with self.session.get(f"{self.URI}/ws/connect") as resp: # gets token
if resp.status == 200:
self.token = await resp.json()["token"]
else:
raise Exception(
f"Error! API not worked... Status code: {resp.status}"
)
async def __get(self, url, *, to_json: bool = True, **args) -> dict | aiohttp.ClientResponse:
async with self.session.get(f"{self.URI}{url}", kwargs=args) as resp:
if resp.status == 200:
if to_json:
return await resp.json()
else:
return resp
else:
raise aiohttp.ServerConnectionError(f"Error in __get function. Status: {resp.status}")
async def __post(self, url, *, data, to_json: bool = True, **kwargs) -> dict | aiohttp.ClientResponse:
async with self.session.post(f"{self.URI}{url}", data=data, kwargs=kwargs) as resp:
if resp.status == 200:
if to_json:
return await resp.json()
else:
return resp
else:
raise aiohttp.ServerConnectionError(f"Error in __get function. Status: {resp.status}")
async def get_target(self, post_name: str) -> target.target | list[target.target]:
"""Return target on specific post or list of target
Args:
post_name (str): Name of post`s
Returns:
target.target | list[target.target]: target or targets to money or any
"""
json = await self.__get(f"/target/{post_name}/?", to_json=True)
if len(json.get("data")) == 0:
return "Not Found"
elif len(json.get("data")) == 1:
return target.target(json.get("data"))
else:
target_list: list[target.target] = []
for i in json.get("data"):
target_list.append(target.target(i))
return target_list
async def get_post(self, post_id: str, post_author: str) -> dict:
return await self.__get(f"/blog/{post_author}/post/{post_id}/")
async def get_user(self, user: str) -> dict:
"""Get ur user
Args:
user (str): ur nickname
Returns:
dict: all data with ur user
"""
self.user = user
return await self.__get(f"/blog/{user}")
@property
async def events(self) -> dict:
"""Return all events
Returns:
dict: dict with all events
"""
return await self.__get(f"/notification/standalone/event/")

View File

@@ -0,0 +1 @@
from . import *

7
boosty-py/utils/post.py Normal file
View File

@@ -0,0 +1,7 @@
class post:
def __init__(self, json):
"""Post!
"""
self.json = json

13
boosty-py/utils/target.py Normal file
View File

@@ -0,0 +1,13 @@
class target:
def __init__(self, json):
self.json = json
if json.get("data"):
self.json = json.get("data")
self.type: str = self.json[0].get("type")
self.blogger_id: str = self.json[0].get("bloggerUrl")
self.created_at: int = self.json[0].get("createdAt")
self.currentSum: int = self.json[0].get("currentSum")
self.targetSum: int = self.json[0].get("targetSum")
self.description: str = self.json[0].get("description")