Debian Codegraph @

recipes/ifzm.recipe

raw
1 #!/usr/bin/env python
2 import json
3
4 from calibre.web.feeds.news import BasicNewsRecipe
5
6
7 def absurl(url):
8 if url.startswith('/'):
9 return 'https://www.infzm.com' + url
10
11
12 def json_to_html(raw, link):
13 data = json.loads(raw)
14 data = data['data']['content']
15 title = '<h1 title="{}">'.format(link) + data['subject'] + '</h1>'
16 auth = '<p><b>' + data['author'] + '</b></p>'
17 sub = '<p class="intro"><i>' + data['introtext'] + '</i></p>'
18 body = data['fulltext']
19 return '<html><body><div>' + title + auth + sub + body + '</div></body></html>'
20
21
22 class infzm(BasicNewsRecipe):
23 title = '南方周末'
24 __author__ = 'unkn0wn'
25 description = 'Southern Weekly (infzm.com), founded in 1984, is a Chinese weekly newspaper based in Guangzhou. Download Weekly.'
26 language = 'zh'
27 encoding = 'utf-8'
28 no_stylesheets = True
29 remove_javascript = True
30 ignore_duplicate_articles = {'title', 'url'}
31 remove_empty_feeds = True
32 use_embedded_content = False
33 remove_attributes = ['style', 'height', 'width']
34 masthead_url = 'http://ssimg.kkod.cn/web/02/14227.gif'
35
36 remove_tags = [dict(name=['video', 'svg', 'button'])]
37
38 articles_are_obfuscated = True
39
40 def get_obfuscated_article(self, url):
41 br = self.get_browser()
42 link = url
43 res_link = (
44 link.replace('https://www.infzm.com', 'https://api.infzm.com/mobile')
45 + '?platform=wap&version=1.89.0&machine_id=35458aa29603f2b246636e5492122b50&user_id=&token=&member_type='
46 )
47 # if article is paywalled, add code to figure out machine_id
48 raw = br.open(res_link).read()
49 html = json_to_html(raw, link)
50 return {'data': html, 'url': link}
51
52 extra_css = '''
53 img {display:block; margin:0 auto;}
54 .cm_pic_caption, .cm_pic_author { font-size:small; text-align:center; }
55 '''
56
57 def parse_index(self):
58 index = 'https://www.infzm.com/'
59 sections = ['contents']
60 feeds = []
61 soup = self.index_to_soup(index)
62 for sec in sections:
63 section = sec.capitalize()
64 self.log(section)
65 articles = []
66 for a in soup.findAll('a', attrs={'href': lambda x: x and x.startswith('/' + sec + '/')}):
67 url = absurl(a['href'].split('?')[0])
68 if url in {index + sec + '/', index + sec}:
69 continue
70 title = self.tag_to_string(a)
71 self.log('\t', title, '\n\t\t', url)
72 articles.append({'title': title, 'url': url})
73 if articles:
74 feeds.append((section, articles))
75 return feeds
76
77 def populate_article_metadata(self, article, soup, first):
78 if soup.find(attrs={'class': 'intro'}):
79 article.summary = article.text_summary = self.tag_to_string(soup.find(attrs={'class': 'intro'}))