Debian Codegraph @

recipes/irish_times_free.recipe

raw
1 #!/usr/bin/env python
2 from datetime import date
3
4 from calibre.web.feeds.news import BasicNewsRecipe, classes
5
6
7 def absurl(url):
8 if url.startswith('/'):
9 return 'https://www.irishtimes.com' + url
10
11
12 class IrishTimes(BasicNewsRecipe):
13 title = 'The Irish Times (free)'
14 __author__ = 'unkn0wn'
15 description = 'Daily news from The Irish Times'
16 language = 'en_IE'
17
18 masthead_url = 'http://www.irishtimes.com/assets/images/generic/website/logo_theirishtimes.png'
19
20 encoding = 'utf-8'
21 max_articles_per_feed = 50
22 remove_empty_feeds = True
23 no_stylesheets = True
24 extra_css = '''
25 img {display:block; margin:0 auto;}
26 em, blockquote { color:#202020; }
27 .b-it-subheadline { font-style:italic; }
28 .calibre-nuked-tag-figcaption, .b-it-byline-block {font-size:small;}
29 '''
30
31 keep_only_tags = [
32 classes('b-it-headline b-it-subheadline b-it-byline-block__text b-it-lead-art__wrapper b-it-article-body'),
33 ]
34
35 remove_tags_after = [
36 classes('b-it-article-body'),
37 ]
38
39 remove_tags = [
40 dict(name=['button', 'svg']),
41 classes('b-top-table-list arcad-feature c-unordered-list b-it-article-body__podcast'),
42 ]
43
44 remove_attributes = ['width', 'height', 'style']
45 ignore_duplicate_articles = {'title', 'url'}
46 resolve_internal_links = True
47
48 def get_cover_url(self):
49 soup = self.index_to_soup('https://www.frontpages.com/ireland-newspapers/')
50 for img in soup.findAll('img', src=True):
51 if 'irish-times' in img['src']:
52 return 'https://www.frontpages.com' + img['src']
53
54 feeds = []
55
56 def parse_index(self):
57 index = 'https://www.irishtimes.com/'
58 sections = [
59 'ireland',
60 'world',
61 'opinion',
62 'politics',
63 'crime-law',
64 'culture',
65 'business',
66 'life-style',
67 'health',
68 'sport',
69 'property',
70 'food',
71 'abroad',
72 'environment',
73 'obituaries',
74 ]
75 feeds = []
76 soup = self.index_to_soup(index)
77 for sec in sections:
78 section = sec.capitalize()
79 self.log(section)
80 articles = []
81 for a in soup.findAll('a', attrs={'href': lambda x: x and x.startswith('/' + sec + '/')}):
82 url = absurl(a['href'].split('?')[0])
83 if url in {index + sec + '/', index + sec}:
84 continue
85 if date.today().strftime('%Y') not in url:
86 continue
87 title = self.tag_to_string(a)
88 self.log('\t', title, '\n\t\t', url)
89 articles.append({'title': title, 'url': url})
90 if articles:
91 feeds.append((section, articles))
92 return feeds
93
94 def preprocess_html(self, soup):
95 h2 = soup.find(**classes('b-it-subheadline'))
96 if h2:
97 h2.name = 'p'
98 for img in soup.findAll('img', attrs={'srcset': True}):
99 img['src'] = img['srcset'].split()[0]
100 return soup
101
102 def populate_article_metadata(self, article, soup, first):
103 desc = soup.find(**classes('b-it-subheadline'))
104 if desc:
105 article.summary = article.text_summary = self.tag_to_string(desc)