Debian Codegraph @

recipes/sciimmunol.recipe

raw
1 #!/usr/bin/env python
2 from calibre.web.feeds.news import BasicNewsRecipe, classes, prefixed_classes
3
4
5 def absurl(url):
6 if url.startswith('/'):
7 url = 'https://www.science.org' + url
8 return url
9
10
11 class scienceadv(BasicNewsRecipe):
12 title = 'Science Immunology'
13 __author__ = 'unkn0wn'
14 description = (
15 'Immunology is in a period of unprecedented expansion and progress. The broad influence of the immune system on diverse '
16 'aspects of health and disease is beginning to be appreciated, and innovative immunological interventions are proving successful '
17 'in the clinic. New tools are revealing the extent of this influence with exceptional precision and reach. Science Immunology '
18 'provides a platform for the most exciting findings in this growing field.'
19 )
20 encoding = 'utf-8'
21 no_javascript = True
22 no_stylesheets = True
23 remove_attributes = ['style', 'height', 'width']
24 masthead_url = 'https://www.science.org/pb-assets/images/logos/sciimmunol-logo-1620488349717.svg'
25 language = 'en'
26 simultaneous_downloads = 1
27 browser_type = 'webengine'
28
29 extra_css = '''
30 .news-article__figure__caption, .calibre-nuked-tag-figcaption, .card-related {font-size:small;}
31 .core-self-citation, .meta-panel__left-content, .news-article__hero__top-meta {font-size:small;}
32 .contributors, .news-article__hero__bottom-meta, #bibliography, #elettersSection {font-size:small;}
33 img {display:block; margin:0 auto;}
34 .core-lede {font-style:italic; color:#202020;}
35 '''
36
37 ignore_duplicate_articles = {'url'}
38
39 keep_only_tags = [
40 classes('meta-panel__left-content news-article__hero__info news-article__hero__figure bodySection'),
41 dict(name='h1', attrs={'property': 'name'}),
42 dict(name='div', **classes('core-lede contributors core-self-citation')),
43 dict(attrs={'data-core-wrapper': 'content'}),
44 ]
45
46 remove_tags = [classes('pb-ad news-article__hero__scroller news-article__version-of-story')]
47
48 recipe_specific_options = {
49 'issue': {
50 'short': 'Enter the Issue Number you want to download\n(Vol/Issue format)',
51 'long': 'For example, 385/6710',
52 'default': 'current',
53 },
54 'res': {
55 'short': 'For hi-res images, select a resolution from the\nfollowing options: 800, 1000, 1200 or 1500',
56 'long': 'This is useful for non e-ink devices, and for a lower file size\nthan the default, use 400 or 300.',
57 'default': '600',
58 },
59 }
60
61 def preprocess_html(self, soup):
62 for img in soup.findAll('img', attrs={'src': True}):
63 if img['src'].endswith('.jpg'):
64 res = '/cdn-cgi/image/width=600'
65 w = self.recipe_specific_options.get('res')
66 if w and isinstance(w, str):
67 res = '/cdn-cgi/image/width=' + w
68 img['src'] = absurl(res + img['src'])
69 for div in soup.findAll('div', attrs={'role': 'paragraph'}):
70 div.name = 'p'
71 return soup
72
73 def postprocess_html(self, soup, first_fetch):
74 bd = soup.find('body')
75 if bd:
76 bd.attrs = {}
77 return soup
78
79 def parse_index(self):
80 issue_url = 'https://www.science.org/toc/sciimmunol/current'
81 d = self.recipe_specific_options.get('issue')
82 if d and isinstance(d, str):
83 issue_url = 'https://www.science.org/toc/sciimmunol/' + d
84
85 soup = self.index_to_soup(issue_url)
86 tme = soup.find(**classes('journal-issue__vol'))
87 if tme:
88 self.timefmt = ' [%s]' % self.tag_to_string(tme).strip().replace('|', ' | ')
89 det = soup.find(attrs={'id': 'journal-issue-details'})
90 if det:
91 self.description = self.tag_to_string(det).strip()
92 cov = soup.find(**classes('cover-image__image'))
93 if cov:
94 self.cover_url = absurl('/cdn-cgi/image/width=800' + cov.img['src'])
95
96 feeds = []
97
98 for sec in soup.findAll('section', **prefixed_classes('toc__section')):
99 name = sec.find(**classes('sidebar-article-title--decorated'))
100 section = self.tag_to_string(name).strip()
101 self.log(section)
102
103 articles = []
104
105 for card in sec.findAll(**classes('card-header')):
106 ti = card.find(**classes('article-title'))
107 url = absurl(ti.a['href'])
108 title = self.tag_to_string(ti).strip()
109 desc = ''
110 meta = card.find(**classes('card-meta'))
111 if meta:
112 desc = self.tag_to_string(meta).strip()
113 self.log(' ', title, '\n\t', desc, '\n\t\t', url)
114 articles.append({'title': title, 'description': desc, 'url': url})
115 feeds.append((section, articles))
116 return feeds