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

  1. import re
  2. from datetime import date, timedelta
  3.  
  4. from calibre.web.feeds.recipes import BasicNewsRecipe
  5.  
  6. class MediaDaumRecipe(BasicNewsRecipe):
  7.     title = u'\uBBF8\uB514\uC5B4 \uB2E4\uC74C \uC624\uB298\uC758 \uC8FC\uC694 \uB274\uC2A4'
  8.     description = 'Articles from media.daum.net'
  9.     __author__ = 'trustin'
  10.     language  = 'ko'
  11.     max_articles = 100
  12.  
  13.     timefmt = ''
  14.     masthead_url = 'http://img-media.daum-img.net/2010ci/service_news.gif'
  15.     cover_margins = (18,18,'grey99')
  16.     no_stylesheets = True
  17.     remove_tags_before = dict(id='GS_con')
  18.     remove_tags_after  = dict(id='GS_con')
  19.     remove_tags = [dict(attrs={'class':[
  20.                             'bline',
  21.                             'GS_vod',
  22.                             ]}),
  23.                    dict(id=[
  24.                             'GS_swf_poll',
  25.                             'ad250',
  26.                             ]),
  27.                    dict(name=['script', 'noscript', 'style', 'object'])]
  28.     preprocess_regexps = [
  29.        (re.compile(r'<\s+', re.DOTALL|re.IGNORECASE),
  30.         lambda match: '< '),
  31.        (re.compile(r'(<br[^>]*>[ \t\r\n]*){3,}', re.DOTALL|re.IGNORECASE),
  32.         lambda match: ''),
  33.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*</div>', re.DOTALL|re.IGNORECASE),
  34.         lambda match: '</div>'),
  35.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*</p>', re.DOTALL|re.IGNORECASE),
  36.         lambda match: '</p>'),
  37.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*</td>', re.DOTALL|re.IGNORECASE),
  38.         lambda match: '</td>'),
  39.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*</strong>', re.DOTALL|re.IGNORECASE),
  40.         lambda match: '</strong>'),
  41.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*</b>', re.DOTALL|re.IGNORECASE),
  42.         lambda match: '</b>'),
  43.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*</em>', re.DOTALL|re.IGNORECASE),
  44.         lambda match: '</em>'),
  45.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*</i>', re.DOTALL|re.IGNORECASE),
  46.         lambda match: '</i>'),
  47.        (re.compile(u'\(\uB05D\)[ \t\r\n]*<br[^>]*>.*</div>', re.DOTALL|re.IGNORECASE),
  48.         lambda match: '</div>'),
  49.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*<div', re.DOTALL|re.IGNORECASE),
  50.         lambda match: '<div'),
  51.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*<p', re.DOTALL|re.IGNORECASE),
  52.         lambda match: '<p'),
  53.        (re.compile(r'(<br[^>]*>[ \t\r\n]*)*<table', re.DOTALL|re.IGNORECASE),
  54.         lambda match: '<table'),
  55.        (re.compile(r'<strong>(<br[^>]*>[ \t\r\n]*)*', re.DOTALL|re.IGNORECASE),
  56.         lambda match: '<strong>'),
  57.        (re.compile(r'<b>(<br[^>]*>[ \t\r\n]*)*', re.DOTALL|re.IGNORECASE),
  58.         lambda match: '<b>'),
  59.        (re.compile(r'<em>(<br[^>]*>[ \t\r\n]*)*', re.DOTALL|re.IGNORECASE),
  60.         lambda match: '<em>'),
  61.        (re.compile(r'<i>(<br[^>]*>[ \t\r\n]*)*', re.DOTALL|re.IGNORECASE),
  62.         lambda match: '<i>'),
  63.        (re.compile(u'(<br[^>]*>[ \t\r\n]*)*(\u25B6|\u25CF|\u261E|\u24D2|\(c\))*\[[^\]]*(\u24D2|\(c\)|\uAE30\uC0AC|\uC778\uAE30[^\]]*\uB274\uC2A4)[^\]]*\].*</div>', re.DOTALL|re.IGNORECASE),
  64.         lambda match: '</div>'),
  65.     ]
  66.  
  67.     def parse_index(self):
  68.         today = date.today();
  69.         articles = []
  70.         articles = self.parse_list_page(articles, today)
  71.         articles = self.parse_list_page(articles, today - timedelta(1))
  72.         return [('\uBBF8\uB514\uC5B4 \uB2E4\uC74C \uC624\uB298\uC758 \uC8FC\uC694 \uB274\uC2A4', articles)]
  73.  
  74.  
  75.     def parse_list_page(self, articles, date):
  76.         if len(articles) >= self.max_articles:
  77.             return articles
  78.  
  79.         for page in range(1, 10):
  80.             soup = self.index_to_soup('http://media.daum.net/primary/total/list.html?cateid=100044&date=%(date)s&page=%(page)d' % {'date': date.strftime('%Y%m%d'), 'page': page})
  81.             done = True
  82.             for item in soup.findAll('dl'):
  83.                 dt = item.find('dt', { 'class': 'tit' })
  84.                 dd = item.find('dd', { 'class': 'txt' })
  85.                 if dt is None:
  86.                     break
  87.                 a = dt.find('a', href=True)
  88.                 url = 'http://media.daum.net/primary/total/' + a['href']
  89.                 title = self.tag_to_string(dt)
  90.                 if dd is None:
  91.                     description = ''
  92.                 else:
  93.                     description = self.tag_to_string(dd)
  94.                 articles.append(dict(title=title, description=description, url=url, content=''))
  95.                 done = len(articles) >= self.max_articles
  96.                 if done:
  97.                     break
  98.             if done:
  99.                 break
  100.         return articles
  101.  
  102.  
  103.     def preprocess_html(self, soup):
  104.         return self.strip_anchors(soup)
  105.  
  106.     def strip_anchors(self, soup):
  107.         for para in soup.findAll(True):
  108.             aTags = para.findAll('a')
  109.             for a in aTags:
  110.                 if a.img is None:
  111.                     a.replaceWith(a.renderContents().decode('utf-8','replace'))
  112.         return soup
  113.