incorporate suggestions:

- fix get_last_update_time (int)
- use continuation query
- sleep 15 min b/w pages
- sleep 24 hrs b/w each cycle
- sleep 1 hr if error
- Push extra highlights properties
This commit is contained in:
Swapnil 2024-01-17 19:20:22 +05:30
parent 0da2a87caf
commit 5a5f737ff4
Signed by: swapnil
GPG Key ID: 58029C48BB100574

31
main.py
View File

@ -25,11 +25,11 @@ class APIHandler:
def get_last_update_time(): def get_last_update_time():
with open(DATA_STORE_PATH, 'r') as file: with open(DATA_STORE_PATH, 'r') as file:
return file.read().strip() return int(file.read().strip())
def update_last_update_time(new_time): def update_last_update_time(new_time):
with open(DATA_STORE_PATH, 'w') as file: with open(DATA_STORE_PATH, 'w') as file:
file.write(new_time) file.write(str(new_time))
def get_new_annotations(last_annotation_time): def get_new_annotations(last_annotation_time):
inoreader = APIHandler( inoreader = APIHandler(
@ -39,23 +39,37 @@ def get_new_annotations(last_annotation_time):
} }
) )
inoreader_response = inoreader.get( all_annotations = []
"/user/-/state/com.google/annotated", continuation = None
while True:
params = { params = {
"annotations": 1, "annotations": 1,
"n": 100, "n": 100,
} }
if continuation:
params["c"] = continuation
inoreader_response = inoreader.get(
"/user/-/state/com.google/annotated",
params=params
) )
data = json.loads(inoreader_response) data = json.loads(inoreader_response)
all_annotations = []
for item in data["items"]: for item in data["items"]:
annotations = item.get("annotations", []) annotations = item.get("annotations", [])
for annotation in annotations: for annotation in annotations:
annotation['title'] = item['title'] annotation['title'] = item['title']
annotation['author'] = item['author'] annotation['author'] = item['author']
annotation['sources'] = item['canonical']
all_annotations.append(annotation) all_annotations.append(annotation)
if 'continuation' in data:
continuation = data['continuation']
time.sleep(900) # Sleep for 15 minutes between pages
else:
break
return [annotation for annotation in all_annotations if annotation['added_on'] > last_annotation_time] return [annotation for annotation in all_annotations if annotation['added_on'] > last_annotation_time]
def push_annotations_to_readwise(annotations): def push_annotations_to_readwise(annotations):
@ -76,6 +90,9 @@ def push_annotations_to_readwise(annotations):
'title': annotation['title'], 'title': annotation['title'],
'author': annotation['author'], 'author': annotation['author'],
'note': annotation['note'], 'note': annotation['note'],
'highlighted_at': annotation['added_on'],
'category': 'articles',
'source_url': annotation['sources'][0]['href'] if annotation['sources'] else None,
} }
for annotation in annotations for annotation in annotations
] ]
@ -96,11 +113,11 @@ def main():
else: else:
logging.info("No new annotations found") logging.info("No new annotations found")
time.sleep(3600) time.sleep(86400) # Sleep for 24 hours
except Exception as e: except Exception as e:
logging.error(f"An error occurred: {e}") logging.error(f"An error occurred: {e}")
time.sleep(60) time.sleep(3600) # Sleep for 1 hour in case of error
if __name__ == "__main__": if __name__ == "__main__":
main() main()