fix api calls and readwise payload

This commit is contained in:
Swapnil 2024-01-17 16:55:43 +05:30
parent cc5bab0b5b
commit 04dae288ac
Signed by: swapnil
GPG Key ID: 58029C48BB100574

48
main.py
View File

@ -11,17 +11,17 @@ DATA_STORE_PATH = "/data/last_update_time.txt"
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
class APIHandler: class APIHandler:
def __init__(self, base_url, username, password): def __init__(self, base_url, headers={}):
self.base_url = base_url self.base_url = base_url
self.auth = (username, password) self.headers = headers
def get(self, endpoint, params=None): 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() response.raise_for_status()
return response.json() return response.json()
def post(self, endpoint, data=None): 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() response.raise_for_status()
return response.status_code return response.status_code
@ -37,13 +37,16 @@ def main():
# Get credentials from environment variables # 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",
os.getenv("INOREADER_USERNAME"), headers = {
os.getenv("INOREADER_PASSWORD") 'Authorization': 'Bearer ' + os.getenv("INOREADER_ACCESS_TOKEN")
) })
readwise = APIHandler( readwise = APIHandler(
"https://readwise.io", "https://readwise.io",
os.getenv("READWISE_USERNAME"), headers = {
os.getenv("READWISE_PASSWORD") 'Authorization': 'Token ' + os.getenv("READWISE_ACCESS_TOKEN"),
'Content-Type': 'application/json'
}
) )
while True: while True:
@ -74,8 +77,33 @@ def main():
else: else:
latest_added_on = None 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 # 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 # Update the last annotation time
if latest_added_on: if latest_added_on: