Debian Codegraph @

recipes/natgeo_kids.recipe

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