home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_280 / folhadesaopaulo.recipe < prev    next >
Text File  |  2011-09-09  |  8KB  |  166 lines

  1. # -*- coding: utf-8 -*-
  2. from calibre.web.feeds.news import BasicNewsRecipe
  3. from datetime import datetime, timedelta
  4. from calibre.ebooks.BeautifulSoup import Tag,BeautifulSoup
  5. from calibre.utils.magick import Image, PixelWand
  6. from urllib2 import Request, urlopen, URLError
  7.  
  8. class FolhaOnline(BasicNewsRecipe):
  9.     THUMBALIZR_API        = '' # ---->Get your at http://www.thumbalizr.com/ and put here
  10.     LANGUAGE              = 'pt_br'
  11.     language = 'pt'
  12.     LANGHTM               = 'pt-br'
  13.     ENCODING              = 'cp1252'
  14.     ENCHTM                = 'iso-8859-1'
  15.     directionhtm          = 'ltr'
  16.     requires_version      = (0,7,47)
  17.     news                  = True
  18.  
  19.     title                 = u'Folha de S\xE3o Paulo'
  20.     __author__            = 'Euler Alves and Alex Mitrani'
  21.     description           = u'Brazilian news from Folha de S\xE3o Paulo'
  22.     publisher             = u'Folha de S\xE3o Paulo'
  23.     category              = 'news, rss'
  24.  
  25.     oldest_article        = 4
  26.     max_articles_per_feed = 100
  27.     summary_length        = 1000
  28.  
  29.     remove_javascript     = True
  30.     no_stylesheets        = True
  31.     use_embedded_content  = False
  32.     remove_empty_feeds    = True
  33.     timefmt               = ' [%d %b %Y (%a)]'
  34.  
  35.     html2lrf_options      = [
  36.                             '--comment', description
  37.                             ,'--category', category
  38.                             ,'--publisher', publisher
  39.     ]
  40.  
  41.     html2epub_options     = 'publisher="' + publisher + '"\ncomments="' + description + '"\ntags="' + category + '"'
  42.  
  43.     hoje                  = datetime.now()
  44.     pubdate               = hoje.strftime('%a, %d %b')
  45.     if hoje.hour<6:
  46.         hoje = hoje-timedelta(days=1)
  47.     CAPA                  = 'http://www1.folha.uol.com.br/fsp/images/cp'+hoje.strftime('%d%m%Y')+'.jpg'
  48.     SCREENSHOT            = 'http://www1.folha.uol.com.br/'
  49.     cover_margins         = (0,0,'white')
  50.     masthead_url          = 'http://f.i.uol.com.br/fsp/furniture/images/lgo-fsp-430x50-ffffff.gif'
  51.  
  52.     keep_only_tags      = [dict(name='div', attrs={'id':'articleNew'})]
  53.     remove_tags         = [
  54.                         dict(name='div',
  55.                             attrs={'id':[
  56.                                 'articleButton'
  57.                                 ,'bookmarklets'
  58.                                 ,'ad-180x150-1'
  59.                                 ,'contextualAdsArticle'
  60.                                 ,'articleEnd'
  61.                                 ,'articleComments'
  62.                             ]})
  63.                         ,dict(name='div',
  64.                             attrs={'class':[
  65.                             'openBox adslibraryArticle'
  66.            ,'toolbar'
  67.                             ]})
  68.  
  69.                         ,dict(name='a')
  70.                         ,dict(name='iframe')
  71.                         ,dict(name='link')
  72.                         ,dict(name='script')
  73.         ,dict(name='li')
  74.     ]
  75.     remove_tags_after =     dict(name='div',attrs={'id':'articleEnd'})
  76.  
  77.     feeds = [
  78.     (u'Em cima da hora', u'http://feeds.folha.uol.com.br/emcimadahora/rss091.xml')
  79.     ,(u'Cotidiano', u'http://feeds.folha.uol.com.br/folha/cotidiano/rss091.xml')
  80.     ,(u'Brasil', u'http://feeds.folha.uol.com.br/folha/brasil/rss091.xml')
  81.     ,(u'Mundo', u'http://feeds.folha.uol.com.br/mundo/rss091.xml')
  82.     ,(u'Poder', u'http://feeds.folha.uol.com.br/poder/rss091.xml')
  83.     ,(u'Mercado', u'http://feeds.folha.uol.com.br/folha/dinheiro/rss091.xml')
  84.     ,(u'Saber', u'http://feeds.folha.uol.com.br/folha/educacao/rss091.xml')
  85.     ,(u'Tec', u'http://feeds.folha.uol.com.br/folha/informatica/rss091.xml')
  86.     ,(u'Ilustrada', u'http://feeds.folha.uol.com.br/folha/ilustrada/rss091.xml')
  87.     ,(u'Ambiente', u'http://feeds.folha.uol.com.br/ambiente/rss091.xml')
  88.     ,(u'Bichos', u'http://feeds.folha.uol.com.br/bichos/rss091.xml')
  89.     ,(u'Ci\xEAncia', u'http://feeds.folha.uol.com.br/ciencia/rss091.xml')
  90.     ,(u'Equil\xEDbrio e Sa\xFAde', u'http://feeds.folha.uol.com.br/equilibrioesaude/rss091.xml')
  91.     ,(u'Turismo', u'http://feeds.folha.uol.com.br/folha/turismo/rss091.xml')
  92.     ,(u'Esporte', u'http://feeds.folha.uol.com.br/folha/esporte/rss091.xml')
  93.     ,(u'Zapping', u'http://feeds.folha.uol.com.br/colunas/zapping/rss091.xml')
  94.     ,(u'Cida Santos', u'http://feeds.folha.uol.com.br/colunas/cidasantos/rss091.xml')
  95.     ,(u'Cl├│vis Rossi', u'http://feeds.folha.uol.com.br/colunas/clovisrossi/rss091.xml')
  96.     ,(u'Eliane Cantanh├¬de', u'http://feeds.folha.uol.com.br/colunas/elianecantanhede/rss091.xml')
  97.     ,(u'Fernando Canzian', u'http://feeds.folha.uol.com.br/colunas/fernandocanzian/rss091.xml')
  98.     ,(u'Gilberto Dimenstein', u'http://feeds.folha.uol.com.br/colunas/gilbertodimenstein/rss091.xml')
  99.     ,(u'H├⌐lio Schwartsman', u'http://feeds.folha.uol.com.br/colunas/helioschwartsman/rss091.xml')
  100.     ,(u'Humberto Luiz Peron', u'http://feeds.folha.uol.com.br/colunas/futebolnarede/rss091.xml')
  101.     ,(u'Jo├úo Pereira Coutinho', u'http://feeds.folha.uol.com.br/colunas/joaopereiracoutinho/rss091.xml')
  102.     ,(u'Jos├⌐ Antonio Ramalho', u'http://feeds.folha.uol.com.br/colunas/canalaberto/rss091.xml')
  103.     ,(u'Kennedy Alencar', u'http://feeds.folha.uol.com.br/colunas/kennedyalencar/rss091.xml')
  104.     ,(u'Luiz Caversan', u'http://feeds.folha.uol.com.br/colunas/luizcaversan/rss091.xml')
  105.     ,(u'Luiz Rivoiro', u'http://feeds.folha.uol.com.br/colunas/paiepai/rss091.xml')
  106.     ,(u'Marcelo Leite', u'http://feeds.folha.uol.com.br/colunas/marceloleite/rss091.xml')
  107.     ,(u'S├⌐rgio Malbergier', u'http://feeds.folha.uol.com.br/colunas/sergiomalbergier/rss091.xml')
  108.     ,(u'Sylvia Colombo', u'http://feeds.folha.uol.com.br/colunas/sylviacolombo/rss091.xml')
  109.     ,(u'Valdo Cruz', u'http://feeds.folha.uol.com.br/colunas/valdocruz/rss091.xml')
  110.     ]
  111.  
  112.  
  113.     conversion_options = {
  114.     'title'            : title
  115.     ,'comments'        : description
  116.     ,'publisher'       : publisher
  117.     ,'tags'            : category
  118.     ,'language'        : LANGUAGE
  119.     ,'linearize_tables': True
  120.     }
  121.  
  122.     def preprocess_html(self, soup):
  123.         for item in soup.findAll(style=True):
  124.             del item['style']
  125.         if not soup.find(attrs={'http-equiv':'Content-Language'}):
  126.             meta0 = Tag(soup,'meta',[("http-equiv","Content-Language"),("content",self.LANGHTM)])
  127.             soup.head.insert(0,meta0)
  128.         if not soup.find(attrs={'http-equiv':'Content-Type'}):
  129.             meta1 = Tag(soup,'meta',[("http-equiv","Content-Type"),("content","text/html; charset="+self.ENCHTM)])
  130.             soup.head.insert(0,meta1)
  131.         return soup
  132.  
  133.     def postprocess_html(self, soup, first):
  134.         #process all the images. assumes that the new html has the correct path
  135.         for tag in soup.findAll(lambda tag: tag.name.lower()=='img' and tag.has_key('src')):
  136.             iurl = tag['src']
  137.             img = Image()
  138.             img.open(iurl)
  139.             width, height = img.size
  140.             print 'img is: ', iurl, 'width is: ', width, 'height is: ', height
  141.             if img < 0:
  142.                 raise RuntimeError('Out of memory')
  143.             pw = PixelWand()
  144.             if( width > height and width > 590) :
  145.                 print 'Rotate image'
  146.                 img.rotate(pw, -90)
  147.                 img.save(iurl)
  148.         return soup
  149.  
  150.     def get_cover_url(self):
  151.         cover_url      = self.CAPA
  152.         pedido         = Request(self.CAPA)
  153.         pedido.add_header('User-agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; '+self.LANGHTM+'; userid='+self.THUMBALIZR_API+') Calibre/0.8.47 (like Gecko)')
  154.         pedido.add_header('Accept-Charset',self.ENCHTM)
  155.         pedido.add_header('Referer',self.SCREENSHOT)
  156.         try:
  157.             resposta   = urlopen(pedido)
  158.             soup       = BeautifulSoup(resposta)
  159.             cover_item = soup.find('body')
  160.             if cover_item:
  161.                 cover_url='http://api.thumbalizr.com/?api_key='+self.THUMBALIZR_API+'&url='+self.SCREENSHOT+'&width=600&quality=90'
  162.             return cover_url
  163.         except URLError:
  164.             cover_url='http://api.thumbalizr.com/?api_key='+self.THUMBALIZR_API+'&url='+self.SCREENSHOT+'&width=600&quality=90'
  165.             return cover_url
  166.