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):
"""Scrapes an RSS feed and extract all relevant data from it."""
try:
res = requests.get(feed_url)
res.raise_for_status()
except requests.exceptions.RequestException as e:
# TODO: log the error
return
headers = {"User-Agent": "Feed me RSS"}
res = requests.get(feed_url, headers=headers, timeout=10)
res.raise_for_status()
encoding = re.search(r'encoding="(.*)"', res.text)
if encoding:
encoding = encoding.group(1)
@ -113,14 +111,23 @@ def scrape_factorio(soup):
def update_feed(feed_url):
"""Updates the given feed_id."""
feed = scrape_feed(feed_url)
try:
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))
def update_all_feeds():
"""Updates all feeds being watched."""
for feed_url in config.FEEDS:
#print(feed_url)
update_feed(feed_url)

View File

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