Debian Codegraph @

recipes/jacobinmag.recipe

raw
1 # -*- mode: python -*-
2 # -*- coding: utf-8 -*-
3 # vi: set ft=python :
4
5 __license__ = 'GPL v3'
6 __copyright__ = '2017, Darko Miletic <darko.miletic at gmail.com>'
7 '''
8 www.jacobinmag.com
9 '''
10
11 from calibre.web.feeds.news import BasicNewsRecipe
12
13
14 def classes(classes):
15 q = frozenset(classes.split(' '))
16 return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})
17
18
19 class Jacobinmag(BasicNewsRecipe):
20 title = 'Jacobin'
21 __author__ = 'Darko Miletic'
22 description = 'Jacobin is a leading voice of the American left, offering socialist perspectives on politics, economics, and culture.'
23 publisher = 'Jacobin'
24 category = 'news, politics, USA'
25 oldest_article = 65
26 no_stylesheets = True
27 encoding = 'utf8'
28 use_embedded_content = False
29 language = 'en'
30 remove_empty_feeds = True
31 publication_type = 'magazine'
32 needs_subscription = 'optional'
33 auto_cleanup = False
34 issue_url = None
35 PREFIX = 'https://www.jacobinmag.com'
36 LOGIN = 'https://auth.jacobinmag.com/mini_profile?redirect=https%3A%2F%2Fwww.jacobinmag.com%2F'
37 extra_css = '''
38 body{font-family: Antwerp, 'Times New Roman', Times, serif}
39 img{margin-top:1em; margin-bottom: 1em; display:block}
40 .entry-dek,.entry-author{font-family: Hurme-No3, Futura, sans-serif}
41 '''
42
43 conversion_options = {'comment': description, 'tags': category, 'publisher': publisher, 'language': language}
44
45 remove_tags = [
46 dict(id=['post-header-share', 'post-print']),
47 dict(name='form'),
48 ]
49
50 keep_only_tags = [classes('po__article')]
51
52 def parse_index(self):
53 ans = []
54 articles = []
55 soup = self.index_to_soup('https://www.jacobinmag.com/store/issues')
56 lurl = 'https://jacobinmag.com' + soup.find('a', text='View Issue')['href']
57 feedtitle = 'Articles'
58 self.log('Loading issue from', lurl)
59 soup = self.index_to_soup(lurl)
60
61 # Find cover url
62 di = soup.find('figure', attrs={'class': lambda x: x and '__cover' in x})
63 img = di.find('img')
64 self.cover_url = img['src']
65 # End find cover url
66
67 # Get series title
68 title = soup.find('h1', attrs={'class': lambda x: x and '__heading' in x})
69 feedtitle = self.tag_to_string(title)
70
71 # Scrape article links
72 for section in soup.findAll('div', attrs={'class': lambda x: x and '__content' in x}):
73 for art in section.findAll('article'):
74 h1 = art.find('h1')
75 a = h1.find('a')
76 title = self.tag_to_string(a)
77 url = 'https://jacobinmag.com' + a['href']
78 desc = ''
79 p = art.find('p')
80 if p:
81 desc = self.tag_to_string(p)
82 articles.append({'title': title, 'url': url, 'description': desc})
83 self.log(title, 'at', url)
84 if desc:
85 self.log('\t', desc)
86 if articles:
87 ans.append((feedtitle, articles))
88 return ans
89
90 def get_browser(self):
91 br = BasicNewsRecipe.get_browser(self)
92 br.open(self.PREFIX)
93 if self.username is not None and self.password is not None:
94 br.open(self.LOGIN)
95 br.select_form(nr=0)
96 br['login.email'] = self.username
97 br['login.password'] = self.password
98 br.submit()
99 page = br.response().read()
100 soup = self.index_to_soup(page)
101 div = soup.find('div', attrs={'id': 'redirect-target'})
102 if div:
103 br.open(div['data-redirect'])
104 return br