Debian Codegraph @

recipes/fortune_magazine.recipe

raw
1 from calibre.web.feeds.recipes import BasicNewsRecipe
2
3
4 def classes(classes):
5 q = frozenset(classes.split(' '))
6 return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})
7
8
9 def prefix_classes(classes):
10 q = classes.split()
11
12 def test(x):
13 if x:
14 for cls in x.split():
15 for c in q:
16 if cls.startswith(c):
17 return True
18 return False
19
20 return dict(attrs={'class': test})
21
22
23 class Fortune(BasicNewsRecipe):
24 title = 'Fortune Magazine'
25 __author__ = 'Rick Shang'
26
27 description = 'FORTUNE is a global business magazine that has been revered in its content and credibility since 1930. FORTUNE covers the entire field of business, including specific companies and business trends, prominent business leaders, and new ideas shaping the global marketplace.' # noqa: E501
28 language = 'en'
29 category = 'news'
30 encoding = 'UTF-8'
31 keep_only_tags = [
32 classes('amp-wp-title amp-wp-dek amp-header-meta amp-wp-article-featured-image amp-wp-article-content'),
33 ]
34
35 no_javascript = True
36 no_stylesheets = True
37 needs_subscription = 'optional'
38
39 def get_browser(self):
40 br = BasicNewsRecipe.get_browser(self)
41 if self.username and self.password:
42 br.open('https://fortune.com/')
43 br.select_form(id='sign-in-form')
44 br['username'] = self.username
45 br['password'] = self.password
46 br.submit()
47 return br
48
49 def preprocess_html(self, soup, *a):
50 for ai in soup.findAll(name='amp-img'):
51 ai.name = 'img'
52 return soup
53
54 def parse_index(self):
55 articles = []
56
57 # Go to the latestissue
58 soup = self.index_to_soup('https://fortune.com/section/magazine/')
59 articles = []
60
61 for li in soup.findAll('li', attrs={'class': lambda x: x and 'termArchiveContentList__item--' in x}):
62 a = li.find('a', href=True)
63 url = a['href'].rstrip('/') + '/amp'
64 div = li.find(attrs={'class': lambda x: x and 'termArchiveContentListItem__title--' in x})
65 title = self.tag_to_string(div)
66 if title.startswith('Magazine'):
67 title = title[len('Magazine') :]
68 desc = ''
69 div = li.find(attrs={'class': lambda x: x and 'termArchiveContentListItem__excerpt--' in x})
70 if div is not None:
71 desc = self.tag_to_string(div)
72 self.log(title, url)
73 articles.append({'title': title, 'url': url, 'description': desc})
74 return [('Articles', articles)]