Debian Codegraph @

recipes/nejm.recipe

raw
1 # -*- coding: utf-8 -*-
2 from calibre.web.feeds.recipes import BasicNewsRecipe
3
4
5 def classes(classes):
6 q = frozenset(classes.split(' '))
7 return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})
8
9
10 class NEJM(BasicNewsRecipe):
11 title = 'New England Journal of Medicine'
12 __author__ = 'Kovid Goyal'
13 description = 'Medical news'
14 timefmt = ' [%d %b, %Y]'
15 needs_subscription = True
16 language = 'en'
17
18 no_stylesheets = True
19 keep_only_tags = [dict(id='content')]
20 remove_tags_after = dict(id='article_references')
21 remove_attributes = ['width', 'height']
22
23 # TO LOGIN
24 def get_browser(self):
25 br = BasicNewsRecipe.get_browser(self)
26 br.open('https://www.nejm.org/action/showLogin?uri=http%3A%2F%2Fwww.nejm.org%2F')
27 br.select_form(name='frmLogin')
28 br['login'] = self.username
29 br['password'] = self.password
30 response = br.submit()
31 raw = response.read()
32 if '>Sign Out<' not in raw:
33 raise Exception('Login failed. Check your username and password')
34 return br
35
36 # TO GET ARTICLE TOC
37 def nejm_get_index(self):
38 return self.index_to_soup('https://www.nejm.org/toc/nejm/medical-journal')
39
40 # To parse article toc
41 def parse_index(self):
42 soup = self.nejm_get_index()
43 feeds = []
44 current_section = None
45 articles = []
46 div = soup.find(**classes('pagefulltext'))
47 for x in div.findAll(name=['h2', 'li']):
48 if x.name == 'h2':
49 if current_section and articles:
50 feeds.append((current_section, articles))
51 current_section = self.tag_to_string(x).strip()
52 articles = []
53 if current_section:
54 self.log(current_section)
55 else:
56 a = x.find('a')
57 if a is None:
58 continue
59 title = self.tag_to_string(a.find('strong')).strip()
60 blurb = a.find(**classes('f-blurb'))
61 desc = ''
62 if blurb is not None:
63 desc = self.tag_to_string(blurb)
64 self.log('\t', title)
65 articles.append({'title': title, 'url': 'https://www.nejm.org' + a['href'], 'description': desc})
66
67 if current_section and articles:
68 feeds.append((current_section, articles))
69
70 return feeds