Compare commits
2 Commits
main
...
feature/se
Author | SHA1 | Date | |
---|---|---|---|
6ad03b120c | |||
a17303347f |
|
@ -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=
|
|
94
app/main.py
Normal file
94
app/main.py
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
import os
|
||||||
|
from flask import Flask, render_template, request, redirect, abort
|
||||||
|
import requests
|
||||||
|
|
||||||
|
def get_env_variable(var_name):
|
||||||
|
value = os.environ.get(var_name)
|
||||||
|
if not value:
|
||||||
|
raise ValueError(f"Missing required environment variable: {var_name}")
|
||||||
|
return value
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Read environment variables outside the route function
|
||||||
|
client_id = get_env_variable('CLIENT_ID')
|
||||||
|
redirect_uri = get_env_variable('REDIRECT_URI')
|
||||||
|
optional_scopes = get_env_variable('OPTIONAL_SCOPES')
|
||||||
|
database_url = get_env_variable('DATABASE_URL')
|
||||||
|
|
||||||
|
csrf_protection_string = None
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def home():
|
||||||
|
if is_logged_in():
|
||||||
|
return render_template('success.html')
|
||||||
|
|
||||||
|
# Generate a CSRF protection string
|
||||||
|
global csrf_protection_string
|
||||||
|
csrf_protection_string = os.urandom(16).hex()
|
||||||
|
|
||||||
|
# Pass dynamic variables to the template
|
||||||
|
return render_template('index.html', client_id=client_id, redirect_uri=redirect_uri,
|
||||||
|
optional_scopes=optional_scopes, csrf_protection_string=csrf_protection_string)
|
||||||
|
|
||||||
|
@app.route('/oauth-redirect')
|
||||||
|
def oauth_redirect():
|
||||||
|
auth_code = request.args.get('code')
|
||||||
|
csrf_token = request.args.get('state')
|
||||||
|
|
||||||
|
# Verify the CSRF protection string
|
||||||
|
if csrf_token != csrf_protection_string:
|
||||||
|
abort(400, 'Invalid CSRF token. Please try again.')
|
||||||
|
|
||||||
|
# Exchange authorization code for access and refresh tokens
|
||||||
|
response = requests.post(
|
||||||
|
'https://www.inoreader.com/oauth2/token',
|
||||||
|
headers={
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
'User-agent': 'your-user-agent'
|
||||||
|
},
|
||||||
|
data={
|
||||||
|
'code': auth_code,
|
||||||
|
'redirect_uri': get_env_variable('REDIRECT_URI'),
|
||||||
|
'client_id': get_env_variable('CLIENT_ID'),
|
||||||
|
'client_secret': get_env_variable('CLIENT_SECRET'),
|
||||||
|
'scope': '',
|
||||||
|
'grant_type': 'authorization_code'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
tokens = response.json()
|
||||||
|
|
||||||
|
# Save tokens for later use
|
||||||
|
save_tokens(tokens['access_token'], tokens['refresh_token'], tokens['expires_in'])
|
||||||
|
|
||||||
|
return redirect('/')
|
||||||
|
|
||||||
|
def is_logged_in():
|
||||||
|
response = requests.get(f'{database_url}/token/latest')
|
||||||
|
response.raise_for_status()
|
||||||
|
if response.status_code == 204:
|
||||||
|
return False
|
||||||
|
elif response.status_code == 200:
|
||||||
|
resp_json = response.json()
|
||||||
|
return resp_json['token']['expiration_seconds'] + resp_json['token']['timestamp'] > datetime.now().timestamp()
|
||||||
|
return False
|
||||||
|
|
||||||
|
def save_tokens(access_token, refresh_token, expiration_seconds):
|
||||||
|
response = requests.post(
|
||||||
|
f'{database_url}/token',
|
||||||
|
headers={
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
json={
|
||||||
|
'access_token': access_token,
|
||||||
|
'refresh_token': refresh_token,
|
||||||
|
'expiration_seconds': expiration_seconds
|
||||||
|
}
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(debug=True, port=5000)
|
2
app/requirements.txt
Normal file
2
app/requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Flask==3.0.1
|
||||||
|
requests==2.31.0
|
|
@ -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>
|
|
25
app/templates/index.html
Normal file
25
app/templates/index.html
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Simple Frontend</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<button onclick="redirectToOAuth()">Click Me</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function redirectToOAuth() {
|
||||||
|
// Encode URL components using Jinja filters
|
||||||
|
var encodedRedirectUri = encodeURIComponent('{{ redirect_uri }}');
|
||||||
|
var encodedOptionalScopes = encodeURIComponent('{{ optional_scopes }}');
|
||||||
|
|
||||||
|
// Construct the URL using Jinja variables
|
||||||
|
var oauthUrl = `https://www.inoreader.com/oauth2/auth?client_id={{ client_id }}&redirect_uri=${encodedRedirectUri}&response_type=code&scope=${encodedOptionalScopes}&state={{ csrf_protection_string }}`;
|
||||||
|
|
||||||
|
// Redirect to the constructed URL
|
||||||
|
window.location.href = oauthUrl;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -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>
|
|
11
app/templates/success.html
Normal file
11
app/templates/success.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Simple Frontend</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Logged In!</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
55
database/main.py
Normal file
55
database/main.py
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
from flask import Flask, jsonify, request
|
||||||
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tokens.db' # Use SQLite for simplicity
|
||||||
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||||
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
|
class Token(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
access_token = db.Column(db.String(255), nullable=False)
|
||||||
|
refresh_token = db.Column(db.String(255), nullable=False)
|
||||||
|
expiration_seconds = db.Column(db.Integer, nullable=False)
|
||||||
|
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<Token {self.id}>'
|
||||||
|
|
||||||
|
# Create an application context
|
||||||
|
with app.app_context():
|
||||||
|
db.create_all()
|
||||||
|
|
||||||
|
# API to create a new token entry
|
||||||
|
@app.route('/token', methods=['POST'])
|
||||||
|
def create_token():
|
||||||
|
data = request.get_json()
|
||||||
|
access_token = data.get('access_token')
|
||||||
|
refresh_token = data.get('refresh_token')
|
||||||
|
expiration_seconds = data.get('expiration_seconds')
|
||||||
|
|
||||||
|
new_token = Token(access_token=access_token, refresh_token=refresh_token, expiration_seconds=expiration_seconds)
|
||||||
|
db.session.add(new_token)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return ('', 204)
|
||||||
|
|
||||||
|
# API to get the latest token entry
|
||||||
|
@app.route('/token/latest', methods=['GET'])
|
||||||
|
def get_latest_token():
|
||||||
|
latest_token = Token.query.order_by(Token.timestamp.desc()).first()
|
||||||
|
|
||||||
|
if latest_token:
|
||||||
|
token_info = {
|
||||||
|
'access_token': latest_token.access_token,
|
||||||
|
'refresh_token': latest_token.refresh_token,
|
||||||
|
'expiration_seconds': latest_token.expiration_seconds,
|
||||||
|
'timestamp': latest_token.timestamp.isoformat()
|
||||||
|
}
|
||||||
|
return jsonify({'token': token_info}), 200
|
||||||
|
else:
|
||||||
|
return '', 204
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(host='0.0.0.0', port=5000, debug=True)
|
2
database/requirements.txt
Normal file
2
database/requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Flask==3.0.1
|
||||||
|
Flask-SQLAlchemy==3.1.1
|
|
@ -1,3 +0,0 @@
|
||||||
DATABASE_URL=
|
|
||||||
INOREADER_CLIENT_ID=
|
|
||||||
INOREADER_CLIENT_SECRET=
|
|
184
job/main.py
Normal file
184
job/main.py
Normal file
|
@ -0,0 +1,184 @@
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
import requests
|
||||||
|
import logging
|
||||||
|
|
||||||
|
DATA_STORE_PATH = "/data/last_update_time.txt"
|
||||||
|
|
||||||
|
DATABASE_URL = os.getenv("DATABASE_URL")
|
||||||
|
|
||||||
|
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 ' + get_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 get_inoreader_access_token():
|
||||||
|
response = requests.get(f'{DATABASE_URL}/token/latest')
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
if response.status_code == 204:
|
||||||
|
# throw error - not logged in. Please log in first through the web app
|
||||||
|
raise Exception("Not logged in. Please log in first through the web app")
|
||||||
|
elif response.status_code == 200:
|
||||||
|
resp_json = response.json()
|
||||||
|
if resp_json['token']['expiration_seconds'] + resp_json['token']['timestamp'] > datetime.now().timestamp():
|
||||||
|
return resp_json['token']['access_token']
|
||||||
|
else:
|
||||||
|
return refresh_inoreader_access_token(resp_json['token']['refresh_token'])
|
||||||
|
|
||||||
|
access_token = get_token_from_database()
|
||||||
|
if not access_token:
|
||||||
|
access_token = refresh_inoreader_access_token()
|
||||||
|
|
||||||
|
if not access_token:
|
||||||
|
raise Exception("Unable to get access token. Try logging in again through the web app")
|
||||||
|
return access_token
|
||||||
|
|
||||||
|
def refresh_inoreader_access_token(refresh_token):
|
||||||
|
response = requests.post(
|
||||||
|
'https://www.inoreader.com/oauth2/token',
|
||||||
|
headers={
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
},
|
||||||
|
data={
|
||||||
|
'refresh_token': refresh_token,
|
||||||
|
'client_id': os.getenv("INOREADER_CLIENT_ID"),
|
||||||
|
'client_secret': os.getenv("INOREADER_CLIENT_SECRET"),
|
||||||
|
'grant_type': 'refresh_token'
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
tokens = response.json()
|
||||||
|
|
||||||
|
# Save tokens for later use
|
||||||
|
save_tokens(tokens['access_token'], tokens['refresh_token'], tokens['expires_in'])
|
||||||
|
|
||||||
|
return tokens['access_token']
|
||||||
|
|
||||||
|
def save_tokens(access_token, refresh_token, expiration_seconds):
|
||||||
|
response = requests.post(
|
||||||
|
f'{DATABASE_URL}/token',
|
||||||
|
headers={
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
json={
|
||||||
|
'access_token': access_token,
|
||||||
|
'refresh_token': refresh_token,
|
||||||
|
'expiration_seconds': expiration_seconds
|
||||||
|
}
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
|
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
job/requirements.txt
Normal file
1
job/requirements.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
requests==2.31.0
|
Loading…
Reference in New Issue
Block a user