Spaces:
Running
Running
File size: 4,960 Bytes
4051191 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import os
from abc import abstractmethod, ABC
from dataclasses import dataclass
from typing import List, AsyncIterable
from aiohttp import ClientSession
from pathlib import Path
from models import LastChapter
from tools import LanguageSingleton
@dataclass
class MangaCard:
client: "MangaClient"
name: str
url: str
picture_url: str
def get_url(self):
return self.url
def unique(self):
return str(hash(self.url))
@dataclass
class MangaChapter:
client: "MangaClient"
name: str
url: str
manga: MangaCard
pictures: List[str]
def get_url(self):
return self.url
def unique(self):
return str(hash(self.url))
def clean(name, length=-1):
while ' ' in name:
name = name.replace(' ', ' ')
name = name.replace(':', '')
if length != -1:
name = name[:length]
return name
class MangaClient(ClientSession, metaclass=LanguageSingleton):
def __init__(self, *args, name="client", **kwargs):
if name == "client":
raise NotImplementedError
super().__init__(*args, **kwargs)
self.name = name
async def get_url(self, url, *args, file_name=None, cache=False, req_content=True, method='get', data=None,
**kwargs):
def response():
pass
response.status = "200"
if cache:
path = Path(f'cache/{self.name}/{file_name}')
os.makedirs(path.parent, exist_ok=True)
try:
with open(path, 'rb') as f:
content = f.read()
except FileNotFoundError:
if method == 'get':
response = await self.get(url, *args, **kwargs)
elif method == 'post':
response = await self.post(url, data=data or {}, **kwargs)
else:
raise ValueError
if str(response.status).startswith('2'):
content = await response.read()
with open(path, 'wb') as f:
f.write(content)
else:
if method == 'get':
response = await self.get(url, *args, **kwargs)
elif method == 'post':
response = await self.post(url, data=data or {}, **kwargs)
else:
raise ValueError
content = await response.read()
if req_content:
return content
else:
return response
async def set_pictures(self, manga_chapter: MangaChapter):
requests_url = manga_chapter.url
# Set manga url as the referer if there is one
headers = {**self.headers}
if manga_chapter.manga:
headers['referer'] = manga_chapter.manga.url
response = await self.get(requests_url, headers=headers)
content = await response.read()
manga_chapter.pictures = await self.pictures_from_chapters(content, response)
return manga_chapter
async def download_pictures(self, manga_chapter: MangaChapter):
if not manga_chapter.pictures:
await self.set_pictures(manga_chapter)
folder_name = f'{clean(manga_chapter.manga.name)}/{clean(manga_chapter.name)}'
i = 0
for picture in manga_chapter.pictures:
ext = picture.split('.')[-1].split('?')[0].lower()
file_name = f'{folder_name}/{format(i, "05d")}.{ext}'
for _ in range(3):
req = await self.get_picture(manga_chapter, picture, file_name=file_name, cache=True,
req_content=False)
if str(req.status).startswith('2'):
break
else:
raise ValueError
i += 1
return Path(f'cache/{manga_chapter.client.name}') / folder_name
async def get_picture(self, manga_chapter: MangaChapter, url, *args, **kwargs):
return await self.get_url(url, *args, **kwargs)
async def get_cover(self, manga_card: MangaCard, *args, **kwargs):
return await self.get_url(manga_card.picture_url, *args, **kwargs)
async def check_updated_urls(self, last_chapters: List[LastChapter]):
return [lc.url for lc in last_chapters], []
@abstractmethod
async def search(self, query: str = "", page: int = 1) -> List[MangaCard]:
raise NotImplementedError
@abstractmethod
async def get_chapters(self, manga_card: MangaCard, page: int = 1) -> List[MangaChapter]:
raise NotImplementedError
@abstractmethod
async def contains_url(self, url: str):
raise NotImplementedError
@abstractmethod
async def iter_chapters(self, manga_url: str, manga_name: str) -> AsyncIterable[MangaChapter]:
raise NotImplementedError
@abstractmethod
async def pictures_from_chapters(self, content: bytes, response=None):
raise NotImplementedError
|