Debian Codegraph @

recipes/natgeo_traveller.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 from pprint import pformat
4
5 from calibre.web.feeds.news import BasicNewsRecipe, classes
6
7
8 class NatGeo(BasicNewsRecipe):
9 title = 'National Geographic Traveller'
10 description = 'News articles from The National Geographic Traveller, Download Monthly. Inspiring people to care about the planet since 1888'
11 language = 'en'
12 encoding = 'utf8'
13 publisher = 'nationalgeographic.com'
14 category = 'science, nat geo'
15 __author__ = 'unkn0wn'
16 timefmt = ' [%a, %d %b, %Y]'
17 no_stylesheets = True
18 use_embedded_content = False
19 remove_attributes = ['style']
20 remove_javascript = False
21 masthead_url = 'https://i.natgeofe.com/n/e76f5368-6797-4794-b7f6-8d757c79ea5c/ng-logo-2fl.png?w=600&h=600'
22 remove_empty_feeds = True
23 resolve_internal_links = True
24 ignore_duplicate_articles = {'url'}
25
26 recipe_specific_options = {
27 'res': {
28 'short': 'For hi-res images, select a resolution from the\nfollowing options: 800, 1000, 1200 or 1500',
29 'long': 'This is useful for non e-ink devices, and for a lower file size\nthan the default, use 400 or 300.',
30 'default': '600',
31 }
32 }
33
34 @property
35 def natgeo_parser(self):
36 ans = getattr(self, '_natgeo_parser', None)
37 if ans is None:
38 from calibre.live import load_module
39
40 self._natgeo_parser = ans = load_module('calibre.web.site_parsers.natgeo')
41 return ans
42
43 def preprocess_raw_html(self, raw_html, url):
44 return self.natgeo_parser.extract_html(raw_html)
45
46 extra_css = '''
47 blockquote { color:#404040; }
48 .byline, i { font-style:italic; color:#202020; }
49 .cap { font-size:small; }
50 img {display:block; margin:0 auto;}
51 .cred { font-style:italic; font-size:small; color:#404040; }
52 .auth, .time, .sub { font-size:small; color:#5c5c5c; }
53 '''
54
55 def parse_index(self):
56 pages = ['https://www.nationalgeographic.com/travel/topic/national-geographic-traveller-uk']
57
58 feeds = []
59
60 for sec in pages:
61 soup = self.index_to_soup(sec)
62 parsed = self.articles_from_soup(soup)
63 if parsed:
64 feeds += parsed
65 return feeds
66
67 def articles_from_soup(self, soup):
68 ans = {}
69 for article in soup.findAll('article'):
70 a = article.find('a')
71 url = a['href']
72 if url.startswith('/'):
73 url = 'https://www.nationalgeographic.com' + url
74 section = self.tag_to_string(article.find(**classes('SectionLabel')))
75 if section.startswith('Paid Content'):
76 continue
77 title = self.tag_to_string(article.find(**classes('PromoTile__Title--truncated')))
78 articles = ans.setdefault(section, [])
79 articles.append({'title': title, 'url': url})
80 self.log(pformat(ans))
81 return list(ans.items())
82
83 def preprocess_html(self, soup):
84 for h2 in soup.findAll('h2'):
85 h2.name = 'h4'
86 for img in soup.findAll('img', src=True):
87 res = '?w=600'
88 w = self.recipe_specific_options.get('res')
89 if w and isinstance(w, str):
90 res = '?w=' + w
91 img['src'] = img['src'] + res
92 return soup
93
94 def populate_article_metadata(self, article, soup, first):
95 summ = soup.find(attrs={'class': 'byline'})
96 if summ:
97 article.summary = self.tag_to_string(summ)
98 article.text_summary = self.tag_to_string(summ)