From 5277bf92da402870ad938724dcca2c418b139e36 Mon Sep 17 00:00:00 2001 From: Andy Lulham Date: Thu, 8 Aug 2024 08:45:02 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20if=20there=E2=80=99s=20a=20problem=20ups?= =?UTF-8?q?tream,=20continue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- x_notes/exceptions.py | 2 ++ x_notes/notes.py | 8 +++++++- x_notes/ratings.py | 3 ++- x_notes/statuses.py | 8 +++++++- x_notes/tsv.py | 4 +++- 5 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 x_notes/exceptions.py diff --git a/x_notes/exceptions.py b/x_notes/exceptions.py new file mode 100644 index 00000000..dbbc4a40 --- /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 ea5d1d18..be21a1c4 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 b2db5db0..f8e78ba4 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 4e7fba25..da2e175a 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 6984cda8..7422bc1d 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