forked from PeterSurda/inoreader2readwise
fix api calls and readwise payload
This commit is contained in:
parent
cc5bab0b5b
commit
04dae288ac
48
main.py
48
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:
|
||||
|
|
Loading…
Reference in New Issue
Block a user