hyperboria/nexus/pylon/sources/libgen_md5.py
the-superpirate 9ce67ec590 - feat(pylon): Refactor code
- feat(idm): Rename IDM-2 to IDM
  - feat(idm): Open IDM
3 internal commit(s)

GitOrigin-RevId: e302e9b5cda18cca1adc4ae8a3d906714d222106
2021-04-12 23:38:54 +03:00

44 lines
1.3 KiB
Python

import re
from typing import (
AsyncIterable,
Callable,
)
from library.logging import error_log
from .base import (
Md5Source,
PreparedRequest,
)
class LibgenMd5Source(Md5Source):
base_url = 'http://libgen.gs'
resolve_timeout = 10
async def resolve_lg(self, session, url):
async with PreparedRequest(
method='get',
url=url,
timeout=self.resolve_timeout
).execute_with(session=session) as resp:
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:
return PreparedRequest(method='get', url=match.group(), timeout=self.timeout)
async def resolve(self, error_log_func: Callable = error_log) -> AsyncIterable[PreparedRequest]:
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