forked from PeterSurda/inoreader2readwise
Compare commits
3 Commits
be4500796c
...
4c022a6080
Author | SHA1 | Date |
---|---|---|
Swapnil | 4c022a6080 | |
Swapnil | faed583490 | |
Swapnil | 55a1b643d4 |
|
@ -97,6 +97,8 @@ def oauth_redirect():
|
|||
token['refresh_token'] = 'N/A'
|
||||
token['expires_in'] = 3600
|
||||
|
||||
# REPLACE user API call with inoreader API call
|
||||
# https://www.inoreader.com/reader/api/0/user-info
|
||||
user_info = requests.get('https://api.github.com/user', headers={
|
||||
'Authorization': f'Bearer {token.get("access_token")}'
|
||||
}).json()
|
||||
|
|
|
@ -34,6 +34,7 @@ def create_token():
|
|||
access_token = data.get('access_token')
|
||||
refresh_token = data.get('refresh_token')
|
||||
expiration_seconds = data.get('expiration_seconds')
|
||||
readwise_api_key = data.get('readwise_api_key')
|
||||
|
||||
required_fields = ['email', 'access_token', 'refresh_token', 'expiration_seconds']
|
||||
missing_fields = [field for field in required_fields if not data.get(field)]
|
||||
|
@ -46,7 +47,13 @@ def create_token():
|
|||
if existing_token:
|
||||
return jsonify({'error': 'An active token with this email already exists'}), 400
|
||||
|
||||
new_token = Token(email=email, access_token=access_token, refresh_token=refresh_token, expiration_seconds=expiration_seconds)
|
||||
new_token = Token(
|
||||
email=email,
|
||||
access_token=access_token,
|
||||
refresh_token=refresh_token,
|
||||
expiration_seconds=expiration_seconds,
|
||||
readwise_api_key=readwise_api_key
|
||||
)
|
||||
db.session.add(new_token)
|
||||
db.session.commit()
|
||||
|
||||
|
@ -101,16 +108,24 @@ def update_token_by_id(id):
|
|||
token.access_token = data.get('access_token', token.access_token)
|
||||
token.refresh_token = data.get('refresh_token', token.refresh_token)
|
||||
token.expiration_seconds = data.get('expiration_seconds', token.expiration_seconds)
|
||||
token.active = data.get('active', token.active)
|
||||
token.readwise_api_key = data.get('readwise_api_key', token.readwise_api_key)
|
||||
token.updated_at = datetime.utcnow()
|
||||
db.session.commit()
|
||||
return '', 204
|
||||
|
||||
# deactivate token
|
||||
@app.route('/token/<id>/deactivate', methods=['POST'])
|
||||
def deactivate_token(id):
|
||||
token = Token.query.get_or_404(id)
|
||||
token.active = False
|
||||
db.session.commit()
|
||||
return '', 204
|
||||
|
||||
# get all tokens
|
||||
@app.route('/token/all', methods=['GET'])
|
||||
def get_all_tokens():
|
||||
tokens = Token.query.all()
|
||||
only_active = request.args.get('only_active')
|
||||
tokens = Token.query.all() if not only_active else Token.query.filter_by(active=True).all()
|
||||
tokens_info = [{
|
||||
'id': token.id,
|
||||
'email': token.email,
|
||||
|
|
116
job/main.py
116
job/main.py
|
@ -33,11 +33,11 @@ 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):
|
||||
def get_new_annotations(last_annotation_time, inoreader_token):
|
||||
inoreader = APIHandler(
|
||||
"https://www.inoreader.com/reader/api/0/stream/contents",
|
||||
headers = {
|
||||
'Authorization': 'Bearer ' + get_inoreader_access_token()
|
||||
'Authorization': 'Bearer ' + inoreader_token()
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -74,11 +74,11 @@ def get_new_annotations(last_annotation_time):
|
|||
|
||||
return [annotation for annotation in all_annotations if annotation['added_on'] > last_annotation_time]
|
||||
|
||||
def push_annotations_to_readwise(annotations):
|
||||
def push_annotations_to_readwise(annotations, readwise_token):
|
||||
readwise = APIHandler(
|
||||
"https://readwise.io",
|
||||
headers = {
|
||||
'Authorization': 'Token ' + os.getenv("READWISE_ACCESS_TOKEN"),
|
||||
'Authorization': 'Token ' + readwise_token,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
)
|
||||
|
@ -101,29 +101,29 @@ def push_annotations_to_readwise(annotations):
|
|||
}
|
||||
)
|
||||
|
||||
def get_inoreader_access_token():
|
||||
response = requests.get(f'{DATABASE_URL}/token/latest')
|
||||
response.raise_for_status()
|
||||
# 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'])
|
||||
# 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()
|
||||
# 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
|
||||
# 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):
|
||||
def refresh_inoreader_access_token(refresh_token, readwise_api_key):
|
||||
response = requests.post(
|
||||
'https://www.inoreader.com/oauth2/token',
|
||||
headers={
|
||||
|
@ -139,40 +139,84 @@ def refresh_inoreader_access_token(refresh_token):
|
|||
|
||||
response.raise_for_status()
|
||||
|
||||
tokens = response.json()
|
||||
token = response.json()
|
||||
|
||||
user_info = requests.get('https://www.inoreader.com/reader/api/0/user-info', headers={
|
||||
'Authorization': f'Bearer {token.get("access_token")}'
|
||||
}).json()
|
||||
|
||||
# Save tokens for later use
|
||||
save_tokens(tokens['access_token'], tokens['refresh_token'], tokens['expires_in'])
|
||||
save_token(
|
||||
user_info.get('userEmail'),
|
||||
token['access_token'],
|
||||
token['refresh_token'],
|
||||
token['expires_in'],
|
||||
readwise_api_key
|
||||
)
|
||||
|
||||
return tokens['access_token']
|
||||
return token['access_token'], readwise_api_key
|
||||
|
||||
def save_tokens(access_token, refresh_token, expiration_seconds):
|
||||
def save_token(email, access_token, refresh_token, expiration_seconds, readwise_api_key):
|
||||
response = requests.post(
|
||||
f'{DATABASE_URL}/token',
|
||||
headers={
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
json={
|
||||
'email': email,
|
||||
'access_token': access_token,
|
||||
'refresh_token': refresh_token,
|
||||
'expiration_seconds': expiration_seconds
|
||||
'expiration_seconds': expiration_seconds,
|
||||
'readwise_api_key': readwise_api_key
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
def get_all_active_tokens():
|
||||
response = requests.get(f'{DATABASE_URL}/token/all?only_active=true')
|
||||
response.raise_for_status()
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.json()['tokens']
|
||||
else:
|
||||
return []
|
||||
|
||||
def deactivate_token(token_id):
|
||||
response = requests.post(
|
||||
f'{DATABASE_URL}/token/{token_id}/deactivate',
|
||||
headers={
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
def check_and_refresh_access_token(token):
|
||||
if token['expiration_seconds'] + token['timestamp'] > datetime.now().timestamp():
|
||||
return token['access_token'], token['readwise_api_key']
|
||||
else:
|
||||
deactivate_token(token['id'])
|
||||
return refresh_inoreader_access_token(token['refresh_token'], token['readwise_api_key'])
|
||||
|
||||
def main():
|
||||
|
||||
while True:
|
||||
try:
|
||||
last_annotation_time = get_last_update_time()
|
||||
new_annotations = get_new_annotations(last_annotation_time)
|
||||
all_tokens = get_all_active_tokens()
|
||||
for token in all_tokens:
|
||||
logging.info("Checking for new annotations for user with email: {}".format(token['email']))
|
||||
|
||||
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")
|
||||
inoreader_token, readwise_api_key = check_and_refresh_access_token(token)
|
||||
|
||||
last_annotation_time = get_last_update_time()
|
||||
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)
|
||||
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']))
|
||||
|
||||
time.sleep(86400) # Sleep for 24 hours
|
||||
|
||||
|
|
Loading…
Reference in New Issue