2021-01-08 21:08:47 +01:00
|
|
|
import re
|
2021-04-12 19:47:21 +02:00
|
|
|
from typing import (
|
|
|
|
AsyncIterable,
|
|
|
|
Callable,
|
|
|
|
)
|
|
|
|
|
|
|
|
from library.logging import error_log
|
2021-01-08 21:08:47 +01:00
|
|
|
|
|
|
|
from .base import (
|
|
|
|
Md5Source,
|
|
|
|
PreparedRequest,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class LibgenMd5Source(Md5Source):
|
|
|
|
base_url = 'http://libgen.gs'
|
|
|
|
resolve_timeout = 10
|
|
|
|
|
|
|
|
async def resolve_lg(self, session, url):
|
2021-04-12 19:47:21 +02:00
|
|
|
async with PreparedRequest(
|
|
|
|
method='get',
|
|
|
|
url=url,
|
2021-01-08 21:08:47 +01:00
|
|
|
timeout=self.resolve_timeout
|
2021-04-12 19:47:21 +02:00
|
|
|
).execute_with(session=session) as resp:
|
2021-01-08 21:08:47 +01:00
|
|
|
downloaded_page_fiction = await resp.text()
|
|
|
|
match = re.search(
|
|
|
|
'https?://.*/get\\.php\\?md5=.*&key=[A-Za-z0-9]+',
|
|
|
|
downloaded_page_fiction,
|
|
|
|
re.IGNORECASE,
|
|
|
|
)
|
|
|
|
if match:
|
2021-04-12 19:47:21 +02:00
|
|
|
return PreparedRequest(method='get', url=match.group(), timeout=self.timeout)
|
2021-01-08 21:08:47 +01:00
|
|
|
|
2021-04-12 19:47:21 +02:00
|
|
|
async def resolve(self, error_log_func: Callable = error_log) -> AsyncIterable[PreparedRequest]:
|
2021-01-08 21:08:47 +01:00
|
|
|
async with self.get_resolve_session() as session:
|
|
|
|
url = f'{self.base_url}/ads.php?md5={self.md5}'
|
|
|
|
result = await self.resolve_lg(session, url)
|
|
|
|
if result:
|
|
|
|
yield result
|
|
|
|
url = f'{self.base_url}/foreignfiction/ads.php?md5={self.md5}'
|
|
|
|
result = await self.resolve_lg(session, url)
|
|
|
|
if result:
|
|
|
|
yield result
|