added error handling

This commit is contained in:
iou1name 2019-04-07 14:15:07 -04:00
parent ef3da61515
commit cd0383d507
2 changed files with 18 additions and 8 deletions

View File

@ -19,12 +19,10 @@ _r = redis.Redis(
def scrape_feed(feed_url): def scrape_feed(feed_url):
"""Scrapes an RSS feed and extract all relevant data from it.""" """Scrapes an RSS feed and extract all relevant data from it."""
try: headers = {"User-Agent": "Feed me RSS"}
res = requests.get(feed_url) res = requests.get(feed_url, headers=headers, timeout=10)
res.raise_for_status() res.raise_for_status()
except requests.exceptions.RequestException as e:
# TODO: log the error
return
encoding = re.search(r'encoding="(.*)"', res.text) encoding = re.search(r'encoding="(.*)"', res.text)
if encoding: if encoding:
encoding = encoding.group(1) encoding = encoding.group(1)
@ -113,14 +111,23 @@ def scrape_factorio(soup):
def update_feed(feed_url): def update_feed(feed_url):
"""Updates the given feed_id.""" """Updates the given feed_id."""
try:
feed = scrape_feed(feed_url) feed = scrape_feed(feed_url)
except Exception as e:
feed = {
'meta': {
'title': feed_url,
'html_url': feed_url,
'description': ""
},
'error': str(e),
}
_r.set(feed_url, json.dumps(feed)) _r.set(feed_url, json.dumps(feed))
def update_all_feeds(): def update_all_feeds():
"""Updates all feeds being watched.""" """Updates all feeds being watched."""
for feed_url in config.FEEDS: for feed_url in config.FEEDS:
#print(feed_url)
update_feed(feed_url) update_feed(feed_url)

View File

@ -21,6 +21,9 @@
{% for feed in feeds %} {% for feed in feeds %}
<div> <div>
<h3>{{ feed['meta']['title'] }}</h3> <h3>{{ feed['meta']['title'] }}</h3>
{% if feed['error'] %}
<code>Error loading feed: {{ feed['error'] }}</code>
{% endif %}
{% for entry in feed['entries'] %} {% for entry in feed['entries'] %}
<span class="date">{{ entry['date'] }}</span><a href="{{ entry['link'] }}">{{ entry['title'] }}</a><br> <span class="date">{{ entry['date'] }}</span><a href="{{ entry['link'] }}">{{ entry['title'] }}</a><br>
{% endfor %} {% endfor %}