# -*- coding: utf-8 -*-
from calibre.web.feeds.news import BasicNewsRecipe

class RevistaPiaui(BasicNewsRecipe):
    title          = u'Revista piau\xed'
    language = 'pt_BR'
    __author__ = u'Eduardo Gustini Simões'
    oldest_article = 31
    max_articles_per_feed = 50
    auto_cleanup = True

    feeds          = [(u'Edi\xe7\xe3o Atual', u'http://revistapiaui.estadao.com.br/feed/rss/edicao-atual.xml')]

    def parse_feeds (self):
        feeds = BasicNewsRecipe.parse_feeds(self)
        for feed in feeds:
           for article in feed.articles[:]:
                 soup = self.index_to_soup('http://revistapiaui.estadao.com.br/feed/rss/edicao-atual.xml')
                 itemTitle = article.title.partition('|')[0].rstrip()
                 item = soup.find(text=itemTitle)
                 articleDescription = item.parent.parent.description.string.partition('<br  />')[2]
                 article.summary = articleDescription

        return feeds

    def populate_article_metadata(self, article, soup, first):
        h2 = soup.find('h2')
        h2.string.replaceWith(h2.string.partition('|')[0].rstrip())
        h2.replaceWith(h2.prettify() +  '<p><em>' + article.summary + '</em></p><p><em>' + ' posted at ' + article.localtime.strftime('%d-%m-%Y') + '</em></p>')
