From c46fe53e5fa21710a7b3aedec28d93dab5c75a99 Mon Sep 17 00:00:00 2001 From: jay817 Date: Fri, 23 Aug 2024 08:29:03 -0400 Subject: [PATCH] Update playwright/README.md --- playwright/README.md | 68 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/playwright/README.md b/playwright/README.md index ca1c28e..31b506d 100644 --- a/playwright/README.md +++ b/playwright/README.md @@ -1,4 +1,5 @@ ```python +# main.py from playwright.async_api import async_playwright as aP from db import DB import xvfbwrapper, io, PIL.Image, os @@ -26,4 +27,69 @@ async def Page(browser='chromium', headless=True): await context.route(block, lambda route: route.abort()) return await context.new_page() -``` \ No newline at end of file +``` + +```python +# db.py +import sqlite3, json + +class DB(sqlite3.Connection): + def __init__(self, db_name="db.sqlite"): + super().__init__(db_name) + with self: + self.execute(''' + CREATE TABLE IF NOT EXISTS kv_store + (key TEXT PRIMARY KEY, value BLOB) + ''') + + def __setitem__(self, key, value): + if not isinstance(value, bytes): value = json.dumps(value) + with self: + cur = self.execute(''' + INSERT OR REPLACE INTO kv_store + (key, value) VALUES (?, ?) + ''', (key, value)) + return {"modified_count": cur.rowcount} + + def __getitem__(self, key): + with self: + result = self.execute(''' + SELECT value FROM kv_store + WHERE key = ? + ''', (key,)).fetchone() + if result: + if isinstance(result[0], str): + try: return json.loads(result[0]) + except json.JSONDecodeError: pass + return result[0] + + def delete(self, key): + with self: + cur = self.execute(''' + DELETE FROM kv_store + WHERE key = ? + ''', (key,)) + return {"deleted_count": cur.rowcount} + + def keys(self, pattern='*'): + pattern = pattern.translate(str.maketrans({ + '\\': '\\\\', '%': '\\%', '_': '\\_', '*': '%', '?': '_' + })) + with self: + result = self.execute(''' + SELECT key FROM kv_store + WHERE key LIKE ? ESCAPE '\\' + ''', (pattern,)).fetchall() + return [row[0] for row in result] + + def __repr__(self): return repr(self.keys()) + + def exists(self, key): + with self: + cur = self.execute(''' + SELECT 1 FROM kv_store + WHERE key = ? + LIMIT 1 + ''', (key,)) + return bool(cur.fetchone()) +```