diff --git a/requirements.txt b/requirements.txt index 09da9115b..3c7e7bbfa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ python-dotenv +loguru PyNaCl requests twscrape diff --git a/x_notes/__main__.py b/x_notes/__main__.py index a0bcc7edf..f5b8e899e 100644 --- a/x_notes/__main__.py +++ b/x_notes/__main__.py @@ -1,24 +1,25 @@ +from loguru import logger from .notes import get_notes from .statuses import add_statuses from .helpers import load_notes, save_notes, save_metadata if __name__ == "__main__": - print("Fetching notes ...") + logger.info("Fetching notes ...") notes = load_notes() notes = get_notes(notes) - print("Fetching statuses ...") + logger.info("Fetching statuses ...") notes = add_statuses(notes) - print("Sorting ...") + logger.info("Sorting ...") notes = dict(sorted( notes.items(), key=lambda x: x[1]["created_at"], reverse=True)) - print("Saving ...") + logger.info("Saving ...") save_notes(notes) save_metadata(notes) - print("Done.") + logger.info("Done.") diff --git a/x_notes/tweets.py b/x_notes/tweets.py index be0bf8851..54a0fb771 100644 --- a/x_notes/tweets.py +++ b/x_notes/tweets.py @@ -1,13 +1,14 @@ import json from os import environ from typing import Any +from loguru import logger from twscrape import API, NoAccountError from .github import update_secret from .helpers import load_notes, save_notes async def login() -> API: - print("Attempting to log in") + logger.info("Attempting to log in") api = API() username = environ["USER"] @@ -29,7 +30,7 @@ async def login() -> API: await api.pool.login_all() account = await api.pool.get(username) if environ.get("UPDATE_SECRET"): - print("Updating secret ...") + logger.info("Updating secret ...") update_secret("COOKIES", json.dumps(account.cookies)) return api @@ -40,7 +41,7 @@ def get_next_unfetched_note(notes: dict[str, dict[str, Any]]) -> dict[str, Any] notes = load_notes() if not get_next_unfetched_note(notes): - print("No tweets to fetch") + logger.info("No tweets to fetch") return api = await login() @@ -49,17 +50,17 @@ def get_next_unfetched_note(notes: dict[str, dict[str, Any]]) -> dict[str, Any] while True: note = get_next_unfetched_note(notes) if not note: - print("No more tweets to fetch") + logger.info("No more tweets to fetch") break note_id = note["note_id"] try: tweet = await api.tweet_details(int(note["tweet_id"])) except NoAccountError: - print("Rate limited – giving up") + logger.info("Rate limited – giving up") break account = await api.pool.get(environ["USER"]) if not account.active: - print("Failed to fetch tweet") + logger.info("Failed to fetch tweet") if environ.get("COOKIES"): await api.pool.delete_inactive() del environ["COOKIES"] @@ -75,5 +76,5 @@ def get_next_unfetched_note(notes: dict[str, dict[str, Any]]) -> dict[str, Any] note["deleted"] = 1 notes[note_id] = note - print(f"Total fetched: {total_fetched}") + logger.info(f"Total fetched: {total_fetched}") save_notes(notes)