From b55ca08fe1dc61ee894ae09b90e0d0200345c5c0 Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Fri, 15 May 2026 17:57:48 +0100 Subject: [PATCH] Move caches to cache/ folder and add cleanup Signed-off-by: Markus Birth --- cache/.gitignore | 4 ++++ main.py | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 cache/.gitignore diff --git a/cache/.gitignore b/cache/.gitignore new file mode 100644 index 0000000..56be017 --- /dev/null +++ b/cache/.gitignore @@ -0,0 +1,4 @@ +# Cachefiles +*.pickle +*.json +*.txt diff --git a/main.py b/main.py index ed84d39..9ef676b 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ import configparser +import glob import json import requests +import os import os.path import pickle from collections.abc import Callable @@ -19,7 +21,7 @@ class BlueBackBlocker(): return self.agent.app.bsky.graph.get_lists({'actor': self.did}) def get_list(self, list_id: str, progress: Callable[[int, int], None] = None): - cachefile = f"bs_{list_id}.pickle" + cachefile = f"cache/bs_{list_id}.pickle" if os.path.isfile(cachefile): with open(cachefile, "rb") as f: return pickle.load(f) @@ -78,7 +80,7 @@ class ClearSky(): url = self.BASE_URL + "/api/v1/" + uri if page > 1: url += f"/{page}" - cachefile = "cs_" + url[len(self.BASE_URL)+8:].replace("/", "_") + ".json" + cachefile = "cache/cs_" + url[len(self.BASE_URL)+8:].replace("/", "_") + ".json" if os.path.isfile(cachefile): with open(cachefile, "rt") as f: r = json.load(f) @@ -140,11 +142,15 @@ def main(): did_map = {} already_blocked = set() to_block = set() + processed = 0 for item in blocklist["items"]: item_did = item["subject"]["did"] already_blocked.add(item_did) did_map[item_did] = item["subject"]["handle"] + processed += 1 + + print(f"Processed {processed} records, got {len(already_blocked)} unique entries.") cs = ClearSky(blocker.did) @@ -185,7 +191,7 @@ def main(): print(f"Totals: Found [bold red]{len(to_block)}[/bold red] users not already on list.") - success_file = "successes.txt" + success_file = "cache/successes.txt" if os.path.isfile(success_file): print("Found state file. Processing...") removed = 0 @@ -209,6 +215,16 @@ def main(): with open(success_file, "at") as f: f.write(did_to_block + "\n") prog.update(task_add_to_list, completed=users_processed) + + print(f"Processed {users_processed} people, got {users_error} errors.") + print("Removing BlueSky API cache file.") + for f in glob.glob("cache/bs_*.pickle"): + os.remove(f) + print("Removing ClearSky API caches.") + for f in glob.glob("cache/cs_*.json"): + os.remove(f) + print("Removing state cache.") + os.remove(success_file) if __name__ == "__main__": main()