From 3a1d008790de70f51a0aacee8b852c2f0839418f Mon Sep 17 00:00:00 2001 From: fzzinchemical Date: Thu, 13 Nov 2025 20:11:49 +0100 Subject: [PATCH] Refactor JSON handling and improve error logging in main functionality --- main.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index b8674c9..12c469e 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,12 @@ OUTPUT_FILE = os.getenv("OUTPUT_FILE", "tweets.json") logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") logger = logging.getLogger(__name__) -COOKIES = os.getenv("COOKIES") +def check_if_json_exists(file_path): + return os.path.isfile(file_path) and os.path.getsize(file_path) > 0 + +def load_json(file_path): + with open(file_path, "r", encoding="utf-8") as f: + return json.load(f) async def main(): api = API() # or API("path-to.db") – default is `accounts.db` @@ -32,17 +37,22 @@ async def main(): await api.pool.login_all() # try to login to receive account cookies # NOTE 2: all methods have `raw` version (returns `httpx.Response` object): - async for rep in api.search("AI"): + + if check_if_json_exists(OUTPUT_FILE): + _results = load_json(OUTPUT_FILE).get("tweets", []) + else: _results = [] + + async for rep in api.search("AI", limit=5): try: _results.append(rep.json()) except Exception: - _results.append(rep.text) + logger.error("Failed to parse tweet JSON") await asyncio.sleep(random.uniform(7, 15)) # random delay between 7 and 15 seconds - with open(OUTPUT_FILE, "w", encoding="utf-8") as f: - json.dump({"tweets": _results}, f, ensure_ascii=False, indent=2) + with open(OUTPUT_FILE, "w", encoding="utf-8") as f: + f.write(json.dumps({"tweets": _results}, ensure_ascii=False, indent=4)) if __name__ == "__main__":