Skip to content

Commit

Permalink
Fix for some Python exception. By default we are getting at least 10 …
Browse files Browse the repository at this point in the history
…feeds.

Signed-off-by: Petr Hracek <phracek@redhat.com>
  • Loading branch information
phracek committed Nov 23, 2015
1 parent 3663fae commit 47d0379
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions rss.py
@@ -1,6 +1,7 @@
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-

from __future__ import print_function
import codecs
import os
import sys
Expand Down Expand Up @@ -35,7 +36,8 @@
<div class="row">
"""
cnt = 0
for item in feed["items"][:4]:
# Getting at least 4 items in case of some python exceptions.
for item in feed["items"][:6]:
if int(cnt) % 2 == 0:
HTML += u"""
<div class="col-sm-6 blog-headlines">
Expand All @@ -44,15 +46,19 @@
author, title = item.title.split(':', 1)
link = item.links[0]['href']
# Remove image tag from beginning
article_desc = '\n'.join(item.description.split('\n')[1:])
# remove html tags from description
article_desc = re.sub('<[^<]+?>', '', article_desc)
article_desc = re.sub('<', '&lt;', article_desc)
article_desc = re.sub('>', '&gt;', article_desc)
if len(article_desc) > 140:
article_desc = ' '.join(article_desc.split()[0:25]) + '...'
if not article_desc.startswith('<p>'):
article_desc = '<p>%s</p>' % article_desc
try:
article_desc = '\n'.join(item.description.split('\n')[1:])
# remove html tags from description
article_desc = re.sub('<[^<]+?>', '', article_desc)
article_desc = re.sub('<', '&lt;', article_desc)
article_desc = re.sub('>', '&gt;', article_desc)
if len(article_desc) > 140:
article_desc = ' '.join(article_desc.split()[0:25]) + '...'
if not article_desc.startswith('<p>'):
article_desc = '<p>%s</p>' % article_desc
except AttributeError:
print ('AttributeError. Going to next item')
continue
# we got
# Tue, 20 Oct 2015 03:28:42 +0000
# But we expect
Expand All @@ -75,6 +81,9 @@
HTML += u"""
</div>
"""
# Condition if items were collected properly
if int(cnt) > 3:
break
HTML += u"""
</div>
</div>
Expand Down

0 comments on commit 47d0379

Please sign in to comment.