From 04dae288acbce2f36aea6a1d4369c45e34664082 Mon Sep 17 00:00:00 2001 From: Swapnil Date: Wed, 17 Jan 2024 16:55:43 +0530 Subject: [PATCH] fix api calls and readwise payload --- main.py | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 147c1a0..9dda78b 100644 --- a/main.py +++ b/main.py @@ -11,17 +11,17 @@ DATA_STORE_PATH = "/data/last_update_time.txt" logging.basicConfig(level=logging.INFO) class APIHandler: - def __init__(self, base_url, username, password): + def __init__(self, base_url, headers={}): self.base_url = base_url - self.auth = (username, password) + self.headers = headers def get(self, endpoint, params=None): - response = requests.get(self.base_url + endpoint, params=params, auth=self.auth) + response = requests.get(self.base_url + endpoint, params=params, headers=self.headers) response.raise_for_status() return response.json() def post(self, endpoint, data=None): - response = requests.post(self.base_url + endpoint, data=json.dumps(data), auth=self.auth) + response = requests.post(self.base_url + endpoint, data=json.dumps(data), headers=self.headers) response.raise_for_status() return response.status_code @@ -37,13 +37,16 @@ def main(): # Get credentials from environment variables inoreader = APIHandler( "https://www.inoreader.com/reader/api/0/stream/contents", - os.getenv("INOREADER_USERNAME"), - os.getenv("INOREADER_PASSWORD") - ) + headers = { + 'Authorization': 'Bearer ' + os.getenv("INOREADER_ACCESS_TOKEN") + }) + readwise = APIHandler( "https://readwise.io", - os.getenv("READWISE_USERNAME"), - os.getenv("READWISE_PASSWORD") + headers = { + 'Authorization': 'Token ' + os.getenv("READWISE_ACCESS_TOKEN"), + 'Content-Type': 'application/json' + } ) while True: @@ -73,9 +76,34 @@ def main(): latest_added_on = max(annotation['added_on'] for annotation in new_annotations) else: 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/annotations", data=annotations) + 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: