From 73260297ef82af30ba012617d7c89921ee4dfcc5 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Thu, 1 Feb 2024 16:24:16 +0530 Subject: [PATCH] job: get/update last annotation time from database --- job/main.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/job/main.py b/job/main.py index bb52848..6039ad1 100644 --- a/job/main.py +++ b/job/main.py @@ -25,13 +25,27 @@ class APIHandler: response.raise_for_status() return response.status_code -def get_last_update_time(): - with open(DATA_STORE_PATH, 'r') as file: - return int(file.read().strip()) +def get_last_update_time(email): + response = requests.get(f'{DATABASE_URL}/annotation_last_update/{email}') + response.raise_for_status() -def update_last_update_time(new_time): - with open(DATA_STORE_PATH, 'w') as file: - file.write(str(new_time)) + if response.status_code == 204: + return 0 + elif response.status_code == 200: + return response.json()['last_update_time'] + +def update_last_update_time(email, new_time): + response = requests.post( + f'{DATABASE_URL}/annotation_last_update', + headers={ + 'Content-Type': 'application/json' + }, + json={ + 'email': email, + 'last_update_time': new_time + } + ) + response.raise_for_status() def get_new_annotations(last_annotation_time, inoreader_token): inoreader = APIHandler( @@ -207,13 +221,13 @@ def main(): inoreader_token, readwise_api_key = check_and_refresh_access_token(token) - last_annotation_time = get_last_update_time() + last_annotation_time = get_last_update_time(token['email']) new_annotations = get_new_annotations(last_annotation_time, inoreader_token) if new_annotations: latest_added_on = max(annotation['added_on'] for annotation in new_annotations) push_annotations_to_readwise(new_annotations, readwise_api_key) - update_last_update_time(latest_added_on) + update_last_update_time(token['email'], latest_added_on) logging.info("Successfully pushed {} new annotations to Readwise for user with email: {}".format(len(new_annotations), token['email'])) else: logging.info("No new annotations found for user with email: {}".format(token['email']))