Compare commits

...

2 Commits
main ... main

14 changed files with 74 additions and 189 deletions

11
app/Dockerfile Normal file
View File

@ -0,0 +1,11 @@
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"]

8
app/dummy.env Normal file
View File

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

28
app/templates/home.html Normal file
View File

@ -0,0 +1,28 @@
<!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>

13
app/templates/login.html Normal file
View File

@ -0,0 +1,13 @@
<!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>

11
database/Dockerfile Normal file
View File

@ -0,0 +1,11 @@
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

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

3
job/dummy.env Normal file
View File

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

123
main.py
View File

@ -1,123 +0,0 @@
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()

View File

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

View File

@ -1,9 +0,0 @@
<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>

View File

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

View File

@ -1,21 +0,0 @@
<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

@ -1,15 +0,0 @@
<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

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