Skip to content

Commit

Permalink
🐛 Add handler for httpx exception
Browse files Browse the repository at this point in the history
  • Loading branch information
wiseCirno committed Aug 24, 2024
1 parent 68deae3 commit 9556b1b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
27 changes: 11 additions & 16 deletions bot/CoreFunction.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ def format_time(seconds):


class TelegraphHandler:
def __init__(self, proxy: Optional[Proxy] = None, user_id: int = -1):
def __init__(self, proxy: Optional[Proxy] = None, cf_proxy: Optional[str] = None, user_id: int = -1):
self._proxy = proxy
self._cf_proxy = cf_proxy
self._user_id = user_id
self._komga_task_queue = asyncio.Queue()
self._idle_count = 0
Expand All @@ -175,26 +176,20 @@ def __init__(self, proxy: Optional[Proxy] = None, user_id: int = -1):
async def _run_periodically(self):
async def process_queue(queue, num_tasks):
self._idle_count = 0
tasks = [Telegraph(await queue.get(), self._proxy) for _ in range(num_tasks)]
await asyncio.gather(*[task.get_zip() for task in tasks])
tasks = [Telegraph(await queue.get(), self._proxy, self._cf_proxy) for _ in range(num_tasks)]
try:
await asyncio.gather(*[task.get_zip() for task in tasks])
except Exception as exc:
logger.error(exc)

while True:
await asyncio.sleep(60) if self._idle_count >= 20 else None

if not self._komga_task_queue.empty():
queue_size = self._komga_task_queue.qsize()
await asyncio.sleep(60) if self._idle_count >= 20 else await asyncio.sleep(3)

if queue_size == 1:
self._idle_count = 0
instance = Telegraph(await self._komga_task_queue.get(), self._proxy)
await asyncio.create_task(instance.get_zip())
elif 2 <= queue_size <= 9:
await process_queue(self._komga_task_queue, 2)
elif queue_size >= 10:
await process_queue(self._komga_task_queue, 3)
while not self._komga_task_queue.empty():
self._idle_count = 0
await process_queue(self._komga_task_queue, 1 if 1 <= self._komga_task_queue.qsize() <= 4 else 2)

self._idle_count += 1
await asyncio.sleep(3)

async def _get_link(self, content = None):
telegra_ph_links = URLExtract().find_urls(content)
Expand Down
5 changes: 4 additions & 1 deletion src/service/Telegraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ async def get_epub(self) -> str:
raise exc

async def get_zip(self):
return await self._process_handler(is_zip = True)
try:
return await self._process_handler(is_zip = True)
except Exception as exc:
raise exc

async def get_info(self):
return await self._get_info_handler()

0 comments on commit 9556b1b

Please sign in to comment.