Spaces:
Paused
Paused
# -*- coding: utf-8 -*- | |
import os | |
import traceback | |
import requests | |
from modules import untils | |
from modules import g_config | |
system_status = {"error": False, "message": None} | |
class TruyenFull: | |
def __init__(self, manga=None, logger=None): | |
self.manga = manga | |
self.logger = logger | |
self.domain = g_config.DOMAIN_TRUYENFULL | |
self.is_manga_uploaded = True | |
self.post_id = 0 | |
self.chapters_current = self.get_list_chapter() | |
self.total_chapters_current = len(self.chapters_current) | |
def upload_chapter(self, episode, chapter_type="image"): | |
url = f"{self.domain}/wp-admin/admin-ajax.php?action=manga_upload_chapter" | |
if self.post_id == 0: | |
self.logger.error("=" * 20) | |
self.logger.error("TruyenFull - upload_chapter - Post id can't none!") | |
return False | |
try: | |
string_images = "" | |
file = None | |
chapter_content = "" | |
if chapter_type == 'text': | |
chapter_content = episode['content'] | |
if chapter_type == 'image': | |
filename = os.path.join(os.getcwd(), f"resources/{self.manga.slug}/{episode['name']}/archive.zip") | |
if g_config.TYPE_LINK_IMAGE == 'local': | |
file = { | |
'file_0': (filename, open(filename, 'rb'), "multipart/form-data") | |
} | |
string_images = untils.convert_link_images_to_do(self.manga.slug, episode['name'], episode['images']) | |
string_images = ",".join(string_images) | |
payload = { | |
"secret_key": g_config.SECRET_KEY, | |
'post': self.post_id, | |
'postID': self.post_id, | |
'name': episode['name'], | |
'chapterName': episode['name'], | |
'nameExtend': '', | |
'chapterIndex': '', | |
'storage': g_config.TYPE_LINK_IMAGE, | |
'volume': '0', | |
'directlink': 'None', | |
'link_images': string_images, | |
'chapterContent': chapter_content, | |
'chapter_type': chapter_type, | |
} | |
# req = requests.request("POST", url=url, data=payload, files=file, headers=headers) | |
response = requests.request("POST", url, headers=None, data=payload, files=file) | |
except KeyboardInterrupt: | |
raise KeyboardInterrupt | |
except: | |
self.logger.error("=" * 20) | |
self.logger.error("TruyenFull - Error upload_chapter") | |
self.logger.error(traceback.format_exc()) | |
return True | |
def get_list_chapter(self): | |
result = [] | |
url = f"{self.domain}/wp-admin/admin-ajax.php?action=manga_get_list_chapter" | |
headers = None | |
if self.manga is None: | |
return [] | |
try: | |
payload = { | |
"secret_key": g_config.SECRET_KEY, | |
"manga_name": self.manga.name | |
} | |
response = requests.request("POST", url, headers=headers, data=payload) | |
if response.status_code != 200: | |
return [] | |
response = response.json() | |
if response["success"] is False: | |
self.is_manga_uploaded = False | |
return [] | |
for item in response['records']: | |
result.append(item['chapter_name']) | |
return result | |
except KeyboardInterrupt: | |
raise KeyboardInterrupt | |
except: | |
self.logger.error("=" * 20) | |
self.logger.error("TruyenFull - Error get_list_chapter") | |
self.logger.error(traceback.format_exc()) | |
return result | |
def upload_chapters_newest(self, episode, chapter_type="image"): | |
try: | |
if self.manga is None: | |
return False | |
if len(self.manga.episodes) == 0: | |
return False | |
if episode['name'] in self.chapters_current: | |
return False | |
# Upload | |
is_uploaded = self.upload_chapter(episode, chapter_type) | |
if is_uploaded: | |
self.chapters_current.append(episode['name']) | |
return is_uploaded | |
except KeyboardInterrupt: | |
raise KeyboardInterrupt | |
except: | |
self.logger.error("=" * 20) | |
self.logger.error("TruyenFull - Error upload_chapters_newest") | |
self.logger.error(traceback.format_exc()) | |
return False | |
def create_manga(self, chapter_type="image"): | |
url = f"{self.domain}/wp-admin/admin-ajax.php?action=manga_create_manga" | |
headers = None | |
try: | |
thumb = os.path.join(os.getcwd(), f"resources/{self.manga.slug}/thumb.jpg") | |
file = {'featured': (thumb, open(thumb, 'rb'), "multipart/form-data")} | |
payload = { | |
"secret_key": g_config.SECRET_KEY, | |
"manga_name": self.manga.name, | |
"manga_des": self.manga.description, | |
"manga_tags": ",".join(self.manga.tags).strip(), | |
"author": self.manga.author_name, | |
"manga_name_alternative": self.manga.name_alternative, | |
"manga_status": self.manga.status, | |
"chapter_type": chapter_type | |
} | |
response = requests.request("POST", url, headers=headers, data=payload, files=file) | |
try: | |
response = response.json() | |
except requests.exceptions.JSONDecodeError: | |
# self.logger.error("=" * 20) | |
# self.logger.error("TruyenFull - Error create_manga json") | |
return True | |
if "post_id" in response: | |
self.post_id = response["post_id"] | |
except KeyboardInterrupt: | |
raise KeyboardInterrupt | |
except: | |
self.logger.error("=" * 20) | |
self.logger.error("TruyenFull - Error create_manga") | |
self.logger.error(traceback.format_exc()) | |
return False | |
return True | |
def init_post_id(self): | |
url = f"{self.domain}/wp-admin/admin-ajax.php?action=manga_get_post_id" | |
headers = None | |
try: | |
payload = { | |
"secret_key": g_config.SECRET_KEY, | |
"manga_name": self.manga.name | |
} | |
response = requests.request("POST", url, headers=headers, data=payload) | |
try: | |
response = response.json() | |
except requests.exceptions.JSONDecodeError: | |
return | |
if "post_id" in response and int(response["post_id"]) > 0: | |
self.post_id = response["post_id"] | |
except KeyboardInterrupt: | |
raise KeyboardInterrupt | |
except: | |
self.logger.error("=" * 20) | |
self.logger.error("TruyenFull - Error init_post_id") | |
self.logger.error(traceback.format_exc()) | |
def get_list_task_customs(self, domain_target): | |
result = [] | |
# system_status["error"] = True | |
url = f"{self.domain}/wp-admin/admin-ajax.php?action=manga_get_list_task_upload_custom&status=1&paging=all" \ | |
f"&secret_key={g_config.SECRET_KEY}" | |
headers = None | |
try: | |
response = requests.request("GET", url, headers=headers) | |
if response.status_code != 200: | |
return [] | |
response = response.json() | |
if response["success"] is False: | |
return [] | |
for item in response["records"]: | |
for domain in domain_target: | |
if domain in item["link_manga"]: | |
result.append(item) | |
return result | |
except KeyboardInterrupt: | |
raise KeyboardInterrupt | |
except: | |
self.logger.error("=" * 20) | |
self.logger.error("TruyenFull - Error get_list_task_customs") | |
self.logger.error(traceback.format_exc()) | |
return result | |
def update_task_custom(self, payload): | |
url = f"{self.domain}/wp-admin/admin-ajax.php?action=manga_update_task_upload_custom" | |
headers = None | |
try: | |
payload.update({"secret_key": g_config.SECRET_KEY}) | |
response = requests.request("POST", url, headers=headers, data=payload) | |
except KeyboardInterrupt: | |
raise KeyboardInterrupt | |
except: | |
self.logger.error("=" * 20) | |
self.logger.error("TruyenFull - Error update_task_custom") | |
self.logger.error(traceback.format_exc()) | |