Update Esquire

This commit is contained in:
Kovid Goyal 2019-08-27 19:25:31 +05:30
parent f8f464efc0
commit 62a4659148
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -9,6 +9,18 @@ from calibre.web.feeds.news import BasicNewsRecipe
from css_selectors import Select from css_selectors import Select
def absolutize(url):
if url.startswith('/'):
url = 'https://www.esquire.com' + url
return url
def classes(classes):
q = frozenset(classes.split(' '))
return dict(attrs={
'class': lambda x: x and frozenset(x.split()).intersection(q)})
class Esquire(BasicNewsRecipe): class Esquire(BasicNewsRecipe):
title = 'Esquire' title = 'Esquire'
__author__ = 'Kovid Goyal' __author__ = 'Kovid Goyal'
@ -19,18 +31,11 @@ class Esquire(BasicNewsRecipe):
language = 'en' language = 'en'
keep_only_tags = [ keep_only_tags = [
dict(name='header', attrs={ classes('article-header gallery-header listicle-header listicle-body standard-header standard-body article-body gallery-main-view')
'class': ['gallery-header', 'article-header']}),
dict(attrs={'class': ['gallery-main-view', 'article-body--content']}),
] ]
remove_tags = [ remove_tags = [
dict(attrs={'class': 'article-body--share-container'}), classes('article-body--share-container tags--top image-share share-gallery embedded-image--expand embedded-image--close')
dict(attrs={'class': lambda x: x and 'tags--top' in x}),
dict(attrs={'class': lambda x: x and 'image-share' in x}),
dict(attrs={'class': lambda x: x and 'share-gallery' in x}),
dict(attrs={'class': lambda x: x and 'embedded-image--expand' in x}),
dict(attrs={'class': lambda x: x and 'embedded-image--close' in x}),
] ]
def preprocess_html(self, soup): def preprocess_html(self, soup):
@ -39,40 +44,26 @@ class Esquire(BasicNewsRecipe):
return soup return soup
def parse_index(self): def parse_index(self):
url = 'http://www.esquire.com' url = 'https://www.esquire.com'
root = self.index_to_soup(url, as_tree=True) root = self.index_to_soup(url, as_tree=True)
select = Select(root) select = Select(root)
feeds = defaultdict(list) feeds = defaultdict(list)
for a in select('.custom-promo--title a[href]'): for a in select('.cover-story-marquee a[href]'):
title = self.tag_to_string(a).strip() title = self.tag_to_string(a).strip() or 'Cover Story'
url = a.get('href') url = absolutize(a.get('href'))
if url.startswith('/'): self.log('Cover story:', title, url)
url = 'http://www.esquire.com' + url
feeds['Cover Story'] = [{'title': title, 'url': url}] feeds['Cover Story'] = [{'title': title, 'url': url}]
break break
for story in select('.landing-feed--story-container'): for a in select('a[data-vars-cta]'):
for sec in select('.landing-feed--story-section-name', story): title = self.tag_to_string(a).strip()
section = self.tag_to_string(sec).strip() if not title:
break
else:
continue continue
articles = feeds[section] url = absolutize(a.get('href'))
for a in select('a.landing-feed--story-title[href]', story): section = a.get('data-vars-cta')
title = self.tag_to_string(a).strip() feeds[section].append({'title': title, 'url': url})
url = a.get('href') self.log(title, url)
if url.startswith('/'):
url = 'http://www.esquire.com' + url
break
else:
continue
for div in select('.landing-feed--story-abstract', story):
desc = self.tag_to_string(div).strip()
break
else:
desc = ''
articles.append({'title': title, 'url': url, 'description': desc})
ans = [] ans = []
for sec in sorted(feeds, key=lambda x: (x != 'Cover Story', x)): for sec in sorted(feeds, key=lambda x: (x != 'Cover Story', x)):