forked from PeterSurda/inoreader2readwise
Refactor code and better annotations handling logic
This commit is contained in:
parent
644539a236
commit
cc5bab0b5b
92
main.py
92
main.py
|
@ -2,10 +2,29 @@ import os
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
import logging
|
||||||
|
|
||||||
# Define the path to the persistent data store
|
# 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)
|
||||||
|
|
||||||
|
class APIHandler:
|
||||||
|
def __init__(self, base_url, username, password):
|
||||||
|
self.base_url = base_url
|
||||||
|
self.auth = (username, password)
|
||||||
|
|
||||||
|
def get(self, endpoint, params=None):
|
||||||
|
response = requests.get(self.base_url + endpoint, params=params, auth=self.auth)
|
||||||
|
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.raise_for_status()
|
||||||
|
return response.status_code
|
||||||
|
|
||||||
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 file.read().strip()
|
||||||
|
@ -14,47 +33,62 @@ 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 get_annotations(last_update_time, inoreader_username, inoreader_password):
|
|
||||||
# Replace with actual API call
|
|
||||||
# This is a placeholder implementation
|
|
||||||
response = requests.get(
|
|
||||||
"https://www.inoreader.com/reader/api/0",
|
|
||||||
params={"since": last_update_time},
|
|
||||||
auth=(inoreader_username, inoreader_password),
|
|
||||||
)
|
|
||||||
return response.json()
|
|
||||||
|
|
||||||
def push_to_readwise(annotations, readwise_username, readwise_password):
|
|
||||||
# Replace with actual API call
|
|
||||||
# This is a placeholder implementation
|
|
||||||
response = requests.post(
|
|
||||||
"https://readwise.io/api/annotations",
|
|
||||||
data=json.dumps(annotations),
|
|
||||||
auth=(readwise_username, readwise_password),
|
|
||||||
)
|
|
||||||
return response.status_code
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Get credentials from environment variables
|
# Get credentials from environment variables
|
||||||
inoreader_username = os.getenv("INOREADER_USERNAME")
|
inoreader = APIHandler(
|
||||||
inoreader_password = os.getenv("INOREADER_PASSWORD")
|
"https://www.inoreader.com/reader/api/0/stream/contents",
|
||||||
readwise_username = os.getenv("READWISE_USERNAME")
|
os.getenv("INOREADER_USERNAME"),
|
||||||
readwise_password = os.getenv("READWISE_PASSWORD")
|
os.getenv("INOREADER_PASSWORD")
|
||||||
|
)
|
||||||
|
readwise = APIHandler(
|
||||||
|
"https://readwise.io",
|
||||||
|
os.getenv("READWISE_USERNAME"),
|
||||||
|
os.getenv("READWISE_PASSWORD")
|
||||||
|
)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
last_update_time = get_last_update_time()
|
try:
|
||||||
|
last_annotation_time = get_last_update_time()
|
||||||
|
|
||||||
# Get annotations after the last update time
|
# Get annotations after the last update time
|
||||||
annotations = get_annotations(last_update_time, inoreader_username, inoreader_password)
|
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:
|
||||||
|
latest_added_on = max(annotation['added_on'] for annotation in new_annotations)
|
||||||
|
else:
|
||||||
|
latest_added_on = None
|
||||||
|
|
||||||
# Push annotations to Readwise
|
# Push annotations to Readwise
|
||||||
push_to_readwise(annotations, readwise_username, readwise_password)
|
readwise.post("/api/annotations", data=annotations)
|
||||||
|
|
||||||
# Update the last update time
|
# Update the last annotation time
|
||||||
update_last_update_time(time.ctime())
|
if latest_added_on:
|
||||||
|
update_last_update_time(latest_added_on)
|
||||||
|
else:
|
||||||
|
logging.info("No new annotations found")
|
||||||
|
|
||||||
# Wait for an hour
|
# Wait for an hour
|
||||||
time.sleep(3600)
|
time.sleep(3600)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"An error occurred: {e}")
|
||||||
|
time.sleep(60) # Wait a minute before retrying
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue
Block a user