4
Robo_feed I’m new to python, I was looking to develop a robot that would take the last post of some news portals here in my region, With a survey I came to the conclusion that it would be easier to do with the feedparser module of python. So I’ll try to explain the code...
- I made a class that took care of data entries, site name and url, thus making a list of the same
- I defined a routine in feed_parser() that extracts the data from the variables giving a print for min on the screen of the last posts
- I put a while inside the Try to call the functions The script runs smoothly in the first 2 loops after the stick
As I said I’m young and inexperienced, but with a great desire to learn programming right in Python
code
import feedparser
import time
import base64
timeaut = 15
class Robo_Feed(object):
    def __init__(self):
        self.data = []
        self.sites= {}
        self.allheadlines = []
    def append_url(self, name, site):
        self.sites[name] = (site)
    def encrip(self, published):
        self.data.append(base64.b64encode(published))
def feed_parser():
    def parseRSS(rss_url):
        return feedparser.parse(rss_url)
    def getHeadlines(rss_url):
        robo.headlines = []
        feed = parseRSS(rss_url)
        if (base64.b64encode(feed.entries[0].published)) in str(robo.data):
            return robo.headlines
        else:
            POSTAGEN = '*'+feed['feed']['title']+'*' +"\n" + feed.entries[0].title + "\n" + feed.entries[0].link + "\n\r"
            print (POSTAGEN)
            robo.encrip(feed.entries[0].published)
            return robo.headlines
    for key, url in robo.sites.items():
        robo.allheadlines.extend(getHeadlines(url))
robo = Robo_Feed()
robo.append_url('Oglobo', 'http://oglobo.globo.com/rss.xml?secao=ece_frontpage')
robo.append_url('ZeroHora', 'http://zh.clicrbs.com.br/rs/ultimas-noticias-rss/')
try:
    while True:
        feed_parser()
        time.sleep(time)
except KeyboardInterrupt:
    print "Umteromped"
Error
      File "/home/ubuntu/PycharmProjects/BotTelegran/testes.py", line 38, in <module>
    feed_parser()
  File "/home/ubuntu/PycharmProjects/BotTelegran/testes.py", line 30, in feed_parser
    robo.allheadlines.extend(getHeadlines(url))
  File "/home/ubuntu/PycharmProjects/BotTelegran/testes.py", line 22, in getHeadlines
    if (base64.b64encode(feed.entries[0].published)) in str(robo.data):
IndexError: list index out of range