Skip to content

Commit

Permalink
Use loguru for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
andylolz committed May 12, 2024
1 parent 07254a3 commit e567c22
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
python-dotenv
loguru
PyNaCl
requests
twscrape
11 changes: 6 additions & 5 deletions x_notes/__main__.py
Original file line number Diff line number Diff line change
@@ -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.")
15 changes: 8 additions & 7 deletions x_notes/tweets.py
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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"]
Expand All @@ -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)

0 comments on commit e567c22

Please sign in to comment.