added error handling
This commit is contained in:
parent
ef3da61515
commit
cd0383d507
23
database.py
23
database.py
|
@ -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."""
|
||||||
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))
|
_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)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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 %}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user