diff --git a/jimm.py b/jimm.py index f912d74..507f1cb 100644 --- a/jimm.py +++ b/jimm.py @@ -1,21 +1,3 @@ -def sync(coro): - import asyncio, functools - if not asyncio.iscoroutinefunction(coro): return coro - @functools.wraps(coro) - def wrapper(*args, **kwargs): - loop, future = asyncio.get_event_loop(), asyncio.ensure_future(coro(*args, **kwargs)) - while not future.done(): - loop._process_events(loop._selector.select(0)) - if (ready := loop._ready) and (handle := ready.popleft())._cancelled is False: - task = (tasks := asyncio.tasks._current_tasks).pop(loop, None) - handle._run(); tasks[loop] = task - return future.result() - return wrapper - -for i, c in enumerate('RGYBMC'): globals()[c] = lambda s, i=i: f'\x1b[{91+i}m{s}\x1b[0m' -unsafe = __import__('contextlib').suppress(Exception) -Soup = lambda html: __import__('bs4').BeautifulSoup(html, 'lxml') - def SQL(): import sqlite3, os, hashlib (con := sqlite3.connect('.db', isolation_level=None)).row_factory = sqlite3.Row @@ -43,6 +25,24 @@ def SQL(): 'SELECT k, v FROM kv GROUP BY k HAVING t = MAX(t)'))))() sql = SQL() +for i, c in enumerate('RGYBMC'): globals()[c] = lambda s, i=i: f'\x1b[{91+i}m{s}\x1b[0m' +unsafe = __import__('contextlib').suppress(Exception) +Soup = lambda html: __import__('bs4').BeautifulSoup(html, 'lxml') + +def sync(coro): + import asyncio, functools + if not asyncio.iscoroutinefunction(coro): return coro + @functools.wraps(coro) + def wrapper(*args, **kwargs): + loop, future = asyncio.get_event_loop(), asyncio.ensure_future(coro(*args, **kwargs)) + while not future.done(): + loop._process_events(loop._selector.select(0)) + if (ready := loop._ready) and (handle := ready.popleft())._cancelled is False: + task = (tasks := asyncio.tasks._current_tasks).pop(loop, None) + handle._run(); tasks[loop] = task + return future.result() + return wrapper + @sync async def Page(headless=True): from playwright.async_api import async_playwright @@ -51,8 +51,9 @@ async def Page(headless=True): for attr in dir(page): if callable(method := getattr(page, attr)): setattr(page, attr, sync(method)) async def handle(route): - if route.request.method == 'GET' and (response := await route.fetch()).ok: - sql.put(route.request.url, await response.body()) + with unsafe: + if route.request.method == 'GET' and (response := await route.fetch()).ok: + sql.put(route.request.url, await response.body()) await route.continue_() page.route('**/*', handle)