diff --git a/x_notes/exceptions.py b/x_notes/exceptions.py new file mode 100644 index 000000000..dbbc4a40a --- /dev/null +++ b/x_notes/exceptions.py @@ -0,0 +1,2 @@ +class DataNotFoundException(Exception): + pass diff --git a/x_notes/notes.py b/x_notes/notes.py index ea5d1d18b..be21a1c4f 100644 --- a/x_notes/notes.py +++ b/x_notes/notes.py @@ -3,6 +3,7 @@ from typing import Any from urllib.parse import urlparse +from .exceptions import DataNotFoundException from .helpers import to_isoformat from .tsv import get_generator @@ -52,7 +53,12 @@ def get_notes(notes: dict[str, dict[str, Any]]) -> dict[str, dict[str, Any]]: for k, v in notes.items() if datetime.fromisoformat(v["created_at"]).timestamp() >= one_week_ago } - for row in get_generator("notes/notes"): + try: + gen = get_generator("notes/notes") + except DataNotFoundException: + return notes + + for row in gen: note_id = row["noteId"] created_at = to_isoformat(row["createdAtMillis"]) if float(row["createdAtMillis"]) / 1000 < one_week_ago: diff --git a/x_notes/ratings.py b/x_notes/ratings.py index b2db5db01..f8e78ba45 100644 --- a/x_notes/ratings.py +++ b/x_notes/ratings.py @@ -1,5 +1,6 @@ from typing import Any +from .exceptions import DataNotFoundException from .helpers import load_notes, save_notes from .tsv import get_generator @@ -21,7 +22,7 @@ def add_ratings(notes: dict[str, dict[str, Any]]) -> None: while True: try: gen = get_generator("noteRatings/ratings", index) - except Exception: + except DataNotFoundException: break for row in gen: if row["noteId"] in notes: diff --git a/x_notes/statuses.py b/x_notes/statuses.py index 4e7fba259..da2e175af 100644 --- a/x_notes/statuses.py +++ b/x_notes/statuses.py @@ -1,5 +1,6 @@ from typing import Any +from .exceptions import DataNotFoundException from .helpers import to_isoformat from .tsv import get_generator @@ -8,7 +9,12 @@ def add_statuses(notes: dict[str, dict[str, Any]]) -> dict[str, dict[str, Any]]: - for row in get_generator("noteStatusHistory/noteStatusHistory"): + try: + gen = get_generator("noteStatusHistory/noteStatusHistory") + except DataNotFoundException: + return notes + + for row in gen: note_id = row["noteId"] if note_id not in notes: continue diff --git a/x_notes/tsv.py b/x_notes/tsv.py index 6984cda83..7422bc1d3 100644 --- a/x_notes/tsv.py +++ b/x_notes/tsv.py @@ -5,6 +5,8 @@ import requests +from .exceptions import DataNotFoundException + def get_data(date: date, fname: str, index: int = 0) -> Generator: url_tmpl = ( @@ -35,4 +37,4 @@ def get_generator(fname: str, index: int = 0) -> Generator: return get_data(n_days_ago, fname, index) except Exception: pass - raise Exception + raise DataNotFoundException