diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index 48d847d3..14dd8d33 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -181,6 +181,18 @@ def main(): if test: print("Example :", test[0]) print() + elif args.clear_cache: + from . import cache + log = logging.getLogger("cache") + cnt = cache.clear() + + if cnt is None: + log.error("Database file not available") + else: + log.info( + "Deleted %d %s from '%s'", + cnt, "entry" if cnt == 1 else "entries", cache._path(), + ) else: if not args.urls and not args.inputfile: parser.error( diff --git a/gallery_dl/cache.py b/gallery_dl/cache.py index a9416705..e6ba61a1 100644 --- a/gallery_dl/cache.py +++ b/gallery_dl/cache.py @@ -166,19 +166,39 @@ def cache(maxage=3600, keyarg=None): return wrap -try: - path = config.get(("cache", "file"), "") - if path is None: - raise RuntimeError() - elif not path: +def clear(): + """Delete all database entries""" + db = DatabaseCacheDecorator.db + + if db: + rowcount = 0 + cursor = db.cursor() + try: + cursor.execute("DELETE FROM data") + except sqlite3.OperationalError: + pass # database is not initialized, can't be modified, etc. + else: + rowcount = cursor.rowcount + db.commit() + cursor.execute("VACUUM") + return rowcount + + return None + + +def _path(): + path = config.get(("cache", "file"), -1) + + if path == -1: import tempfile import os.path - path = os.path.join(tempfile.gettempdir(), ".gallery-dl.cache") - else: - path = util.expand_path(path) + return os.path.join(tempfile.gettempdir(), ".gallery-dl.cache") + return util.expand_path(path) + + +try: DatabaseCacheDecorator.db = sqlite3.connect( - path, timeout=30, check_same_thread=False) - -except (RuntimeError, sqlite3.OperationalError): + _path(), timeout=30, check_same_thread=False) +except (TypeError, sqlite3.OperationalError): cache = memcache # noqa: F811 diff --git a/gallery_dl/option.py b/gallery_dl/option.py index 6dc0fa29..c7cc32bc 100644 --- a/gallery_dl/option.py +++ b/gallery_dl/option.py @@ -93,6 +93,11 @@ def build_parser(): metavar="URL", action=ConfigAction, dest="proxy", help="Use the specified proxy", ) + general.add_argument( + "--clear-cache", + dest="clear_cache", action="store_true", + help="Delete all cached login sessions, cookies, etc.", + ) output = parser.add_argument_group("Output Options") output.add_argument(