Debian Codegraph @

recipes/world_archeology.recipe

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