Debian Codegraph @

recipes/esquire.recipe

raw
1 __license__ = 'GPL v3'
2
3 '''
4 www.esquire.com
5 '''
6 from collections import defaultdict
7
8 from css_selectors import Select
9
10 from calibre.web.feeds.news import BasicNewsRecipe
11
12
13 def absolutize(url):
14 if url.startswith('/'):
15 url = 'https://www.esquire.com' + url
16 return url
17
18
19 def classes(classes):
20 q = frozenset(classes.split(' '))
21 return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})
22
23
24 class Esquire(BasicNewsRecipe):
25 title = 'Esquire'
26 __author__ = 'Kovid Goyal'
27 description = 'Esquire: Man at His Best'
28 publisher = 'Hearst Communications, Inc.'
29 no_stylesheets = True
30 encoding = 'utf-8'
31 language = 'en'
32
33 keep_only_tags = [classes('article-header gallery-header listicle-header listicle-body standard-header standard-body article-body gallery-main-view')]
34
35 remove_tags = [classes('article-body--share-container tags--top image-share share-gallery embedded-image--expand embedded-image--close')]
36
37 def preprocess_html(self, soup):
38 for img in soup.findAll('img', attrs={'data-src': True}):
39 img['src'] = img['data-src']
40 return soup
41
42 def parse_index(self):
43 url = 'https://www.esquire.com'
44 root = self.index_to_soup(url, as_tree=True)
45 select = Select(root)
46 feeds = defaultdict(list)
47
48 for a in select('.cover-story-marquee a[href]'):
49 title = self.tag_to_string(a).strip() or 'Cover Story'
50 url = absolutize(a.get('href'))
51 self.log('Cover story:', title, url)
52 feeds['Cover Story'] = [{'title': title, 'url': url}]
53 break
54
55 for a in select('a[data-vars-cta]'):
56 title = self.tag_to_string(a).strip()
57 if not title:
58 continue
59 url = absolutize(a.get('href'))
60 section = a.get('data-vars-cta')
61 feeds[section].append({'title': title, 'url': url})
62 self.log(title, url)
63
64 ans = []
65 for sec in sorted(feeds, key=lambda x: (x != 'Cover Story', x)):
66 articles = feeds[sec]
67 if articles:
68 ans.append((sec, articles))
69 return ans