Compare commits

..

No commits in common. "main" and "main" have entirely different histories.
main ... main

14 changed files with 189 additions and 74 deletions

View File

@ -6,4 +6,6 @@ ADD . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 80
CMD ["python", "main.py"]

View File

@ -1,11 +0,0 @@
FROM python:3.8-slim-buster
WORKDIR /app
ADD . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
CMD ["python", "main.py"]

View File

@ -1,8 +0,0 @@
CLIENT_ID=
CLIENT_SECRET=
REDIRECT_URI=
OPTIONAL_SCOPES=
DATABASE_URL=
# generated by `openssl rand -hex 24` - used to encrypt session
APP_SECRET_KEY=

View File

@ -1,28 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inoreader To Readwise</title>
</head>
<body>
<h1>Logged In as {{ user_login }} - {{ user_email }}</h1>
<!-- show last synced and next synced time -->
<p>Last Synced: {{ last_synced }}</p>
<p>Next Sync: {{ next_sync }}</p>
<br>
<!-- Take readiwse api key input -->
<form action="/readwise" method="POST">
<label for="api_key">Readwise API Key</label>
<input type="text" name="readwise_api_key" id="api_key", value="{{ readwise_api_key }}" required>
<input type="submit" value="Submit">
</form>
<!-- Logout -->
<form action="/logout" method="POST">
<input type="submit" value="Logout">
</form>
</body>
</html>

View File

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inoreader To Readwise</title>
</head>
<body>
<!-- <button onclick="redirectToOAuth()">Login using inoreader</button> -->
<a href="{{oauth_url}}">Login via InoReader</a>
</body>
</html>

View File

@ -1,11 +0,0 @@
FROM python:3.8-slim-buster
WORKDIR /app
ADD . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 5000
CMD ["python", "main.py"]

View File

@ -1,3 +0,0 @@
DATABASE_URL=
INOREADER_CLIENT_ID=
INOREADER_CLIENT_SECRET=

123
main.py Normal file
View File

@ -0,0 +1,123 @@
import os
import time
import json
import requests
import logging
DATA_STORE_PATH = "/data/last_update_time.txt"
logging.basicConfig(level=logging.INFO)
class APIHandler:
def __init__(self, base_url, headers={}):
self.base_url = base_url
self.headers = headers
def get(self, endpoint, params=None):
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), headers=self.headers)
response.raise_for_status()
return response.status_code
def get_last_update_time():
with open(DATA_STORE_PATH, 'r') as file:
return int(file.read().strip())
def update_last_update_time(new_time):
with open(DATA_STORE_PATH, 'w') as file:
file.write(str(new_time))
def get_new_annotations(last_annotation_time):
inoreader = APIHandler(
"https://www.inoreader.com/reader/api/0/stream/contents",
headers = {
'Authorization': 'Bearer ' + os.getenv("INOREADER_ACCESS_TOKEN")
}
)
all_annotations = []
continuation = None
while True:
params = {
"annotations": 1,
"n": 100,
}
if continuation:
params["c"] = continuation
inoreader_response = inoreader.get(
"/user/-/state/com.google/annotated",
params=params
)
data = json.loads(inoreader_response)
for item in data["items"]:
annotations = item.get("annotations", [])
for annotation in annotations:
annotation['title'] = item['title']
annotation['author'] = item['author']
annotation['sources'] = item['canonical']
all_annotations.append(annotation)
if 'continuation' in data:
continuation = data['continuation']
time.sleep(900) # Sleep for 15 minutes between pages
else:
break
return [annotation for annotation in all_annotations if annotation['added_on'] > last_annotation_time]
def push_annotations_to_readwise(annotations):
readwise = APIHandler(
"https://readwise.io",
headers = {
'Authorization': 'Token ' + os.getenv("READWISE_ACCESS_TOKEN"),
'Content-Type': 'application/json'
}
)
readwise.post(
"/api/v2/highlights/",
data={
'highlights': [
{
'text': annotation['text'],
'title': annotation['title'],
'author': annotation['author'],
'note': annotation['note'],
'highlighted_at': annotation['added_on'],
'category': 'articles',
'source_url': annotation['sources'][0]['href'] if annotation['sources'] else None,
}
for annotation in annotations
]
}
)
def main():
while True:
try:
last_annotation_time = get_last_update_time()
new_annotations = get_new_annotations(last_annotation_time)
if new_annotations:
latest_added_on = max(annotation['added_on'] for annotation in new_annotations)
push_annotations_to_readwise(new_annotations)
update_last_update_time(latest_added_on)
else:
logging.info("No new annotations found")
time.sleep(86400) # Sleep for 24 hours
except Exception as e:
logging.error(f"An error occurred: {e}")
time.sleep(3600) # Sleep for 1 hour in case of error
if __name__ == "__main__":
main()

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
requests==2.31.0

9
templates/login.jinja2 Normal file
View File

@ -0,0 +1,9 @@
<html>
<head>
<title>Logging in</title>
<meta http-equiv="refresh" content="0; url="https://www.inoreader.com/oauth2/auth?client_id={%inoreader_client_id%}"&redirect_uri={%siteurl%}/processoauth2&response_type=code&scope=read&state={%csrf_protection_string%}/">
</head>
<body>
Redirecting ...
</body>
</html>

9
templates/logout.jinja2 Normal file
View File

@ -0,0 +1,9 @@
<html>
<head>
<title>Logout</title>
<meta http-equiv="refresh" content="0; url="{%siteurl%}/">
</head>
<body>
Redirecting ...
</body>
</html>

View File

@ -0,0 +1,21 @@
<html>
<head>
<title>Inoreader 2 Readwise main screen
</head>
<body>
Hello {% inoreader_username %}
<p/>
<form action="setapikey">
API key:
<input type="text" id="apikey">{% readwise_apikey %}</input>
<submit>Change</submit>
</form>
<br/>
Last time sync: {%lastsync%} {%syncstatus%}}
<br/>
Next time sync: {%nextsync%}
<form action="logout">
<submit>Logout</submit>
</form>
</body>
</html>

View File

@ -0,0 +1,15 @@
<html>
<head>
<title>Processing oauth2</title>
{%if login_success%}
<meta http-equiv="refresh" content="0; url="{%siteurl%}/">
{%endif%}
</head>
<body>
{%if login_success%}
Redirecting ...
{%else%}
Login failed.
{%endif%}
</body>
</html>

View File

@ -0,0 +1,9 @@
<html>
<head>
<title>Changing API key</title>
<meta http-equiv="refresh" content="0; url="{%siteurl%}/">
</head>
<body>
Redirecting ...
</body>
</html>