Move caches to cache/ folder and add cleanup
Signed-off-by: Markus Birth <markus@birth-online.de>
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
# Cachefiles
|
||||
*.pickle
|
||||
*.json
|
||||
*.txt
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user