job: get/update last annotation time from database

This commit is contained in:
Swapnil 2024-02-01 16:24:16 +05:30
parent 1c0f991977
commit 889469dcff
Signed by: swapnil
GPG Key ID: 58029C48BB100574
1 changed files with 22 additions and 8 deletions

View File

@ -25,13 +25,27 @@ class APIHandler:
response.raise_for_status() response.raise_for_status()
return response.status_code return response.status_code
def get_last_update_time(): def get_last_update_time(email):
with open(DATA_STORE_PATH, 'r') as file: response = requests.get(f'{DATABASE_URL}/annotation_last_update/{email}')
return int(file.read().strip()) response.raise_for_status()
def update_last_update_time(new_time): if response.status_code == 204:
with open(DATA_STORE_PATH, 'w') as file: return 0
file.write(str(new_time)) 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): def get_new_annotations(last_annotation_time, inoreader_token):
inoreader = APIHandler( inoreader = APIHandler(
@ -207,13 +221,13 @@ def main():
inoreader_token, readwise_api_key = check_and_refresh_access_token(token) 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) new_annotations = get_new_annotations(last_annotation_time, inoreader_token)
if new_annotations: if new_annotations:
latest_added_on = max(annotation['added_on'] for annotation in new_annotations) latest_added_on = max(annotation['added_on'] for annotation in new_annotations)
push_annotations_to_readwise(new_annotations, readwise_api_key) 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'])) logging.info("Successfully pushed {} new annotations to Readwise for user with email: {}".format(len(new_annotations), token['email']))
else: else:
logging.info("No new annotations found for user with email: {}".format(token['email'])) logging.info("No new annotations found for user with email: {}".format(token['email']))