Debian Codegraph @

recipes/globaltimes.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 from datetime import datetime, timedelta, timezone
4
5 from calibre.utils.date import parse_date
6 from calibre.web.feeds.news import BasicNewsRecipe, classes
7
8 index = 'https://www.globaltimes.cn/'
9
10
11 class GlobalTimes(BasicNewsRecipe):
12 title = 'Global Times'
13 __author__ = 'unkn0wn'
14 description = 'DISCOVER CHINA, DISCOVER THE WORLD'
15 language = 'en_CN'
16 no_stylesheets = True
17 remove_attributes = ['height', 'width', 'style']
18 ignore_duplicate_articles = {'url'}
19 masthead_url = 'https://www.globaltimes.cn/img/[email protected]'
20 encoding = 'utf-8'
21 remove_empty_feeds = True
22 resolve_internal_links = True
23 oldest_article = 1 # days
24
25 def get_cover_url(self):
26 soup = self.index_to_soup('https://en.kiosko.net/cn/np/cn_global_times.html')
27 return 'https:' + soup.find('img', attrs={'id': 'portada'})['src']
28
29 extra_css = '''
30 .article_column {font-size:small; color:#404040;}
31 .author_share_left, .picture, .with_name_card, .pub_time {font-size:small; color:#202020;}
32 blockquote, em {color:#202020;}
33 '''
34
35 recipe_specific_options = {
36 'days': {
37 'short': 'Oldest article to download from this news source. In days ',
38 'long': 'For example, 0.5, gives you articles from the past 12 hours',
39 'default': str(oldest_article),
40 }
41 }
42
43 def __init__(self, *args, **kwargs):
44 BasicNewsRecipe.__init__(self, *args, **kwargs)
45 d = self.recipe_specific_options.get('days')
46 if d and isinstance(d, str):
47 self.oldest_article = float(d)
48
49 keep_only_tags = [classes('article_column article_title author_share_left article_content')]
50 remove_tags = [classes('author_card')]
51
52 def preprocess_raw_html(self, raw, *a):
53 return raw.replace('<br /><br />', '</p><p>').replace('<br><br>', '</p><p>')
54
55 def preprocess_html(self, soup):
56 h1 = soup.find(attrs={'class': 'article_title'})
57 if h1:
58 h1.name = 'h1'
59 for div in soup.findAll(attrs={'class': 'picture'}):
60 div.name = 'div'
61 p = soup.find(attrs={'class': 'author_share_left'})
62 if p:
63 p.name = 'p'
64 return soup
65
66 def parse_index(self):
67 self.log('\n***\nif this recipe fails, report it on: https://www.mobileread.com/forums/forumdisplay.php?f=228\n***\n')
68 sec_url = index + '{}/index.html'
69
70 section_list = ['china', 'source', 'opinion', 'In-depth', 'world', 'life', 'sport', 'cartoon']
71
72 feeds = []
73
74 for section in section_list:
75 section_title = section.capitalize()
76 section_url = sec_url.format(section)
77 self.log(section_title, section_url)
78 soup = self.index_to_soup(section_url)
79 articles = self.articles_from_soup(soup)
80 if articles:
81 feeds.append((section_title, articles))
82 return feeds
83
84 def articles_from_soup(self, soup):
85 ans = []
86 dt = datetime.today().strftime('%Y%m')
87 for a in soup.findAll('a', attrs={'href': lambda x: x and x.startswith(index + 'page/' + dt + '/')}):
88 if a.find('img'):
89 continue
90 url = a['href']
91 title = self.tag_to_string(a).strip()
92 desc = ''
93 p = a.find_next_sibling('p')
94 if p:
95 desc = self.tag_to_string(p).strip()
96 src_time = a.find_next_sibling(attrs={'class': 'source_time'})
97 if src_time:
98 time = self.tag_to_string(src_time).strip()
99 if '|' in time:
100 time = time.split('|')[1].strip()
101 date = parse_date(time)
102 today = (datetime.now(timezone.utc)).replace(microsecond=0)
103 if (today - date) > timedelta(self.oldest_article):
104 continue
105 self.log('\t', title, '\n\t', desc, '\n\t\t', url)
106 ans.append({'title': title, 'url': url, 'description': desc})
107 return ans