forked from PeterSurda/inoreader2readwise
Compare commits
3 Commits
d9c5225a63
...
ab44ad09f6
Author | SHA1 | Date |
---|---|---|
Swapnil | ab44ad09f6 | |
Swapnil | 86c51478da | |
Swapnil | 73260297ef |
|
@ -3,4 +3,6 @@ CLIENT_SECRET=
|
|||
REDIRECT_URI=
|
||||
OPTIONAL_SCOPES=
|
||||
DATABASE_URL=
|
||||
|
||||
# generated by `openssl rand -base64 32` - used to encrypt session
|
||||
APP_SECRET_KEY=
|
|
@ -39,7 +39,7 @@ def home():
|
|||
|
||||
last_synced = datetime.fromtimestamp(token.get('updated_at')).strftime('%Y-%m-%d %H:%M:%S')
|
||||
next_sync = datetime.fromtimestamp(token.get('updated_at') + token.get('expiration_seconds')).strftime('%Y-%m-%d %H:%M:%S')
|
||||
return render_template('home.html', user_info=user_info,
|
||||
return render_template('home.html', user_login=user_info.login, user_email=user_info.email, # for inoreader it's userName
|
||||
readwise_api_key=token.get('readwise_api_key') or '',
|
||||
last_synced=last_synced, next_sync=next_sync)
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
<title>Inoreader To Readwise</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Logged In as {{ user_info.login }}({{user_info.name}})</h1>
|
||||
<h1>Logged In as {{ user_login }} - {{ user_email }}</h1>
|
||||
|
||||
<!-- show last synced and next synced time -->
|
||||
<p>Last Synced: {{ last_synced }}</p>
|
||||
|
|
|
@ -22,6 +22,18 @@ class Token(db.Model):
|
|||
def __repr__(self):
|
||||
return f'<Token {self.id}>'
|
||||
|
||||
# This table stores email-wise last annotation timestamp
|
||||
# only one entry per email
|
||||
class AnnotationLastUpdate(db.Model):
|
||||
id = db.Column(db.String(36), primary_key=True, default=str(uuid.uuid4()))
|
||||
email = db.Column(db.String(255), nullable=False)
|
||||
last_update_time = db.Column(db.DateTime, nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
def __repr__(self):
|
||||
return f'<AnnotationLastUpdate {self.id}>'
|
||||
|
||||
# Create an application context
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
|
@ -115,7 +127,7 @@ def update_token_by_id(id):
|
|||
|
||||
# deactivate token
|
||||
@app.route('/token/<id>/deactivate', methods=['POST'])
|
||||
def deactivate_token(id):
|
||||
def deactivate_token_by_id(id):
|
||||
token = Token.query.get_or_404(id)
|
||||
token.active = False
|
||||
db.session.commit()
|
||||
|
@ -139,5 +151,50 @@ def get_all_tokens():
|
|||
} for token in tokens]
|
||||
return jsonify({'tokens': tokens_info}), 200
|
||||
|
||||
# API to create or update the last annotation timestamp
|
||||
@app.route('/annotation_last_update', methods=['POST'])
|
||||
def create_or_update_annotation_last_update():
|
||||
data = request.get_json()
|
||||
email = data.get('email')
|
||||
last_update_time = data.get('last_update_time')
|
||||
|
||||
required_fields = ['email', 'last_update_time']
|
||||
missing_fields = [field for field in required_fields if not data.get(field)]
|
||||
|
||||
if missing_fields:
|
||||
return jsonify({'error': f'Missing required fields: {", ".join(missing_fields)}'}), 400
|
||||
|
||||
existing_annotation_last_update = AnnotationLastUpdate.query.filter_by(email=email).first()
|
||||
if existing_annotation_last_update:
|
||||
existing_annotation_last_update.last_update_time = last_update_time
|
||||
existing_annotation_last_update.updated_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
return '', 204
|
||||
else:
|
||||
new_annotation_last_update = AnnotationLastUpdate(
|
||||
email=email,
|
||||
last_update_time=last_update_time
|
||||
)
|
||||
db.session.add(new_annotation_last_update)
|
||||
db.session.commit()
|
||||
return '', 204
|
||||
|
||||
# API to get the last annotation timestamp based on the email
|
||||
@app.route('/annotation_last_update/<email>', methods=['GET'])
|
||||
def get_annotation_last_update_by_email(email):
|
||||
if not email:
|
||||
return jsonify({'error': 'Missing email query parameter'}), 400
|
||||
annotation_last_update = AnnotationLastUpdate.query.filter_by(email=email).first()
|
||||
if not annotation_last_update:
|
||||
return '', 204
|
||||
annotation_last_update_info = {
|
||||
'id': annotation_last_update.id,
|
||||
'email': annotation_last_update.email,
|
||||
'last_update_time': int(annotation_last_update.last_update_time.timestamp()),
|
||||
'created_at': int(annotation_last_update.created_at.timestamp()),
|
||||
'updated_at': int(annotation_last_update.updated_at.timestamp())
|
||||
}
|
||||
return jsonify(annotation_last_update_info), 200
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=5000, debug=True)
|
||||
|
|
30
job/main.py
30
job/main.py
|
@ -25,13 +25,27 @@ class APIHandler:
|
|||
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 get_last_update_time(email):
|
||||
response = requests.get(f'{DATABASE_URL}/annotation_last_update/{email}')
|
||||
response.raise_for_status()
|
||||
|
||||
def update_last_update_time(new_time):
|
||||
with open(DATA_STORE_PATH, 'w') as file:
|
||||
file.write(str(new_time))
|
||||
if response.status_code == 204:
|
||||
return 0
|
||||
elif response.status_code == 200:
|
||||
return response.json()['last_update_time']
|
||||
|
||||
def update_last_update_time(email, new_time):
|
||||
response = requests.post(
|
||||
f'{DATABASE_URL}/annotation_last_update',
|
||||
headers={
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
json={
|
||||
'email': email,
|
||||
'last_update_time': new_time
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
def get_new_annotations(last_annotation_time, inoreader_token):
|
||||
inoreader = APIHandler(
|
||||
|
@ -207,13 +221,13 @@ def main():
|
|||
|
||||
inoreader_token, readwise_api_key = check_and_refresh_access_token(token)
|
||||
|
||||
last_annotation_time = get_last_update_time()
|
||||
last_annotation_time = get_last_update_time(token['email'])
|
||||
new_annotations = get_new_annotations(last_annotation_time, inoreader_token)
|
||||
|
||||
if new_annotations:
|
||||
latest_added_on = max(annotation['added_on'] for annotation in new_annotations)
|
||||
push_annotations_to_readwise(new_annotations, readwise_api_key)
|
||||
update_last_update_time(latest_added_on)
|
||||
update_last_update_time(token['email'], latest_added_on)
|
||||
logging.info("Successfully pushed {} new annotations to Readwise for user with email: {}".format(len(new_annotations), token['email']))
|
||||
else:
|
||||
logging.info("No new annotations found for user with email: {}".format(token['email']))
|
||||
|
|
Loading…
Reference in New Issue