Separate services - app, db, and job #1
30
job/main.py
30
job/main.py
|
@ -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']))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user