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

  1. from calibre.web.feeds.news import BasicNewsRecipe
  2. from datetime import datetime, timedelta
  3.  
  4. class CyNewsLiveRecipe(BasicNewsRecipe):
  5.     __license__  = 'GPL v3'
  6.     __author__ = 'kwetal'
  7.     language = 'en_CY'
  8.     version = 1
  9.  
  10.     title = u'Cyprus Weekly'
  11.     publisher = u'The Cyprus Weekly'
  12.     category = u'News, Newspaper'
  13.     description = u'News from Cyprus'
  14.  
  15.     use_embedded_content = False
  16.     remove_empty_feeds = True
  17.     oldest_article = 7
  18.     max_articles_per_feed = 100
  19.  
  20.     no_stylesheets = True
  21.     remove_javascript = True
  22.  
  23.     pubTime = None
  24.     minTime = None
  25.     articleCount = 0
  26.  
  27.     INDEX = 'http://www.cyprusweekly.com.cy/main/default.aspx'
  28.  
  29.     feeds = []
  30.     feeds.append(('News: Cyprus', 'http://www.cyprusweekly.com.cy/main/92,0,0,0-CYPRUS.aspx'))
  31.     feeds.append(('News: World', 'http://www.cyprusweekly.com.cy/main/78,0,0,0-UKWORLD.aspx'))
  32.     feeds.append(('Sport: Football', 'http://www.cyprusweekly.com.cy/main/82,0,0,0-FOOTBALL.aspx'))
  33.     feeds.append(('Sport: Rugby', 'http://www.cyprusweekly.com.cy/main/83,0,0,0-RUGBY.aspx'))
  34.     feeds.append(('Sport: Cricket', 'http://www.cyprusweekly.com.cy/main/85,0,0,0-CRICKET.aspx'))
  35.     feeds.append(('Sport: Tennis', 'http://www.cyprusweekly.com.cy/main/84,0,0,0-TENNIS.aspx'))
  36.     feeds.append(('Sport: Other', 'http://www.cyprusweekly.com.cy/main/86,0,0,0-OTHER.aspx'))
  37.     feeds.append(('Business: Local', 'http://www.cyprusweekly.com.cy/main/100,0,0,0-LOCAL.aspx'))
  38.     feeds.append(('Business: Foreign', 'http://www.cyprusweekly.com.cy/main/101,0,0,0-FOREIGN.aspx'))
  39.     feeds.append(('Whats On: Places of Interest', 'http://www.cyprusweekly.com.cy/main/123,0,0,0-PLACES-OF-INTEREST.aspx'))
  40.     feeds.append(('Whats On: Going Out', 'http://www.cyprusweekly.com.cy/main/153,0,0,0-GOING-OUT.aspx'))
  41.     feeds.append(('Whats On: Arts & Entertainment', 'http://www.cyprusweekly.com.cy/main/135,0,0,0-ARTS--and-ENTERTAINMENT.aspx'))
  42.     feeds.append(('Whats On: Things To Do', 'http://www.cyprusweekly.com.cy/main/136,0,0,0-THINGS-TO-DO.aspx'))
  43.     feeds.append(('Whats On: Shopping Guide', 'http://www.cyprusweekly.com.cy/main/142,0,0,0-SHOPPING-GUIDE.aspx'))
  44.     feeds.append(('Culture', 'http://www.cyprusweekly.com.cy/main/208,0,0,0-CULTURE.aspx'))
  45.     feeds.append(('Environment', 'http://www.cyprusweekly.com.cy/main/93,0,0,0-ENVIRONMENT.aspx'))
  46.     feeds.append(('Info', 'http://www.cyprusweekly.com.cy/main/91,0,0,0-INFO.aspx'))
  47.  
  48.     keep_only_tags = []
  49.     keep_only_tags.append(dict(name = 'div', attrs = {'class': 'ArticleCategories'}))
  50.  
  51.     extra_css = '''
  52.                 body{font-family:verdana,arial,helvetica,geneva,sans-serif ;}
  53.                 '''
  54.  
  55.     def parse_index(self):
  56.         answer = []
  57.         for feed in self.feeds:
  58.             self.articleCount = 0
  59.             articles = []
  60.             soup = self.index_to_soup(feed[1])
  61.  
  62.             table = soup.find('table', attrs = {'id': 'ctl00_cp_ctl01_listp'})
  63.             if table:
  64.                 self.pubTime = datetime.now()
  65.                 self.minTime = self.pubTime - timedelta(days = self.oldest_article)
  66.  
  67.                 self.find_articles(table, articles)
  68.  
  69.             answer.append((feed[0], articles))
  70.  
  71.         return answer
  72.  
  73.     def postprocess_html(self, soup, first):
  74.         for el in soup.findAll(attrs = {'style': True}):
  75.             del el['style']
  76.  
  77.         for el in soup.findAll('font'):
  78.             el.name = 'div'
  79.             for attr, value in el:
  80.                 del el[attr]
  81.  
  82.         return soup
  83.  
  84.     def find_articles(self, table, articles):
  85.         for div in table.findAll('div', attrs = {'class': 'ListArticle'}):
  86.             el = div.find('div', attrs = {'class': 'ListArticle_T'})
  87.             title = self.tag_to_string(el.a)
  88.             url = self.INDEX + el.a['href']
  89.  
  90.             description = self.tag_to_string(div.find('div', attrs = {'class': 'ListArticle_BODY300'}))
  91.  
  92.             el = div.find('div', attrs = {'class': 'ListArticle_D'})
  93.             if el:
  94.                 dateParts = self.tag_to_string(el).split(' ')
  95.                 monthNames = {'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6,
  96.                               'July': 7, 'August': 8, 'September': 9, 'October': 10, 'November': 11,
  97.                               'December': 12}
  98.                 timeParts = dateParts[3].split(':')
  99.                 self.pubTime = datetime(year = int(dateParts[2]), month = int(monthNames[dateParts[1]]),
  100.                                         day = int(dateParts[0]), hour = int(timeParts[0]),
  101.                                         minute = int(timeParts[1]))
  102.  
  103.             if self.pubTime >= self.minTime and self.articleCount <= self.max_articles_per_feed:
  104.                 articles.append({'title': title, 'date': self.pubTime, 'url': url, 'description': description})
  105.                 self.articleCount += 1
  106.             else:
  107.                 return
  108.  
  109.