forked from PeterSurda/inoreader2readwise
refactor
This commit is contained in:
parent
04dae288ac
commit
0da2a87caf
104
main.py
104
main.py
|
@ -4,10 +4,8 @@ import json
|
||||||
import requests
|
import requests
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
# Define the path to the persistent data store
|
|
||||||
DATA_STORE_PATH = "/data/last_update_time.txt"
|
DATA_STORE_PATH = "/data/last_update_time.txt"
|
||||||
|
|
||||||
# Set up logging
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
class APIHandler:
|
class APIHandler:
|
||||||
|
@ -33,14 +31,34 @@ 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(new_time)
|
||||||
|
|
||||||
def main():
|
def get_new_annotations(last_annotation_time):
|
||||||
# Get credentials from environment variables
|
|
||||||
inoreader = APIHandler(
|
inoreader = APIHandler(
|
||||||
"https://www.inoreader.com/reader/api/0/stream/contents",
|
"https://www.inoreader.com/reader/api/0/stream/contents",
|
||||||
headers = {
|
headers = {
|
||||||
'Authorization': 'Bearer ' + os.getenv("INOREADER_ACCESS_TOKEN")
|
'Authorization': 'Bearer ' + os.getenv("INOREADER_ACCESS_TOKEN")
|
||||||
})
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
inoreader_response = inoreader.get(
|
||||||
|
"/user/-/state/com.google/annotated",
|
||||||
|
params={
|
||||||
|
"annotations": 1,
|
||||||
|
"n": 100,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
data = json.loads(inoreader_response)
|
||||||
|
all_annotations = []
|
||||||
|
|
||||||
|
for item in data["items"]:
|
||||||
|
annotations = item.get("annotations", [])
|
||||||
|
for annotation in annotations:
|
||||||
|
annotation['title'] = item['title']
|
||||||
|
annotation['author'] = item['author']
|
||||||
|
all_annotations.append(annotation)
|
||||||
|
|
||||||
|
return [annotation for annotation in all_annotations if annotation['added_on'] > last_annotation_time]
|
||||||
|
|
||||||
|
def push_annotations_to_readwise(annotations):
|
||||||
readwise = APIHandler(
|
readwise = APIHandler(
|
||||||
"https://readwise.io",
|
"https://readwise.io",
|
||||||
headers = {
|
headers = {
|
||||||
|
@ -49,74 +67,40 @@ def main():
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
readwise.post(
|
||||||
|
"/api/v2/highlights/",
|
||||||
|
data={
|
||||||
|
'highlights': [
|
||||||
|
{
|
||||||
|
'text': annotation['text'],
|
||||||
|
'title': annotation['title'],
|
||||||
|
'author': annotation['author'],
|
||||||
|
'note': annotation['note'],
|
||||||
|
}
|
||||||
|
for annotation in annotations
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
last_annotation_time = get_last_update_time()
|
last_annotation_time = get_last_update_time()
|
||||||
|
new_annotations = get_new_annotations(last_annotation_time)
|
||||||
# Get annotations after the last update time
|
|
||||||
inoreader_response = inoreader.get(
|
|
||||||
"/user/-/state/com.google/annotated",
|
|
||||||
params={
|
|
||||||
"annotations": 1,
|
|
||||||
"n": 100,
|
|
||||||
#"ot": last_annotation_time or None
|
|
||||||
}
|
|
||||||
)
|
|
||||||
data = json.loads(inoreader_response)
|
|
||||||
all_annotations = []
|
|
||||||
|
|
||||||
for item in data["items"]:
|
|
||||||
annotations = item.get("annotations", [])
|
|
||||||
all_annotations.extend(annotations)
|
|
||||||
|
|
||||||
# Filter annotations
|
|
||||||
new_annotations = [annotation for annotation in all_annotations if annotation['added_on'] > last_annotation_time]
|
|
||||||
|
|
||||||
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)
|
||||||
else:
|
push_annotations_to_readwise(new_annotations)
|
||||||
latest_added_on = None
|
|
||||||
|
|
||||||
# group annotations by title and author
|
|
||||||
grouped_annotations = {}
|
|
||||||
for annotation in new_annotations:
|
|
||||||
title = annotation['title']
|
|
||||||
author = annotation['author']
|
|
||||||
key = (title, author)
|
|
||||||
if key not in grouped_annotations:
|
|
||||||
grouped_annotations[key] = []
|
|
||||||
grouped_annotations[key].append(annotation)
|
|
||||||
|
|
||||||
# Push annotations to Readwise
|
|
||||||
readwise.post(
|
|
||||||
"/api/v2/highlights/",
|
|
||||||
# convert the grouped_annotations above for readwise payload
|
|
||||||
data={
|
|
||||||
'highlights': [
|
|
||||||
{
|
|
||||||
'text': annotation['text'],
|
|
||||||
'title': key[0],
|
|
||||||
'author': key[1],
|
|
||||||
'note': annotation['note'],
|
|
||||||
}
|
|
||||||
for key, annotations in grouped_annotations.items()
|
|
||||||
for annotation in annotations
|
|
||||||
]
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Update the last annotation time
|
|
||||||
if latest_added_on:
|
|
||||||
update_last_update_time(latest_added_on)
|
update_last_update_time(latest_added_on)
|
||||||
else:
|
else:
|
||||||
logging.info("No new annotations found")
|
logging.info("No new annotations found")
|
||||||
|
|
||||||
# Wait for an hour
|
|
||||||
time.sleep(3600)
|
time.sleep(3600)
|
||||||
|
|
||||||
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) # Wait a minute before retrying
|
time.sleep(60)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user