Debian Codegraph @

recipes/new_york_review_of_books_no_sub.recipe

raw
1 #!/usr/bin/env python
2 __license__ = 'GPL v3'
3 __copyright__ = '2008, Kovid Goyal [email protected]'
4 __docformat__ = 'restructuredtext en'
5
6 '''
7 nybooks.com
8 '''
9
10 from calibre.web.feeds.news import BasicNewsRecipe
11
12
13 def classes(classes):
14 q = frozenset(classes.split(' '))
15 return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})
16
17
18 def absurl(url):
19 if url.startswith('//'):
20 url = 'https:' + url
21 elif url.startswith('/'):
22 url = 'https://www.nybooks.com' + url
23 return url
24
25
26 class NewYorkReviewOfBooks(BasicNewsRecipe):
27 title = 'New York Review of Books (no subscription)'
28 description = 'Book reviews'
29 language = 'en_US'
30
31 __author__ = 'Kovid Goyal'
32
33 no_stylesheets = True
34 no_javascript = True
35
36 keep_only_tags = [
37 dict(name='h1'),
38 classes('author article-col article-main-content review-items'),
39 ]
40 remove_tags = [
41 classes('inline-ad'),
42 ]
43 remove_tags_after = classes('article-main-content')
44
45 def parse_index(self):
46 soup = self.index_to_soup('https://www.nybooks.com/current-issue')
47 # from calibre.utils.ipython import ipython
48 # ipython({'soup': soup})
49
50 # Find cover
51 cover = soup.find('img', attrs={'class': 'border-light-gray', 'src': True})
52 if cover is not None:
53 self.cover_url = absurl(cover['src'])
54 self.log('Found cover at:', self.cover_url)
55
56 # Find date
57 div = soup.find('p', **classes('h2'))
58 if div is not None:
59 text = self.tag_to_string(div)
60 self.timefmt = text
61 self.log('Issue date:', text)
62
63 # Find TOC
64 articles = []
65 for h4 in soup.findAll('h2'):
66 title = self.tag_to_string(h4).strip()
67 a = h4.find('a')
68 if a is None:
69 continue
70 url = absurl(a['href'])
71 author = self.tag_to_string(h4.parent.parent.find('a'))
72 title = title + ' (%s)' % author
73 desc = ''
74 div = h4
75 while div.next_sibling:
76 div = div.next_sibling
77 desc += self.tag_to_string(div).strip()
78 self.log('Found article:', title)
79 self.log('\t', url)
80 self.log('\t', desc)
81 articles.append({'title': title, 'url': url, 'date': '', 'description': desc})
82
83 return [('Current Issue', articles)]
84
85 def preprocess_html(self, soup):
86 for img in soup.findAll('img', attrs={'data-lazy-src': True}):
87 img['src'] = img['data-lazy-src']
88 return soup