home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_3876 < prev    next >
Encoding:
Text File  |  2010-09-30  |  4.7 KB  |  126 lines

  1. #!/usr/bin/env  python
  2. # -*- coding: utf-8 -*-
  3.  
  4. __license__   = 'GPL v3'
  5. __copyright__ = '2010, Constantin Hofstetter <consti at consti.de>'
  6. __version__   = '0.95'
  7.  
  8. ''' http://brandeins.de - Wirtschaftsmagazin '''
  9. import re
  10. import string
  11. from calibre.web.feeds.recipes import BasicNewsRecipe
  12.  
  13. class BrandEins(BasicNewsRecipe):
  14.  
  15.   title = u'Brand Eins'
  16.   __author__ = 'Constantin Hofstetter'
  17.   description = u'Wirtschaftsmagazin'
  18.   publisher ='brandeins.de'
  19.   category = 'politics, business, wirtschaft, Germany'
  20.   use_embedded_content = False
  21.   lang = 'de-DE'
  22.   no_stylesheets = True
  23.   encoding = 'utf-8'
  24.   language = 'de'
  25.  
  26.   # 2 is the last full magazine (default)
  27.   # 1 is the newest (but not full)
  28.   # 3 is one before 2 etc.
  29.   which_ausgabe = 2
  30.  
  31.   keep_only_tags = [dict(name='div', attrs={'id':'theContent'}), dict(name='div', attrs={'id':'sidebar'}), dict(name='div', attrs={'class':'intro'}), dict(name='p', attrs={'class':'bodytext'}), dict(name='div', attrs={'class':'single_image'})]
  32.  
  33.   '''
  34.   brandeins.de
  35.   '''
  36.  
  37.   def postprocess_html(self, soup,first):
  38.  
  39.     # Move the image of the sidebar right below the h3
  40.     first_h3 = soup.find(name='div', attrs={'id':'theContent'}).find('h3')
  41.     for imgdiv in soup.findAll(name='div', attrs={'class':'single_image'}):
  42.       if len(first_h3.findNextSiblings('div', {'class':'intro'})) >= 1:
  43.         # first_h3.parent.insert(2, imgdiv)
  44.         first_h3.findNextSiblings('div', {'class':'intro'})[0].parent.insert(4, imgdiv)
  45.       else:
  46.         first_h3.parent.insert(2, imgdiv)
  47.  
  48.     # Now, remove the sidebar
  49.     soup.find(name='div', attrs={'id':'sidebar'}).extract()
  50.  
  51.     # Remove the rating-image (stars) from the h3
  52.     for img in first_h3.findAll(name='img'):
  53.         img.extract()
  54.  
  55.     # Mark the intro texts as italic
  56.     for div in soup.findAll(name='div', attrs={'class':'intro'}):
  57.       for p in div.findAll('p'):
  58.         content = self.tag_to_string(p)
  59.         new_p = "<p><i>"+ content +"</i></p>"
  60.         p.replaceWith(new_p)
  61.  
  62.     return soup
  63.  
  64.   def parse_index(self):
  65.     feeds = []
  66.  
  67.     archive = "http://www.brandeins.de/archiv.html"
  68.  
  69.     soup = self.index_to_soup(archive)
  70.     latest_jahrgang = soup.findAll('div', attrs={'class': re.compile(r'\bjahrgang-latest\b') })[0].findAll('ul')[0]
  71.     pre_latest_issue = latest_jahrgang.findAll('a')[len(latest_jahrgang.findAll('a'))-self.which_ausgabe]
  72.     url = pre_latest_issue.get('href', False)
  73.     # Get the title for the magazin - build it out of the title of the cover - take the issue and year;
  74.     self.title = "Brand Eins "+ re.search(r"(?P<date>\d\d\/\d\d\d\d+)", pre_latest_issue.find('img').get('title', False)).group('date')
  75.     url = 'http://brandeins.de/'+url
  76.  
  77.     # url = "http://www.brandeins.de/archiv/magazin/tierisch.html"
  78.     titles_and_articles = self.brand_eins_parse_latest_issue(url)
  79.     if titles_and_articles:
  80.       for title, articles in titles_and_articles:
  81.         feeds.append((title, articles))
  82.     return feeds
  83.  
  84.   def brand_eins_parse_latest_issue(self, url):
  85.     soup = self.index_to_soup(url)
  86.     article_lists = [soup.find('div', attrs={'class':'subColumnLeft articleList'}), soup.find('div', attrs={'class':'subColumnRight articleList'})]
  87.  
  88.     titles_and_articles = []
  89.     current_articles = []
  90.     chapter_title = "Editorial"
  91.     self.log('Found Chapter:', chapter_title)
  92.  
  93.     # Remove last list of links (thats just the impressum and the 'gewinnspiel')
  94.     article_lists[1].findAll('ul')[len(article_lists[1].findAll('ul'))-1].extract()
  95.  
  96.     for article_list in article_lists:
  97.       for chapter in article_list.findAll('ul'):
  98.         if len(chapter.findPreviousSiblings('h3')) >= 1:
  99.           new_chapter_title = string.capwords(self.tag_to_string(chapter.findPreviousSiblings('h3')[0]))
  100.           if new_chapter_title != chapter_title:
  101.             titles_and_articles.append([chapter_title, current_articles])
  102.             current_articles = []
  103.             self.log('Found Chapter:', new_chapter_title)
  104.           chapter_title = new_chapter_title
  105.         for li in chapter.findAll('li'):
  106.           a = li.find('a', href = True)
  107.           if a is None:
  108.             continue
  109.           title = self.tag_to_string(a)
  110.           url = a.get('href', False)
  111.           if not url or not title:
  112.             continue
  113.           url = 'http://brandeins.de/'+url
  114.           if len(a.parent.findNextSiblings('p')) >= 1:
  115.             description = self.tag_to_string(a.parent.findNextSiblings('p')[0])
  116.           else:
  117.             description = ''
  118.  
  119.           self.log('\t\tFound article:', title)
  120.           self.log('\t\t\t', url)
  121.           self.log('\t\t\t', description)
  122.  
  123.           current_articles.append({'title': title, 'url': url, 'description': description, 'date':''})
  124.     titles_and_articles.append([chapter_title, current_articles])
  125.     return titles_and_articles
  126.