forked from LiveCarta/BookConverter
Annotations in Epub converter
This commit is contained in:
@@ -1,17 +1,23 @@
|
||||
import json
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
import requests
|
||||
from threading import Event
|
||||
from io import BytesIO
|
||||
from threading import Event
|
||||
from typing import List, Tuple, Dict, Union
|
||||
|
||||
|
||||
class Access:
|
||||
"""Class accessing our platform"""
|
||||
def __init__(self, url=None):
|
||||
def __init__(self, url: str = None):
|
||||
"""
|
||||
:param url: str, url received from queue message, if field apiURL exists
|
||||
else None
|
||||
Parameters
|
||||
----------
|
||||
url: str
|
||||
url received from queue message,
|
||||
if field apiURL exists
|
||||
else None
|
||||
|
||||
"""
|
||||
self.PENDING = 1
|
||||
self.PROCESS = 2
|
||||
@@ -19,6 +25,7 @@ class Access:
|
||||
self.FINISH = 4
|
||||
self.ERROR = 5
|
||||
|
||||
self.url = None
|
||||
self.username = None
|
||||
self.password = None
|
||||
|
||||
@@ -32,12 +39,12 @@ class Access:
|
||||
self.get_token()
|
||||
self.refreshing.set()
|
||||
|
||||
def set_credentials(self, url):
|
||||
folder_path = os.path.dirname(
|
||||
def set_credentials(self, url: str):
|
||||
folder_path: str = os.path.dirname(
|
||||
os.path.dirname(os.path.abspath(__file__)))
|
||||
config_path = os.path.join(folder_path, "config/api_config.json")
|
||||
config_path: str = os.path.join(folder_path, "config/api_config.json")
|
||||
with open(config_path, "r") as f:
|
||||
params = json.load(f)
|
||||
params: Dict[str, str] = json.load(f)
|
||||
|
||||
self.refreshing.clear()
|
||||
self.url = url
|
||||
@@ -64,7 +71,7 @@ class Access:
|
||||
}
|
||||
response = requests.post(
|
||||
f'{self.url}/token', json=json_form,
|
||||
# auth=('kiryl.miatselitsa', 'iK4yXCvdyHFEEOvG2v3F')
|
||||
# auth=('kiryl.miatselitsa', 'iK4yXCvdyHFEEOvG2v3F')
|
||||
)
|
||||
|
||||
if response.status_code == 400:
|
||||
@@ -104,7 +111,7 @@ class Access:
|
||||
else:
|
||||
raise Exception(f'{response.status_code}')
|
||||
|
||||
def get_file(self, file_path):
|
||||
def get_file(self, file_path: str) -> bytes:
|
||||
"""Function downloads the file[book, preset] from site"""
|
||||
if self.is_time_for_refreshing():
|
||||
self.refresh_token()
|
||||
@@ -124,10 +131,11 @@ class Access:
|
||||
f'status code:{response.status_code}')
|
||||
return content
|
||||
|
||||
def sleep(timeout: float, retry=3):
|
||||
@staticmethod
|
||||
def sleep(timeout: float, retry: int = 3):
|
||||
def decorator(function):
|
||||
"""Decorator sleeping timeout sec and makes 3 retries"""
|
||||
def wrapper(*args, **kwargs):
|
||||
def wrapper(*args, **kwargs) -> str:
|
||||
retries = 0
|
||||
while retries < retry:
|
||||
try:
|
||||
@@ -141,14 +149,14 @@ class Access:
|
||||
return decorator
|
||||
|
||||
@sleep(3)
|
||||
def send_image(self, img_path, doc_id, img_content: bytes = None):
|
||||
def send_image(self, img_path: str, doc_id: str, img_content: bytes = None) -> str:
|
||||
"""Function sends images to site"""
|
||||
if self.is_time_for_refreshing():
|
||||
self.refresh_token()
|
||||
self.refreshing.wait()
|
||||
|
||||
img_obj = BytesIO(img_content) if img_content else open(img_path, 'rb')
|
||||
files = {
|
||||
img_obj: BytesIO = BytesIO(img_content) if img_content else open(img_path, 'rb')
|
||||
files: Dict[str, Tuple[str, BytesIO]] = {
|
||||
'image': (os.path.basename(img_path), img_obj)
|
||||
}
|
||||
response = requests.post(
|
||||
@@ -165,7 +173,7 @@ class Access:
|
||||
f'{response.status_code} Bad request: {response.json()["message"]}.')
|
||||
return img_url
|
||||
|
||||
def send_book(self, doc_id, content):
|
||||
def send_book(self, doc_id: int, content: Dict[str, List[Dict[str, Union[List, str]]]]):
|
||||
"""Function sends the book to site"""
|
||||
if self.is_time_for_refreshing():
|
||||
self.refresh_token()
|
||||
@@ -184,7 +192,7 @@ class Access:
|
||||
raise Exception(
|
||||
f'{response.status_code} Bad request: {response.json()["message"]}.')
|
||||
|
||||
def update_status(self, doc_id, status):
|
||||
def update_status(self, doc_id: Union[int, str], status: int):
|
||||
"""Function updates status of the book on site"""
|
||||
if self.is_time_for_refreshing():
|
||||
self.refresh_token()
|
||||
|
||||
Reference in New Issue
Block a user