Debian Codegraph @

recipes/people_daily.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 import os
4 import time
5
6 from calibre.web.feeds.news import BasicNewsRecipe
7
8
9 class AdvancedUserRecipe1277129332(BasicNewsRecipe):
10 title = '人民日报'
11 oldest_article = 2
12 max_articles_per_feed = 100
13 __author__ = 'ceapas'
14
15 publisher = 'people.com.cn'
16 description = 'People Daily Newspaper'
17 language = 'zh'
18 category = 'News, China'
19 remove_javascript = True
20 use_embedded_content = False
21 no_stylesheets = True
22 encoding = 'GB2312'
23 conversion_options = {'linearize_tables': True}
24 masthead_url = 'http://www.people.com.cn/img/2010wb/images/logo.gif'
25
26 recipe_specific_options = {
27 'days': {
28 'short': 'Oldest article to download from this news source. In days ',
29 'long': 'For example, 0.5, gives you articles from the past 12 hours',
30 'default': str(oldest_article),
31 }
32 }
33
34 def __init__(self, *args, **kwargs):
35 BasicNewsRecipe.__init__(self, *args, **kwargs)
36 d = self.recipe_specific_options.get('days')
37 if d and isinstance(d, str):
38 self.oldest_article = float(d)
39
40 feeds = [
41 ('时政', 'http://www.people.com.cn/rss/politics.xml'),
42 ('国际', 'http://www.people.com.cn/rss/world.xml'),
43 ('经济', 'http://www.people.com.cn/rss/finance.xml'),
44 ('体育', 'http://www.people.com.cn/rss/sports.xml'),
45 ('教育', 'http://www.people.com.cn/rss/edu.xml'),
46 ('文化', 'http://www.people.com.cn/rss/culture.xml'),
47 ('社会', 'http://www.people.com.cn/rss/society.xml'),
48 ('传媒', 'http://www.people.com.cn/rss/media.xml'),
49 ('娱乐', 'http://www.people.com.cn/rss/ent.xml'),
50 # (u'汽车', u'http://www.people.com.cn/rss/auto.xml'),
51 ('海峡两岸', 'http://www.people.com.cn/rss/haixia.xml'),
52 # (u'IT频道', u'http://www.people.com.cn/rss/it.xml'),
53 # (u'环保', u'http://www.people.com.cn/rss/env.xml'),
54 # (u'科技', u'http://www.people.com.cn/rss/scitech.xml'),
55 # (u'新农村', u'http://www.people.com.cn/rss/nc.xml'),
56 # (u'天气频道', u'http://www.people.com.cn/rss/weather.xml'),
57 ('生活提示', 'http://www.people.com.cn/rss/life.xml'),
58 ('卫生', 'http://www.people.com.cn/rss/medicine.xml'),
59 # (u'人口', u'http://www.people.com.cn/rss/npmpc.xml'),
60 # (u'读书', u'http://www.people.com.cn/rss/booker.xml'),
61 # (u'食品', u'http://www.people.com.cn/rss/shipin.xml'),
62 # (u'女性新闻', u'http://www.people.com.cn/rss/women.xml'),
63 # (u'游戏', u'http://www.people.com.cn/rss/game.xml'),
64 # (u'家电频道', u'http://www.people.com.cn/rss/homea.xml'),
65 # (u'房产', u'http://www.people.com.cn/rss/house.xml'),
66 # (u'健康', u'http://www.people.com.cn/rss/health.xml'),
67 # (u'科学发展观', u'http://www.people.com.cn/rss/kxfz.xml'),
68 # (u'知识产权', u'http://www.people.com.cn/rss/ip.xml'),
69 # (u'高层动态', u'http://www.people.com.cn/rss/64094.xml'),
70 # (u'党的各项工作', u'http://www.people.com.cn/rss/64107.xml'),
71 # (u'党建聚焦', u'http://www.people.com.cn/rss/64101.xml'),
72 # (u'机关党建', u'http://www.people.com.cn/rss/117094.xml'),
73 # (u'事业党建', u'http://www.people.com.cn/rss/117095.xml'),
74 # (u'国企党建', u'http://www.people.com.cn/rss/117096.xml'),
75 # (u'非公党建', u'http://www.people.com.cn/rss/117097.xml'),
76 # (u'社区党建', u'http://www.people.com.cn/rss/117098.xml'),
77 # (u'高校党建', u'http://www.people.com.cn/rss/117099.xml'),
78 # (u'农村党建', u'http://www.people.com.cn/rss/117100.xml'),
79 # (u'军队党建', u'http://www.people.com.cn/rss/117101.xml'),
80 # (u'时代先锋', u'http://www.people.com.cn/rss/78693.xml'),
81 # (u'网友声音', u'http://www.people.com.cn/rss/64103.xml'),
82 # (u'反腐倡廉', u'http://www.people.com.cn/rss/64371.xml'),
83 # (u'综合报道', u'http://www.people.com.cn/rss/64387.xml'),
84 # (u'中国人大新闻', u'http://www.people.com.cn/rss/14576.xml'),
85 # (u'中国政协新闻', u'http://www.people.com.cn/rss/34948.xml'),
86 ]
87 # keep_only_tags = [
88 # dict(name='p'),
89 # ]
90 remove_tags = [dict(name='div', class_='channel cf')]
91
92 remove_tags_before = [dict(name='div', class_='layout rm_txt cf')]
93 remove_tags_after = [dict(name='div', class_='edit cf')]
94
95 def append_page(self, soup, appendtag, position):
96 pager = soup.find('img', attrs={'src': '/img/next_b.gif'})
97 if pager:
98 nexturl = self.INDEX + pager.a['href']
99 soup2 = self.index_to_soup(nexturl)
100 texttag = soup2.find('div', attrs={'class': 'text_c'})
101 # for it in texttag.findAll(style=True):
102 # del it['style']
103 newpos = len(texttag.contents)
104 self.append_page(soup2, texttag, newpos)
105 texttag.extract()
106 appendtag.insert(position, texttag)
107
108 def skip_ad_pages(self, soup):
109 if 'advertisement' in soup.find('title').string.lower():
110 href = soup.find('a').get('href')
111 return self.browser.open(href).read().decode('GB2312', 'ignore')
112 else:
113 return None
114
115 def preprocess_html(self, soup):
116
117 for item in soup.findAll(style=True):
118 del item['form']
119 self.append_page(soup, soup.body, 3)
120 return soup
121
122 def get_cover_url(self):
123 cover = None
124 os.environ['TZ'] = 'Asia/Shanghai'
125 time.tzset()
126 year = time.strftime('%Y')
127 month = time.strftime('%m')
128 day = time.strftime('%d')
129 cover = 'http://paper.people.com.cn/rmrb/images/' + year + '-' + month + '/' + day + '/01/rmrb' + year + month + day + '01_b.jpg'
130 br = BasicNewsRecipe.get_browser(self)
131 try:
132 br.open(cover)
133 except Exception:
134 self.log('\nCover unavailable: ' + cover)
135 cover = None
136 return cover