Debian Codegraph @

recipes/minerva_magazine.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 """
4 https://minervamagazine.com/
5 """
6
7 from calibre import browser
8 from calibre.web.feeds.news import BasicNewsRecipe
9
10
11 class minerva(BasicNewsRecipe):
12 title = 'The Past: Minerva Magazine'
13 language = 'en'
14 __author__ = 'unkn0wn'
15 description = (
16 'Minerva, Archaeology and Ancient art, was a bi-monthly magazine publishing features on exhibitions, excavations, '
17 'and museums, interviews, news items, and book reviews. It ran from 1990 to 2023. So use this recipe to fetch past issues.'
18 )
19 no_stylesheets = True
20 use_embedded_content = False
21 remove_attributes = ['style', 'height', 'width']
22 ignore_duplicate_articles = {'url'}
23 resolve_internal_links = True
24 masthead_url = 'https://minervamagazine.com/media/website/Minerva-mastheadandstrap-black_323x80.png'
25 simultaneous_downloads = 1
26
27 extra_css = '''
28 [class^="meta"], [class~="__author__text"], [class~="__date"] { font-size:small; }
29 .post-subtitle { font-style: italic; color:#202020; }
30 .wp-block-image { font-size:small; text-align:center; }
31 '''
32
33 keep_only_tags = [
34 dict(
35 attrs={
36 'class': lambda x: (
37 x
38 and any(
39 tag in x
40 for tag in [
41 '__image',
42 '__header',
43 '__background',
44 '__body_area',
45 '__author__text',
46 '__date',
47 ]
48 )
49 )
50 }
51 )
52 ]
53
54 remove_tags = [
55 dict(attrs={'class': 'ad-break'}),
56 dict(attrs={'class': lambda x: x and any(cls in x.split() for cls in ['avatar', 'what-mag-row'])}),
57 dict(attrs={'class': lambda x: x and '--share' in x}),
58 ]
59
60 def preprocess_html(self, soup):
61 for img in soup.findAll('img', attrs={'src': True}):
62 if '?w=' in img['src']:
63 res = '?w=600'
64 w = self.recipe_specific_options.get('res')
65 if w and isinstance(w, str):
66 res = '?w=' + w
67 img['src'] = img['src'].split('?')[0] + res
68 img.attrs = {'src': img['src']}
69 exp = soup.find(attrs={'class': lambda x: x and 'post-subtitle' in x.split()})
70 if exp:
71 exp.name = 'p'
72 return soup
73
74 recipe_specific_options = {
75 'issue': {
76 'short': 'Enter the Issue Number you want to download ',
77 'long': 'For example, 136',
78 },
79 'res': {
80 'short': 'For hi-res images, select a resolution from the\nfollowing options: 800, 1000, 1200 or 1500',
81 'long': 'This is useful for non e-ink devices, and for a lower file size\nthan the default, use 400 or 300.',
82 'default': '600',
83 },
84 }
85
86 def parse_index(self):
87 soup = self.index_to_soup('https://the-past.com/category/magazines/minerva/')
88 art = soup.find('article', attrs={'class': lambda x: x and 'tag-magazines' in x.split()})
89 url = art.h2.a['href']
90
91 d = self.recipe_specific_options.get('issue')
92 if d and isinstance(d, str):
93 url = 'https://the-past.com/magazines/minerva-magazine-' + d + '/'
94
95 issue = self.index_to_soup(url)
96 ti = issue.find('h1', attrs={'class': lambda x: x and 'post-title' in x.split()})
97 if ti:
98 self.title = self.tag_to_string(ti).strip()
99 dt = soup.find(attrs={'class': lambda x: x and '__date' in x})
100 if dt:
101 self.timefmt = ' [' + self.tag_to_string(dt).strip() + ']'
102 edit = issue.find('h2', attrs={'id': 'from-the-editor'})
103 if edit and edit.findParent('div'):
104 self.description = self.tag_to_string(edit.findParent('div'))
105 cov = issue.find('figure', attrs={'class': lambda x: x and 'wp-block-image' in x.split()})
106 if cov:
107 self.cover_url = cov.img['src'].split('?')[0] + '?w=600'
108 div = issue.find('div', attrs={'class': lambda x: x and 'entry-content' in x.split()})
109
110 feeds = []
111
112 h2 = div.findAll('h2', attrs={'class': lambda x: x and 'wp-block-heading' in x.split()})
113 lt = div.findAll(attrs={'class': 'display-posts-listing'})
114 for x, y in zip(h2, lt):
115 section = self.tag_to_string(x).strip()
116 self.log(section)
117 articles = []
118 for a in y.findAll('a', href=True, attrs={'class': 'title'}):
119 url = a['href']
120 title = self.tag_to_string(a).strip()
121 desc = ''
122 exp = a.findNext(attrs={'class': 'excerpt'})
123 if exp:
124 desc = self.tag_to_string(exp).strip()
125 self.log('\t', title, '\n\t', desc, '\n\t\t', url)
126 articles.append({'title': title, 'description': desc, 'url': url})
127 if articles:
128 feeds.append((section, articles))
129 return feeds
130
131 def get_browser(self, *args, **kwargs):
132 return self
133
134 def clone_browser(self, *args, **kwargs):
135 return self.get_browser()
136
137 def open_novisit(self, *args, **kwargs):
138 br = browser()
139 return br.open_novisit(*args, **kwargs)
140
141 open = open_novisit