public-health-ch/feedler/refresh.py

81 lines
2.7 KiB
Python
Raw Normal View History

2017-10-13 13:40:24 +00:00
# -*- coding: utf-8 -*-
import requests, json, codecs
import logging
logger = logging.getLogger('feedler')
from .models import Entry
from feedler import feedparser
API_BASEURL = 'https://cloud.feedly.com/v3/'
API_STREAMS = API_BASEURL + 'streams/contents?streamId='
API_TOKENS = API_BASEURL + 'auth/token'
def refresh_streams(settings):
# Iterate through all saved streams
2017-10-13 21:45:55 +00:00
logger.debug("Refreshing all streams")
2017-10-13 13:40:24 +00:00
for stream in settings.streams.all():
if not refresh_stream(stream, settings):
return False
return True
def refresh_token(settings):
# Request a new token
url = API_TOKENS
2017-10-13 21:45:55 +00:00
if not settings.refresh:
logger.warn("No Refresh token available")
return False
logger.info("Refreshing Feedly access token")
2017-10-13 13:40:24 +00:00
payload = {
2017-10-13 21:45:55 +00:00
'refresh_token': settings.refresh,
2017-10-13 13:40:24 +00:00
'client_id': 'feedlydev',
'client_secret': 'feedlydev',
'grant_type': 'refresh_token'
}
2017-10-13 21:45:55 +00:00
contents = requests.post(url, data=payload).json()
2017-10-13 13:40:24 +00:00
if not 'access_token' in contents or not contents['access_token']:
2017-10-13 21:45:55 +00:00
logger.warn("Access token could not be refreshed.")
logger.debug(contents)
2017-10-13 13:40:24 +00:00
return False
settings.token = contents['access_token']
settings.save()
return True
def refresh_stream(stream, settings, retry=False):
# Start a request to download the feed for a particular stream
2017-10-13 21:45:55 +00:00
logger.info("Processing stream %s" % stream.title)
2017-10-13 13:40:24 +00:00
url = API_STREAMS + stream.ident
2017-10-13 21:45:55 +00:00
headers = { 'Authorization': 'OAuth ' + settings.token }
contents = requests.get(url, headers=headers).json()
2017-10-13 13:40:24 +00:00
if 'errorMessage' in contents:
2017-10-23 08:01:02 +00:00
errmsg = contents['errorMessage']
2017-10-13 13:40:24 +00:00
# Usually this is a token expired
2017-10-23 08:01:02 +00:00
if 'expired' in errmsg or 'unauthorized' in errmsg:
logger.debug(errmsg)
2017-10-13 13:40:24 +00:00
if not refresh_token(settings): return False
# Make another attempt
2017-10-23 08:01:02 +00:00
if retry or not refresh_stream(stream, settings, True): return False
2017-10-13 13:40:24 +00:00
else:
2017-10-23 08:01:02 +00:00
# Otherwise log the issue
logger.error(errmsg)
2017-10-13 13:40:24 +00:00
return False
2017-10-23 08:01:02 +00:00
if not 'items' in contents:
logger.error("Could not obtain contents after retrying")
logger.debug(contents)
return False
2017-10-13 13:40:24 +00:00
for raw_entry in contents['items']:
eid = raw_entry['id']
# Create or update data
try:
entry = Entry.objects.get(entry_id=eid)
2017-10-13 21:46:13 +00:00
logger.info("Skipping entry '%s'" % eid)
2017-10-13 13:40:24 +00:00
except Entry.DoesNotExist:
logger.info("Adding entry '%s'" % eid)
entry = Entry()
2017-10-13 21:46:13 +00:00
# Parse the Feedly object
entry = feedparser.parse(entry, raw_entry, stream)
# Persist resulting object
entry.save()
2017-10-14 21:57:39 +00:00
return True