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

  1. #!/usr/bin/env  python
  2. __license__   = 'GPL v3'
  3. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  4. __docformat__ = 'restructuredtext en'
  5.  
  6. from calibre.web.feeds.news import BasicNewsRecipe
  7. import copy
  8.  
  9. class WallStreetJournal(BasicNewsRecipe):
  10.  
  11.     title = 'Wall Street Journal (free)'
  12.     __author__ = 'Kovid Goyal, Sujata Raman, Joshua Oster-Morris, Starson17'
  13.     description = 'News and current affairs'
  14.     language = 'en'
  15.     cover_url           = 'http://dealbreaker.com/images/thumbs/Wall%20Street%20Journal%20A1.JPG'
  16.     max_articles_per_feed = 1000
  17.     timefmt  = ' [%a, %b %d, %Y]'
  18.     no_stylesheets = True
  19.  
  20.     extra_css      = '''h1{color:#093D72 ; font-size:large ; font-family:Georgia,"Century Schoolbook","Times New Roman",Times,serif; }
  21.                     h2{color:#474537; font-family:Georgia,"Century Schoolbook","Times New Roman",Times,serif; font-size:small; font-style:italic;}
  22.                     .subhead{color:gray; font-family:Georgia,"Century Schoolbook","Times New Roman",Times,serif; font-size:small; font-style:italic;}
  23.                     .insettipUnit {color:#666666; font-family:Arial,Sans-serif;font-size:xx-small }
  24.                     .targetCaption{ font-size:x-small; color:#333333; font-family:Arial,Helvetica,sans-serif}
  25.                     .article{font-family :Arial,Helvetica,sans-serif; font-size:x-small}
  26.                     .tagline {color:#333333; font-size:xx-small}
  27.                     .dateStamp {color:#666666; font-family:Arial,Helvetica,sans-serif}
  28.                         h3{color:blue ;font-family:Arial,Helvetica,sans-serif; font-size:xx-small}
  29.                         .byline{color:blue;font-family:Arial,Helvetica,sans-serif; font-size:xx-small}
  30.                         h6{color:#333333; font-family:Georgia,"Century Schoolbook","Times New Roman",Times,serif; font-size:small;font-style:italic; }
  31.                     .paperLocation{color:#666666; font-size:xx-small}'''
  32.  
  33.     remove_tags_before = dict(name='h1')
  34.     remove_tags = [
  35.                     dict(id=["articleTabs_tab_article", "articleTabs_tab_comments", "articleTabs_tab_interactive","articleTabs_tab_video","articleTabs_tab_map","articleTabs_tab_slideshow"]),
  36.                     {'class':['footer_columns','network','insetCol3wide','interactive','video','slideshow','map','insettip','insetClose','more_in', "insetContent", 'articleTools_bottom', 'aTools', "tooltip", "adSummary", "nav-inline"]},
  37.                     dict(name='div', attrs={'data-flash-settings':True}),
  38.                     {'class':['insetContent embedType-interactive insetCol3wide','insetCol6wide','insettipUnit']},
  39.                     dict(rel='shortcut icon'),
  40.                     ]
  41.     remove_tags_after = [dict(id="article_story_body"), {'class':"article story"},]
  42.  
  43.     def postprocess_html(self, soup, first):
  44.         for tag in soup.findAll(name=['table', 'tr', 'td']):
  45.             tag.name = 'div'
  46.  
  47.         for tag in soup.findAll('div', dict(id=["articleThumbnail_1", "articleThumbnail_2", "articleThumbnail_3", "articleThumbnail_4", "articleThumbnail_5", "articleThumbnail_6", "articleThumbnail_7"])):
  48.             tag.extract()
  49.  
  50.         return soup
  51.  
  52.     def wsj_get_index(self):
  53.         return self.index_to_soup('http://online.wsj.com/itp')
  54.  
  55.     def wsj_add_feed(self,feeds,title,url):
  56.         self.log('Found section:', title)
  57.         try:
  58.             if url.endswith('whatsnews'):
  59.                 articles = self.wsj_find_wn_articles(url)
  60.             else:
  61.                 articles = self.wsj_find_articles(url)
  62.         except:
  63.             articles = []
  64.         if articles:
  65.            feeds.append((title, articles))
  66.         return feeds
  67.  
  68.     def parse_index(self):
  69.         soup = self.wsj_get_index()
  70.  
  71.         date = soup.find('span', attrs={'class':'date-date'})
  72.         if date is not None:
  73.             self.timefmt = ' [%s]'%self.tag_to_string(date)
  74.  
  75.         feeds = []
  76.         div = soup.find('div', attrs={'class':'itpHeader'})
  77.         div = div.find('ul', attrs={'class':'tab'})
  78.         for a in div.findAll('a', href=lambda x: x and '/itp/' in x):
  79.             pageone = a['href'].endswith('pageone')
  80.             if pageone:
  81.                title = 'Front Section'
  82.                url = 'http://online.wsj.com' + a['href']
  83.                feeds = self.wsj_add_feed(feeds,title,url)
  84.                title = 'What''s News'
  85.                url = url.replace('pageone','whatsnews')
  86.                feeds = self.wsj_add_feed(feeds,title,url)
  87.             else:
  88.                title = self.tag_to_string(a)
  89.                url = 'http://online.wsj.com' + a['href']
  90.                feeds = self.wsj_add_feed(feeds,title,url)
  91.         return feeds
  92.  
  93.     def wsj_find_wn_articles(self, url):
  94.         soup = self.index_to_soup(url)
  95.         articles = []
  96.  
  97.         whats_news = soup.find('div', attrs={'class':lambda x: x and 'whatsNews-simple' in x})
  98.         if whats_news is not None:
  99.           for a in whats_news.findAll('a', href=lambda x: x and '/article/' in x):
  100.             container = a.findParent(['p'])
  101.             meta = a.find(attrs={'class':'meta_sectionName'})
  102.             if meta is not None:
  103.                 meta.extract()
  104.             title = self.tag_to_string(a).strip()
  105.             url = a['href']
  106.             desc = ''
  107.             if container is not None:
  108.                 desc = self.tag_to_string(container)
  109.  
  110.             articles.append({'title':title, 'url':url,
  111.                 'description':desc, 'date':''})
  112.  
  113.             self.log('\tFound WN article:', title)
  114.  
  115.         return articles
  116.  
  117.     def wsj_find_articles(self, url):
  118.         soup = self.index_to_soup(url)
  119.  
  120.         whats_news = soup.find('div', attrs={'class':lambda x: x and 'whatsNews-simple' in x})
  121.         if whats_news is not None:
  122.            whats_news.extract()
  123.  
  124.         articles = []
  125.  
  126.         flavorarea = soup.find('div', attrs={'class':lambda x: x and 'ahed' in x})
  127.         if flavorarea is not None:
  128.            flavorstory = flavorarea.find('a', href=lambda x: x and x.startswith('/article'))
  129.            if flavorstory is not None:
  130.               flavorstory['class'] = 'mjLinkItem'
  131.               metapage = soup.find('span', attrs={'class':lambda x: x and 'meta_sectionName' in x})
  132.               if metapage is not None:
  133.                  flavorstory.append( copy.copy(metapage) ) #metapage should always be A1 because that should be first on the page
  134.  
  135.         for a in soup.findAll('a', attrs={'class':'mjLinkItem'}, href=True):
  136.             container = a.findParent(['li', 'div'])
  137.             meta = a.find(attrs={'class':'meta_sectionName'})
  138.             if meta is not None:
  139.                 meta.extract()
  140.             title = self.tag_to_string(a).strip() + ' [%s]'%self.tag_to_string(meta)
  141.             url = 'http://online.wsj.com'+a['href']
  142.             desc = ''
  143.             p = container.find('p')
  144.             if p is not None:
  145.                 desc = self.tag_to_string(p)
  146.  
  147.             articles.append({'title':title, 'url':url,
  148.                 'description':desc, 'date':''})
  149.  
  150.             self.log('\tFound article:', title)
  151.  
  152.         return articles
  153.  
  154.     def cleanup(self):
  155.         self.browser.open('http://online.wsj.com/logout?url=http://online.wsj.com')
  156.  
  157.