| 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' |
| 28 |
description = 'Book reviews' |
| 29 |
language = 'en_US' |
| 30 |
|
| 31 |
__author__ = 'Kovid Goyal' |
| 32 |
|
| 33 |
no_stylesheets = True |
| 34 |
no_javascript = True |
| 35 |
needs_subscription = True |
| 36 |
|
| 37 |
keep_only_tags = [ |
| 38 |
dict(name='h1'), |
| 39 |
classes('author article-col article-main-content review-items'), |
| 40 |
] |
| 41 |
remove_tags = [ |
| 42 |
classes('inline-ad'), |
| 43 |
] |
| 44 |
remove_tags_after = classes('article-main-content') |
| 45 |
|
| 46 |
def get_browser(self): |
| 47 |
br = BasicNewsRecipe.get_browser(self) |
| 48 |
br.open('https://www.nybooks.com/account/signin/') |
| 49 |
br.select_form(id='loginform') |
| 50 |
br['log'] = self.username |
| 51 |
br['pwd'] = self.password |
| 52 |
br.submit() |
| 53 |
return br |
| 54 |
|
| 55 |
def parse_index(self): |
| 56 |
soup = self.index_to_soup('https://www.nybooks.com/current-issue') |
| 57 |
# from calibre.utils.ipython import ipython |
| 58 |
# ipython({'soup': soup}) |
| 59 |
|
| 60 |
# Find cover |
| 61 |
cover = soup.find('img', attrs={'class': 'border-light-gray', 'src': True}) |
| 62 |
if cover is not None: |
| 63 |
self.cover_url = absurl(cover['src']) |
| 64 |
self.log('Found cover at:', self.cover_url) |
| 65 |
|
| 66 |
# Find date |
| 67 |
div = soup.find('p', **classes('h2')) |
| 68 |
if div is not None: |
| 69 |
text = self.tag_to_string(div) |
| 70 |
self.timefmt = text |
| 71 |
self.log('Issue date:', text) |
| 72 |
|
| 73 |
# Find TOC |
| 74 |
articles = [] |
| 75 |
for h4 in soup.findAll('h2'): |
| 76 |
title = self.tag_to_string(h4).strip() |
| 77 |
a = h4.find('a') |
| 78 |
if a is None: |
| 79 |
continue |
| 80 |
url = absurl(a['href']) |
| 81 |
author = self.tag_to_string(h4.parent.parent.find('a')) |
| 82 |
title = title + ' (%s)' % author |
| 83 |
desc = '' |
| 84 |
div = h4 |
| 85 |
while div.next_sibling: |
| 86 |
div = div.next_sibling |
| 87 |
desc += self.tag_to_string(div).strip() |
| 88 |
self.log('Found article:', title) |
| 89 |
self.log('\t', url) |
| 90 |
self.log('\t', desc) |
| 91 |
articles.append({'title': title, 'url': url, 'date': '', 'description': desc}) |
| 92 |
|
| 93 |
return [('Current Issue', articles)] |
| 94 |
|
| 95 |
def preprocess_html(self, soup): |
| 96 |
for img in soup.findAll('img', attrs={'data-lazy-src': True}): |
| 97 |
img['src'] = img['data-lazy-src'] |
| 98 |
return soup |