WIP: Client change
This commit is contained in:
@@ -1,10 +1,19 @@
|
|||||||
from os import path, remove, sep
|
from os import path, remove, sep
|
||||||
from shutil import rmtree
|
from shutil import rmtree
|
||||||
from typing import Tuple, Union
|
from typing import Tuple, Union
|
||||||
|
from photosapi_client.api.default.photo_upload_albums_album_photos_post import (
|
||||||
|
asyncio as upload_pic,
|
||||||
|
)
|
||||||
|
from photosapi_client.models.body_photo_upload_albums_album_photos_post import (
|
||||||
|
BodyPhotoUploadAlbumsAlbumPhotosPost,
|
||||||
|
)
|
||||||
|
from photosapi_client.types import File
|
||||||
|
from photosapi_client.models.http_validation_error import HTTPValidationError
|
||||||
|
import aiofiles
|
||||||
from pyrogram.client import Client
|
from pyrogram.client import Client
|
||||||
from pyrogram.types import Message
|
from pyrogram.types import Message
|
||||||
from classes.exceptions import SubmissionDuplicatesError, SubmissionUnavailableError
|
from classes.exceptions import SubmissionDuplicatesError, SubmissionUnavailableError
|
||||||
from modules.api_client import upload_pic
|
from modules.api_client import client
|
||||||
from modules.database import col_submitted
|
from modules.database import col_submitted
|
||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
from modules.logger import logWrite
|
from modules.logger import logWrite
|
||||||
@@ -61,12 +70,26 @@ class PosterClient(Client):
|
|||||||
except:
|
except:
|
||||||
raise SubmissionUnavailableError()
|
raise SubmissionUnavailableError()
|
||||||
|
|
||||||
|
logWrite(f"Uploading {path.basename(filepath)} to the API...", debug=True)
|
||||||
|
|
||||||
|
async with aiofiles.open(filepath, "rb") as f:
|
||||||
|
file_bytes = await f.read()
|
||||||
|
|
||||||
response = await upload_pic(
|
response = await upload_pic(
|
||||||
str(filepath), allow_duplicates=configGet("allow_duplicates", "submission")
|
album=configGet("album", "posting", "api"),
|
||||||
|
client=client,
|
||||||
|
multipart_data=BodyPhotoUploadAlbumsAlbumPhotosPost(
|
||||||
|
File(file_bytes, path.basename(filepath), "image/jpeg")
|
||||||
|
),
|
||||||
|
ignore_duplicates=configGet("allow_duplicates", "submission"),
|
||||||
|
caption="queue",
|
||||||
|
compress=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(response[1]) > 0:
|
if isinstance(response, HTTPValidationError) > 0:
|
||||||
raise SubmissionDuplicatesError(str(filepath), response[1])
|
raise SubmissionDuplicatesError(
|
||||||
|
str(filepath), response.to_dict()["duplicates"]
|
||||||
|
)
|
||||||
|
|
||||||
col_submitted.find_one_and_update(
|
col_submitted.find_one_and_update(
|
||||||
{"_id": ObjectId(id)}, {"$set": {"done": True}}
|
{"_id": ObjectId(id)}, {"$set": {"done": True}}
|
||||||
|
@@ -1,23 +1,27 @@
|
|||||||
"""This is only a temporary solution. Complete Photos API client is yet to be developed."""
|
"""This is only a temporary solution. Complete Photos API client is yet to be developed."""
|
||||||
|
|
||||||
import asyncio
|
from photosapi_client import AuthenticatedClient
|
||||||
|
|
||||||
|
# import asyncio
|
||||||
from base64 import b64decode, b64encode
|
from base64 import b64decode, b64encode
|
||||||
from os import makedirs, path, sep
|
from os import makedirs, path, sep
|
||||||
from random import choice
|
|
||||||
from traceback import print_exc
|
# from random import choice
|
||||||
from typing import Tuple, Union
|
# from traceback import print_exc
|
||||||
|
# from typing import Tuple, Union
|
||||||
|
|
||||||
import aiofiles
|
import aiofiles
|
||||||
from aiohttp import FormData
|
|
||||||
|
|
||||||
from classes.exceptions import (
|
# from aiohttp import FormData
|
||||||
AlbumCreationDuplicateError,
|
|
||||||
AlbumCreationError,
|
# from classes.exceptions import (
|
||||||
AlbumCreationNameError,
|
# AlbumCreationDuplicateError,
|
||||||
SubmissionUploadError,
|
# AlbumCreationError,
|
||||||
UserCreationDuplicateError,
|
# AlbumCreationNameError,
|
||||||
UserCreationError,
|
# SubmissionUploadError,
|
||||||
)
|
# UserCreationDuplicateError,
|
||||||
|
# UserCreationError,
|
||||||
|
# )
|
||||||
from modules.logger import logWrite
|
from modules.logger import logWrite
|
||||||
from modules.utils import configGet, locale
|
from modules.utils import configGet, locale
|
||||||
from modules.http_client import http_session
|
from modules.http_client import http_session
|
||||||
@@ -68,196 +72,200 @@ async def authorize() -> str:
|
|||||||
return (await response.json())["access_token"]
|
return (await response.json())["access_token"]
|
||||||
|
|
||||||
|
|
||||||
async def random_pic(token: Union[str, None] = None) -> Tuple[str, str]:
|
client = AuthenticatedClient(
|
||||||
"""Returns random image id and filename from the queue.
|
base_url=configGet("address", "posting", "api"), token=await authorize()
|
||||||
|
)
|
||||||
|
|
||||||
### Returns:
|
# async def random_pic(token: Union[str, None] = None) -> Tuple[str, str]:
|
||||||
* `Tuple[str, str]`: First value is an ID and the filename in the filesystem to be indexed.
|
# """Returns random image id and filename from the queue.
|
||||||
"""
|
|
||||||
token = await authorize() if token is None else token
|
# ### Returns:
|
||||||
logWrite(
|
# * `Tuple[str, str]`: First value is an ID and the filename in the filesystem to be indexed.
|
||||||
f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos?q=&page_size={configGet("page_size", "posting")}&caption=queue'
|
# """
|
||||||
)
|
# token = await authorize() if token is None else token
|
||||||
resp = await http_session.get(
|
# logWrite(
|
||||||
f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos?q=&page_size={configGet("page_size", "posting")}&caption=queue',
|
# f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos?q=&page_size={configGet("page_size", "posting")}&caption=queue'
|
||||||
headers={"Authorization": f"Bearer {token}"},
|
# )
|
||||||
)
|
# resp = await http_session.get(
|
||||||
logWrite(
|
# f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos?q=&page_size={configGet("page_size", "posting")}&caption=queue',
|
||||||
locale("random_pic_response", "console", locale=configGet("locale_log")).format(
|
# headers={"Authorization": f"Bearer {token}"},
|
||||||
await resp.json()
|
# )
|
||||||
),
|
# logWrite(
|
||||||
debug=True,
|
# locale("random_pic_response", "console", locale=configGet("locale_log")).format(
|
||||||
)
|
# await resp.json()
|
||||||
if resp.status != 200:
|
# ),
|
||||||
logWrite(
|
# debug=True,
|
||||||
locale(
|
# )
|
||||||
"random_pic_error_code",
|
# if resp.status != 200:
|
||||||
"console",
|
# logWrite(
|
||||||
locale=configGet("locale_log").format(
|
# locale(
|
||||||
configGet("album", "posting", "api"), resp.status
|
# "random_pic_error_code",
|
||||||
),
|
# "console",
|
||||||
),
|
# locale=configGet("locale_log").format(
|
||||||
)
|
# configGet("album", "posting", "api"), resp.status
|
||||||
logWrite(
|
# ),
|
||||||
locale(
|
# ),
|
||||||
"random_pic_error_debug",
|
# )
|
||||||
"console",
|
# logWrite(
|
||||||
locale=configGet("locale_log").format(
|
# locale(
|
||||||
configGet("address", "posting", "api"),
|
# "random_pic_error_debug",
|
||||||
configGet("album", "posting", "api"),
|
# "console",
|
||||||
configGet("page_size", "posting"),
|
# locale=configGet("locale_log").format(
|
||||||
token,
|
# configGet("address", "posting", "api"),
|
||||||
resp.status,
|
# configGet("album", "posting", "api"),
|
||||||
),
|
# configGet("page_size", "posting"),
|
||||||
),
|
# token,
|
||||||
debug=True,
|
# resp.status,
|
||||||
)
|
# ),
|
||||||
raise ValueError
|
# ),
|
||||||
if len((await resp.json())["results"]) == 0:
|
# debug=True,
|
||||||
raise KeyError
|
# )
|
||||||
pic = choice((await resp.json())["results"])
|
# raise ValueError
|
||||||
return pic["id"], pic["filename"]
|
# if len((await resp.json())["results"]) == 0:
|
||||||
|
# raise KeyError
|
||||||
|
# pic = choice((await resp.json())["results"])
|
||||||
|
# return pic["id"], pic["filename"]
|
||||||
|
|
||||||
|
|
||||||
async def upload_pic(
|
# async def upload_pic(
|
||||||
filepath: str, allow_duplicates: bool = False, token: Union[str, None] = None
|
# filepath: str, allow_duplicates: bool = False, token: Union[str, None] = None
|
||||||
) -> Tuple[bool, list, Union[str, None]]:
|
# ) -> Tuple[bool, list, Union[str, None]]:
|
||||||
token = await authorize() if token is None else token
|
# token = await authorize() if token is None else token
|
||||||
try:
|
# try:
|
||||||
pic_name = path.basename(filepath)
|
# pic_name = path.basename(filepath)
|
||||||
logWrite(f"Uploading {pic_name} to the API...", debug=True)
|
# logWrite(f"Uploading {pic_name} to the API...", debug=True)
|
||||||
async with aiofiles.open(filepath, "rb") as f:
|
# async with aiofiles.open(filepath, "rb") as f:
|
||||||
file_bytes = await f.read()
|
# file_bytes = await f.read()
|
||||||
formdata = FormData()
|
# formdata = FormData()
|
||||||
formdata.add_field(
|
# formdata.add_field(
|
||||||
"file", file_bytes, filename=pic_name, content_type="image/jpeg"
|
# "file", file_bytes, filename=pic_name, content_type="image/jpeg"
|
||||||
)
|
# )
|
||||||
response = await http_session.post(
|
# response = await http_session.post(
|
||||||
f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos',
|
# f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos',
|
||||||
params={
|
# params={
|
||||||
"caption": "queue",
|
# "caption": "queue",
|
||||||
"compress": "false",
|
# "compress": "false",
|
||||||
"ignore_duplicates": str(allow_duplicates).lower(),
|
# "ignore_duplicates": str(allow_duplicates).lower(),
|
||||||
},
|
# },
|
||||||
headers={"Authorization": f"Bearer {token}"},
|
# headers={"Authorization": f"Bearer {token}"},
|
||||||
data=formdata,
|
# data=formdata,
|
||||||
)
|
# )
|
||||||
response_json = await response.json()
|
# response_json = await response.json()
|
||||||
if response.status != 200 and response.status != 409:
|
# if response.status != 200 and response.status != 409:
|
||||||
logWrite(
|
# logWrite(
|
||||||
locale(
|
# locale(
|
||||||
"pic_upload_error",
|
# "pic_upload_error",
|
||||||
"console",
|
# "console",
|
||||||
locale=configGet("locale_log").format(
|
# locale=configGet("locale_log").format(
|
||||||
filepath, response.status, response.content
|
# filepath, response.status, response.content
|
||||||
),
|
# ),
|
||||||
),
|
# ),
|
||||||
)
|
# )
|
||||||
raise SubmissionUploadError(
|
# raise SubmissionUploadError(
|
||||||
str(filepath), response.status, response.content
|
# str(filepath), response.status, response.content
|
||||||
)
|
# )
|
||||||
id = response_json["id"] if "id" in await response.json() else None
|
# id = response_json["id"] if "id" in await response.json() else None
|
||||||
duplicates = []
|
# duplicates = []
|
||||||
if "duplicates" in response_json:
|
# if "duplicates" in response_json:
|
||||||
for index, duplicate in enumerate(response_json["duplicates"]): # type: ignore
|
# for index, duplicate in enumerate(response_json["duplicates"]): # type: ignore
|
||||||
if response_json["access_token"] is None:
|
# if response_json["access_token"] is None:
|
||||||
duplicates.append(
|
# duplicates.append(
|
||||||
f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/photos/{duplicate["id"]}'
|
# f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/photos/{duplicate["id"]}'
|
||||||
)
|
# )
|
||||||
else:
|
# else:
|
||||||
duplicates.append(
|
# duplicates.append(
|
||||||
f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/token/photo/{response_json["access_token"]}?id={index}'
|
# f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/token/photo/{response_json["access_token"]}?id={index}'
|
||||||
)
|
# )
|
||||||
return True, duplicates, id
|
# return True, duplicates, id
|
||||||
except Exception as exp:
|
# except Exception as exp:
|
||||||
print_exc()
|
# print_exc()
|
||||||
return False, [], None
|
# return False, [], None
|
||||||
|
|
||||||
|
|
||||||
async def find_pic(
|
# async def find_pic(
|
||||||
name: str, caption: Union[str, None] = None, token: Union[str, None] = None
|
# name: str, caption: Union[str, None] = None, token: Union[str, None] = None
|
||||||
) -> Union[dict, None]:
|
# ) -> Union[dict, None]:
|
||||||
token = await authorize() if token is None else token
|
# token = await authorize() if token is None else token
|
||||||
try:
|
# try:
|
||||||
response = await http_session.get(
|
# response = await http_session.get(
|
||||||
f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos',
|
# f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos',
|
||||||
params={"q": name, "caption": caption},
|
# params={"q": name, "caption": caption},
|
||||||
headers={"Authorization": f"Bearer {token}"},
|
# headers={"Authorization": f"Bearer {token}"},
|
||||||
)
|
# )
|
||||||
# logWrite(response.json())
|
# # logWrite(response.json())
|
||||||
if response.status != 200:
|
# if response.status != 200:
|
||||||
return None
|
# return None
|
||||||
if len((await response.json())["results"]) == 0:
|
# if len((await response.json())["results"]) == 0:
|
||||||
return None
|
# return None
|
||||||
return (await response.json())["results"]
|
# return (await response.json())["results"]
|
||||||
except Exception as exp:
|
# except Exception as exp:
|
||||||
logWrite(
|
# logWrite(
|
||||||
locale(
|
# locale(
|
||||||
"find_pic_error",
|
# "find_pic_error",
|
||||||
"console",
|
# "console",
|
||||||
locale=configGet("locale_log").format(name, caption, exp),
|
# locale=configGet("locale_log").format(name, caption, exp),
|
||||||
),
|
# ),
|
||||||
)
|
# )
|
||||||
return None
|
# return None
|
||||||
|
|
||||||
|
|
||||||
async def move_pic(id: str, token: Union[str, None] = None) -> bool:
|
# async def move_pic(id: str, token: Union[str, None] = None) -> bool:
|
||||||
token = await authorize() if token is None else token
|
# token = await authorize() if token is None else token
|
||||||
try:
|
# try:
|
||||||
response = await http_session.patch(
|
# response = await http_session.patch(
|
||||||
f'{configGet("address", "posting", "api")}/photos/{id}?caption=sent',
|
# f'{configGet("address", "posting", "api")}/photos/{id}?caption=sent',
|
||||||
headers={"Authorization": f"Bearer {token}"},
|
# headers={"Authorization": f"Bearer {token}"},
|
||||||
)
|
# )
|
||||||
if response.status != 200:
|
# if response.status != 200:
|
||||||
logWrite(f"Media moving failed with HTTP {response.status}", debug=True)
|
# logWrite(f"Media moving failed with HTTP {response.status}", debug=True)
|
||||||
return False
|
# return False
|
||||||
return True
|
# return True
|
||||||
except:
|
# except:
|
||||||
return False
|
# return False
|
||||||
|
|
||||||
|
|
||||||
async def remove_pic(id: str, token: Union[str, None] = None) -> bool:
|
# async def remove_pic(id: str, token: Union[str, None] = None) -> bool:
|
||||||
token = await authorize() if token is None else token
|
# token = await authorize() if token is None else token
|
||||||
try:
|
# try:
|
||||||
response = await http_session.delete(
|
# response = await http_session.delete(
|
||||||
f'{configGet("address", "posting", "api")}/photos/{id}',
|
# f'{configGet("address", "posting", "api")}/photos/{id}',
|
||||||
headers={"Authorization": f"Bearer {token}"},
|
# headers={"Authorization": f"Bearer {token}"},
|
||||||
)
|
# )
|
||||||
if response.status != 204:
|
# if response.status != 204:
|
||||||
logWrite(f"Media removal failed with HTTP {response.status}", debug=True)
|
# logWrite(f"Media removal failed with HTTP {response.status}", debug=True)
|
||||||
return False
|
# return False
|
||||||
return True
|
# return True
|
||||||
except:
|
# except:
|
||||||
return False
|
# return False
|
||||||
|
|
||||||
|
|
||||||
async def create_user(username: str, email: str, password: str) -> None:
|
# async def create_user(username: str, email: str, password: str) -> None:
|
||||||
response = await http_session.post(
|
# response = await http_session.post(
|
||||||
f'{configGet("address", "posting", "api")}/users',
|
# f'{configGet("address", "posting", "api")}/users',
|
||||||
data={"user": username, "email": email, "password": password},
|
# data={"user": username, "email": email, "password": password},
|
||||||
)
|
# )
|
||||||
if response.status == 409:
|
# if response.status == 409:
|
||||||
raise UserCreationDuplicateError(username)
|
# raise UserCreationDuplicateError(username)
|
||||||
elif response.status != 204:
|
# elif response.status != 204:
|
||||||
raise UserCreationError(response.status, await response.text(encoding="utf-8"))
|
# raise UserCreationError(response.status, await response.text(encoding="utf-8"))
|
||||||
return None
|
# return None
|
||||||
|
|
||||||
|
|
||||||
async def create_album(name: str, title: str) -> None:
|
# async def create_album(name: str, title: str) -> None:
|
||||||
token = await authorize()
|
# token = await authorize()
|
||||||
response = await http_session.post(
|
# response = await http_session.post(
|
||||||
f'{configGet("address", "posting", "api")}/albums',
|
# f'{configGet("address", "posting", "api")}/albums',
|
||||||
params={"name": name, "title": title},
|
# params={"name": name, "title": title},
|
||||||
headers={"Authorization": f"Bearer {token}"},
|
# headers={"Authorization": f"Bearer {token}"},
|
||||||
)
|
# )
|
||||||
if response.status == 409:
|
# if response.status == 409:
|
||||||
raise AlbumCreationDuplicateError(name)
|
# raise AlbumCreationDuplicateError(name)
|
||||||
elif response.status == 406:
|
# elif response.status == 406:
|
||||||
raise AlbumCreationNameError(await response.json())
|
# raise AlbumCreationNameError(await response.json())
|
||||||
elif response.status != 200:
|
# elif response.status != 200:
|
||||||
raise AlbumCreationError(response.status, await response.text(encoding="utf-8"))
|
# raise AlbumCreationError(response.status, await response.text(encoding="utf-8"))
|
||||||
return None
|
# return None
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
# if __name__ == "__main__":
|
||||||
print(asyncio.run(authorize()))
|
# print(asyncio.run(authorize()))
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
photosapi_client==0.1.1
|
||||||
python_dateutil==2.8.2
|
python_dateutil==2.8.2
|
||||||
apscheduler~=3.10.1
|
apscheduler~=3.10.1
|
||||||
pytimeparse~=1.1.8
|
pytimeparse~=1.1.8
|
||||||
@@ -5,7 +6,7 @@ convopyro==0.5
|
|||||||
pyrogram~=2.0.102
|
pyrogram~=2.0.102
|
||||||
aiofiles~=23.1.0
|
aiofiles~=23.1.0
|
||||||
aiohttp~=3.8.4
|
aiohttp~=3.8.4
|
||||||
psutil~=5.9.4
|
|
||||||
pymongo~=4.3.3
|
pymongo~=4.3.3
|
||||||
|
psutil~=5.9.4
|
||||||
pillow~=9.4.0
|
pillow~=9.4.0
|
||||||
ujson~=5.7.0
|
ujson~=5.7.0
|
Reference in New Issue
Block a user