Exception, wenn Feed nicht erreichbar

This commit is contained in:
Andreas Bielawski 2017-05-08 12:39:04 +02:00
parent 2bd88e767a
commit 00e4acfd35

121
bot.py
View File

@ -318,67 +318,70 @@ def check_rss(bot, job):
last = r.get('pythonbot:rss:' + url + ':last_entry') last = r.get('pythonbot:rss:' + url + ':last_entry')
feed_data = feedparser.parse(url) feed_data = feedparser.parse(url)
if feed_data.status < 400: try:
if not 'title' in feed_data.feed: if feed_data.status < 400:
feed_title = feed_data.feed.link if not 'title' in feed_data.feed:
else: feed_title = feed_data.feed.link
feed_title = feed_data.feed.title
newentr = get_new_entries(last, feed_data.entries)
text = ''
for k2, v2 in enumerate(newentr):
if not 'title' in v2:
title = 'Kein Titel'
else: else:
title = remove_tags(v2.title).lstrip() feed_title = feed_data.feed.title
if not 'link' in v2: newentr = get_new_entries(last, feed_data.entries)
link = feed_data.feed.link text = ''
link_name = link for k2, v2 in enumerate(newentr):
else: if not 'title' in v2:
link = v2.link title = 'Kein Titel'
f = re.search('^https?://feedproxy\.google\.com/~r/(.+?)/.*', link) # feedproxy.google.com
if f:
link_name = f.group(1)
else: else:
link_name = urlparse(link).netloc title = remove_tags(v2.title).lstrip()
link_name = re.sub('^www\d?\.', '', link_name) # www. if not 'link' in v2:
if 'content' in v2: link = feed_data.feed.link
content = remove_tags(v2.content[0].value).lstrip() link_name = link
content = cleanRSS(content) else:
if len(content) > 250: link = v2.link
content = content[0:250] + '...' f = re.search('^https?://feedproxy\.google\.com/~r/(.+?)/.*', link) # feedproxy.google.com
elif 'summary' in v2: if f:
content = remove_tags(v2.summary).lstrip() link_name = f.group(1)
content = cleanRSS(content) else:
if len(content) > 250: link_name = urlparse(link).netloc
content = content[0:250] + '...' link_name = re.sub('^www\d?\.', '', link_name) # www.
else: if 'content' in v2:
content = '' content = remove_tags(v2.content[0].value).lstrip()
# Für 1 Nachricht pro Beitrag, tue dies: content = cleanRSS(content)
# Entferne hier das "text + "... if len(content) > 250:
text = text + '\n<b>' + title + '</b>\n<i>' + feed_title + '</i>\n' + content + '\n<a href="' + link + '">Auf ' + link_name + ' weiterlesen</a>\n' content = content[0:250] + '...'
# ...und setze hier vor jeder Zeile 4 zusätzliche Leerzeichen elif 'summary' in v2:
if text != '': content = remove_tags(v2.summary).lstrip()
if not 'id' in newentr[0]: content = cleanRSS(content)
newlast = newentr[0].link if len(content) > 250:
else: content = content[0:250] + '...'
newlast = newentr[0].id else:
r.set('pythonbot:rss:' + url + ':last_entry', newlast) content = ''
for k2, receiver in enumerate(list(r.smembers(v))): # Für 1 Nachricht pro Beitrag, tue dies:
try: # Entferne hier das "text + "...
bot.sendMessage(receiver, text, parse_mode=ParseMode.HTML, disable_web_page_preview=True) text = text + '\n<b>' + title + '</b>\n<i>' + feed_title + '</i>\n' + content + '\n<a href="' + link + '">Auf ' + link_name + ' weiterlesen</a>\n'
except Unauthorized: # ...und setze hier vor jeder Zeile 4 zusätzliche Leerzeichen
print('Chat ' + receiver + ' existiert nicht mehr, lösche aus Abonnenten-Liste') if text != '':
r.srem(v, receiver) if not 'id' in newentr[0]:
r.delete('pythonbot:rss:' + receiver) newlast = newentr[0].link
except ChatMigrated as e: else:
print('Chat migriert: ' + receiver + ' -> ' + str(e.new_chat_id)) newlast = newentr[0].id
r.srem(v, receiver) r.set('pythonbot:rss:' + url + ':last_entry', newlast)
r.sadd(v, e.new_chat_id) for k2, receiver in enumerate(list(r.smembers(v))):
r.rename('pythonbot:rss:' + receiver, 'pythonbot:rss:' + str(e.new_chat_id)) try:
bot.sendMessage(e.new_chat_id, text, parse_mode=ParseMode.HTML, disable_web_page_preview=True) bot.sendMessage(receiver, text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
else: except Unauthorized:
print('HTTP-Fehler: ' + str(feed_data.status)) print('Chat ' + receiver + ' existiert nicht mehr, lösche aus Abonnenten-Liste')
print('----------') r.srem(v, receiver)
r.delete('pythonbot:rss:' + receiver)
except ChatMigrated as e:
print('Chat migriert: ' + receiver + ' -> ' + str(e.new_chat_id))
r.srem(v, receiver)
r.sadd(v, e.new_chat_id)
r.rename('pythonbot:rss:' + receiver, 'pythonbot:rss:' + str(e.new_chat_id))
bot.sendMessage(e.new_chat_id, text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
else:
print('HTTP-Fehler: ' + str(feed_data.status))
except:
print('Fehler: Seite nicht erreichbar')
print('-----------------------')
def error(bot, update, error): def error(bot, update, error):