Created
July 8, 2025 03:08
-
-
Save zone559/56812b118c7f332cd84ad64df0104e8c to your computer and use it in GitHub Desktop.
Summary of Changes (SQLite → Plain Text for Personal Use): Removed SQLite/PostgreSQL – Replaced database backend with a simple text file. Simplified Storage – Uses a .txt file with one entry per line (e.g., {category}_{id}). No Thread Safety – Not designed for concurrent access (single-user only). Kept Core Functions – Same add(), check(), and f…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import os | |
| import logging | |
| from . import util, formatter | |
| log = logging.getLogger("archive") | |
| class TextDownloadArchive: | |
| """Plain text download archive implementation for gallery-dl""" | |
| def __init__(self, path, keygen, cache_key=None): | |
| self.keygen = keygen | |
| self._cache_key = cache_key or "_archive_key" | |
| self._entries = set() | |
| self.path = util.expand_path(path) | |
| # Create parent directory if needed | |
| dirname = os.path.dirname(self.path) | |
| if dirname and not os.path.exists(dirname): | |
| os.makedirs(dirname) | |
| # Load existing entries | |
| if os.path.exists(self.path): | |
| with open(self.path, "r", encoding="utf-8") as f: | |
| self._entries.update(line.strip() for line in f if line.strip()) | |
| def add(self, kwdict): | |
| key = kwdict.get(self._cache_key) or self.keygen(kwdict) | |
| self._entries.add(key) | |
| def check(self, kwdict): | |
| key = kwdict[self._cache_key] = self.keygen(kwdict) | |
| return key in self._entries | |
| def finalize(self): | |
| with open(self.path, "w", encoding="utf-8") as f: | |
| for entry in sorted(self._entries): | |
| f.write(f"{entry}\n") | |
| def close(self): | |
| self.finalize() | |
| def connect(path, prefix, format, table=None, mode=None, pragma=None, kwdict=None, cache_key=None): | |
| """ | |
| gallery-dl compatible connect function | |
| Accepts all arguments gallery-dl might pass | |
| """ | |
| keygen = formatter.parse(prefix + format).format_map | |
| # Handle format strings in path | |
| path = util.expand_path(path) | |
| if kwdict is not None and "{" in path: | |
| path = formatter.parse(path).format_map(kwdict) | |
| return TextDownloadArchive(path, keygen, cache_key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment