added error handling
This commit is contained in:
parent
ef3da61515
commit
cd0383d507
19
database.py
19
database.py
|
@ -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)
|
||||
headers = {"User-Agent": "Feed me RSS"}
|
||||
res = requests.get(feed_url, headers=headers, timeout=10)
|
||||
res.raise_for_status()
|
||||
except requests.exceptions.RequestException as e:
|
||||
# TODO: log the error
|
||||
return
|
||||
|
||||
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."""
|
||||
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)
|
||||
|
||||
|
||||
|
|
|
@ -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 %}
|
||||
|
|
Loading…
Reference in New Issue
Block a user