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

View File

@ -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']))