Debian Codegraph @

recipes/scisignaling.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 Signaling'
13 __author__ = 'unkn0wn'
14 description = (
15 'Science Signaling offers researchers the most up-to-date resource for groundbreaking research and commentary '
16 'in the dynamic field of cellular signaling. From basic science to design of therapeutics and from molecules to '
17 'networks and systems design, this weekly e-resource keeps your researchers, faculty, and students ahead of the curve.'
18 )
19 encoding = 'utf-8'
20 no_javascript = True
21 no_stylesheets = True
22 remove_attributes = ['style', 'height', 'width']
23 masthead_url = 'https://www.science.org/pb-assets/images/logos/signaling-logo-1620488350150.svg'
24 language = 'en'
25 simultaneous_downloads = 1
26 browser_type = 'webengine'
27
28 extra_css = '''
29 .news-article__figure__caption, .calibre-nuked-tag-figcaption, .card-related {font-size:small;}
30 .core-self-citation, .meta-panel__left-content, .news-article__hero__top-meta {font-size:small;}
31 .contributors, .news-article__hero__bottom-meta, #bibliography, #elettersSection {font-size:small;}
32 img {display:block; margin:0 auto;}
33 .core-lede {font-style:italic; color:#202020;}
34 '''
35
36 ignore_duplicate_articles = {'url'}
37
38 keep_only_tags = [
39 classes('meta-panel__left-content news-article__hero__info news-article__hero__figure bodySection'),
40 dict(name='h1', attrs={'property': 'name'}),
41 dict(name='div', **classes('core-lede contributors core-self-citation')),
42 dict(attrs={'data-core-wrapper': 'content'}),
43 ]
44
45 remove_tags = [classes('pb-ad news-article__hero__scroller news-article__version-of-story')]
46
47 recipe_specific_options = {
48 'issue': {
49 'short': 'Enter the Issue Number you want to download\n(Vol/Issue format)',
50 'long': 'For example, 385/6710',
51 'default': 'current',
52 },
53 'res': {
54 'short': 'For hi-res images, select a resolution from the\nfollowing options: 800, 1000, 1200 or 1500',
55 'long': 'This is useful for non e-ink devices, and for a lower file size\nthan the default, use 400 or 300.',
56 'default': '600',
57 },
58 }
59
60 def preprocess_html(self, soup):
61 for img in soup.findAll('img', attrs={'src': True}):
62 if img['src'].endswith('.jpg'):
63 res = '/cdn-cgi/image/width=600'
64 w = self.recipe_specific_options.get('res')
65 if w and isinstance(w, str):
66 res = '/cdn-cgi/image/width=' + w
67 img['src'] = absurl(res + img['src'])
68 for div in soup.findAll('div', attrs={'role': 'paragraph'}):
69 div.name = 'p'
70 return soup
71
72 def postprocess_html(self, soup, first_fetch):
73 bd = soup.find('body')
74 if bd:
75 bd.attrs = {}
76 return soup
77
78 def parse_index(self):
79 issue_url = 'https://www.science.org/toc/signaling/current'
80 d = self.recipe_specific_options.get('issue')
81 if d and isinstance(d, str):
82 issue_url = 'https://www.science.org/toc/signaling/' + d
83
84 soup = self.index_to_soup(issue_url)
85 tme = soup.find(**classes('journal-issue__vol'))
86 if tme:
87 self.timefmt = ' [%s]' % self.tag_to_string(tme).strip().replace('|', ' | ')
88 det = soup.find(attrs={'id': 'journal-issue-details'})
89 if det:
90 self.description = self.tag_to_string(det).strip()
91 cov = soup.find(**classes('cover-image__image'))
92 if cov:
93 self.cover_url = absurl('/cdn-cgi/image/width=800' + cov.img['src'])
94
95 feeds = []
96
97 for sec in soup.findAll('section', **prefixed_classes('toc__section')):
98 name = sec.find(**classes('sidebar-article-title--decorated'))
99 section = self.tag_to_string(name).strip()
100 self.log(section)
101
102 articles = []
103
104 for card in sec.findAll(**classes('card-header')):
105 ti = card.find(**classes('article-title'))
106 url = absurl(ti.a['href'])
107 title = self.tag_to_string(ti).strip()
108 desc = ''
109 meta = card.find(**classes('card-meta'))
110 if meta:
111 desc = self.tag_to_string(meta).strip()
112 self.log(' ', title, '\n\t', desc, '\n\t\t', url)
113 articles.append({'title': title, 'description': desc, 'url': url})
114 feeds.append((section, articles))
115 return feeds