Debian Codegraph @

recipes/zaobao.recipe

raw
1 #!/usr/bin/env python
2
3 """
4 zaobao.com.sg
5 """
6
7 from calibre.web.feeds.news import BasicNewsRecipe, classes
8
9
10 class ZAOBAO(BasicNewsRecipe):
11 title = '联合早报'
12 __author__ = 'unkn0wn'
13 description = '联合早报 是全球华文用户信任的媒体,每天即时为你提供国际最新新闻和热门新闻。从财经、体育、生活娱乐资讯到评论分析,尽在zaobao.com.sg。'
14 no_stylesheets = True
15 language = 'zh'
16 encoding = 'utf-8'
17 masthead_url = 'https://upload.wikimedia.org/wikipedia/en/2/29/Lianhe_Zaobao_new_logo.svg'
18 remove_javascript = True
19 ignore_duplicate_articles = {'url'}
20 remove_attributes = ['width', 'height', 'style']
21 resolve_internal_links = True
22 remove_empty_feeds = True
23
24 extra_css = '''
25 .calibre-nuked-tag-figcaption {font-size:small; text-align:center; }
26 img {display:block; margin:0 auto;}
27 '''
28
29 def get_cover_url(self):
30 soup = self.index_to_soup('https://frontpages.freedomforum.org/newspapers/sing_lz-Lianhe_Zaobao')
31 return soup.find(
32 'img',
33 attrs={
34 'alt': 'Front Page Image',
35 'src': lambda x: x and x.endswith('front-page-large.jpg'),
36 },
37 )['src'].replace('-large', '-medium')
38
39 keep_only_tags = [
40 dict(name='article', attrs={'class': 'max-w-full'}),
41 ]
42
43 remove_tags_after = [classes('articleBody')]
44
45 remove_tags = [classes('bff-google-ad bff-recommend-article')]
46
47 def parse_index(self):
48 index = 'https://www.zaobao.com.sg'
49 sections = ['realtime', 'news', 'forum', 'wencui', 'lifestyle', 'entertainment']
50
51 soup = self.index_to_soup(index)
52
53 feeds = []
54
55 for sec in sections:
56 sectn = sec.capitalize()
57 self.log(sectn)
58
59 articles = []
60
61 for a in soup.findAll(
62 'a',
63 attrs={
64 'class': 'article-type-link',
65 'href': lambda x: x and x.startswith('/' + sec),
66 },
67 ):
68 if a.find('img'):
69 continue
70 title = self.tag_to_string(a)
71 url = index + a['href']
72 if url == index + '/' + sec:
73 continue
74 self.log('\t', title, '\n\t\t', url)
75 articles.append({'title': title, 'url': url})
76 if articles:
77 feeds.append((sectn, articles))
78 return feeds