Debian Codegraph @

recipes/natgeohis.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 from calibre.web.feeds.news import BasicNewsRecipe, classes
4
5
6 class NatGeo(BasicNewsRecipe):
7 title = 'National Geographic History'
8 description = (
9 'From Caesar to Napoleon, the Pyramids to the Parthenon, the Trojan War to the Civil War—National Geographic '
10 'HISTORY draws readers in with more than 5,000 years of people, places, and things to explore. '
11 'Inspiring people to care about the planet since 1888'
12 )
13 language = 'en'
14 encoding = 'utf8'
15 publisher = 'nationalgeographic.com'
16 category = 'science, nat geo'
17 __author__ = 'Kovid Goyal, unkn0wn'
18 timefmt = ' [%a, %d %b, %Y]'
19 no_stylesheets = True
20 use_embedded_content = False
21 remove_attributes = ['style']
22 remove_javascript = False
23 masthead_url = 'https://i.natgeofe.com/n/e76f5368-6797-4794-b7f6-8d757c79ea5c/ng-logo-2fl.png?w=600&h=600'
24 resolve_internal_links = True
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 get_cover_url(self):
56 soup = self.index_to_soup('https://ngsingleissues.nationalgeographic.com/history')
57 wrap = soup.find(attrs={'class': 'product-image-wrapper'})
58 return wrap.img['src']
59
60 def parse_index(self):
61 soup = self.index_to_soup('https://www.nationalgeographic.com/history/history-magazine')
62 ans = []
63 for article in soup.findAll('article'):
64 a = article.find('a')
65 url = a['href']
66 if url.startswith('/'):
67 url = 'https://www.nationalgeographic.com' + url
68 title = self.tag_to_string(article.find(**classes('PromoTile__Title--truncated')))
69 ans.append({'title': title, 'url': url})
70 self.log(title, ' ', url)
71 return [('Articles', ans)]
72
73 def preprocess_html(self, soup):
74 for h2 in soup.findAll('h2'):
75 h2.name = 'h4'
76 for img in soup.findAll('img', src=True):
77 res = '?w=600'
78 w = self.recipe_specific_options.get('res')
79 if w and isinstance(w, str):
80 res = '?w=' + w
81 img['src'] = img['src'] + res
82 return soup
83
84 def populate_article_metadata(self, article, soup, first):
85 summ = soup.find(attrs={'class': 'byline'})
86 if summ:
87 article.summary = self.tag_to_string(summ)
88 article.text_summary = self.tag_to_string(summ)