mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-09-29 15:31:08 -04:00
78 lines
3.1 KiB
Plaintext
78 lines
3.1 KiB
Plaintext
from calibre.web.feeds.news import BasicNewsRecipe
|
|
|
|
|
|
def classes(classes):
|
|
q = frozenset(classes.split(' '))
|
|
return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})
|
|
|
|
|
|
class NewYorkTimesBookReview(BasicNewsRecipe):
|
|
title = u'New York Times Book Review'
|
|
language = 'en'
|
|
description = 'The New York Times Sunday Book Review'
|
|
__author__ = 'Kovid Goyal'
|
|
|
|
no_stylesheets = True
|
|
no_javascript = True
|
|
ignore_duplicate_articles = {'title', 'url'}
|
|
encoding = 'utf-8'
|
|
|
|
keep_only_tags = [
|
|
dict(id='story'),
|
|
]
|
|
remove_tags = [
|
|
dict(attrs={'aria-label':'tools'.split()}),
|
|
dict(attrs={'aria-label': lambda x: x and 'New York Times Logo' in x}),
|
|
dict(href='#site-content #site-index'.split()),
|
|
dict(attrs={'aria-hidden':'true'}),
|
|
dict(attrs={'data-videoid':True}),
|
|
dict(name='button meta link'.split()),
|
|
dict(id=lambda x: x and x.startswith('story-ad-')),
|
|
dict(name='head'),
|
|
dict(role='toolbar'),
|
|
dict(name='a', href=lambda x: x and '#story-continues-' in x),
|
|
dict(name='a', href=lambda x: x and '#whats-next' in x),
|
|
dict(id=lambda x: x and 'sharetools-' in x),
|
|
dict(id='newsletter-promo supported-by-ad bottom-wrapper'.split()),
|
|
classes('story-print-citation supported-by accessibility-ad-header visually-hidden bottom-of-article ad nav-wrapper'),
|
|
dict(attrs={'class': lambda x: x and (
|
|
'SectionBar' in x or 'recirculation' in x or 'ResponsiveAd' in x or 'accessibility-visuallyHidden' in x or 'RelatedCoverage' in x)}),
|
|
]
|
|
|
|
def parse_index(self):
|
|
soup = self.index_to_soup(
|
|
'http://www.nytimes.com/pages/books/review/index.html')
|
|
|
|
# Find TOC
|
|
toc = soup.find('div', attrs={'class': 'rank'})
|
|
main_articles, articles = [], []
|
|
feeds = [('Features', main_articles), ('Latest', articles)]
|
|
for h2 in toc.findAll('h2', attrs={'class': 'headline'}):
|
|
a = h2.find('a', href=True)
|
|
if a is not None:
|
|
title = self.tag_to_string(a)
|
|
url = a['href']
|
|
desc = ''
|
|
p = h2.findNextSibling('p', attrs={'class': 'summary'})
|
|
if p:
|
|
desc = self.tag_to_string(p)
|
|
main_articles.append(
|
|
{'title': title, 'url': url, 'description': desc})
|
|
self.log('Found:', title, 'at', url)
|
|
if desc:
|
|
self.log('\t', desc)
|
|
for li in soup.find(id='latest-panel').find('ol').findAll('li'):
|
|
a = li.find('a', attrs={'class': 'story-link'}, href=True)
|
|
if a is None:
|
|
continue
|
|
url = a['href']
|
|
m = a.find(attrs={'class': 'story-meta'})
|
|
title = self.tag_to_string(m.find('h2'))
|
|
desc = self.tag_to_string(m.find(attrs={'class': 'summary'}))
|
|
articles.append({'title': title, 'url': url, 'description': desc})
|
|
self.log('Found:', title, 'at', url)
|
|
if desc:
|
|
self.log('\t', desc)
|
|
|
|
return feeds
|