Debian Codegraph @

recipes/epw_magazine.recipe

raw
1 from collections import OrderedDict
2
3 from calibre.web.feeds.news import BasicNewsRecipe, classes
4
5
6 def absurl(x):
7 if x.startswith('/'):
8 x = 'http://www.epw.in' + x
9 return x
10
11
12 class epw(BasicNewsRecipe):
13 title = 'EPW Magazine'
14 __author__ = 'unkn0wn'
15 description = 'Economic and Political news from India'
16 no_stylesheets = True
17 use_embedded_content = False
18 encoding = 'utf-8'
19 language = 'en_IN'
20 remove_attributes = ['style', 'height', 'width']
21 masthead_url = 'http://www.epw.in/system/files/epw_masthead.png'
22 ignore_duplicate_articles = {'title', 'url'}
23
24 keep_only_tags = [dict(name='h1', attrs={'id': 'page-title'}), classes('field-name-field-secondary-title field-type-text region-content updated_field')]
25 remove_tags = [classes('premium-message node-readmore tag_container mobile_article_info')]
26
27 def parse_index(self):
28 soup = self.index_to_soup('https://www.epw.in/journal/epw-archive')
29 div = soup.find('div', **classes('fieldset-wrapper'))
30 a = div.find('a', href=lambda x: x and x.startswith('/journal/'))
31 url = absurl(a['href'])
32 self.log(self.tag_to_string(a))
33 soup = self.index_to_soup(url)
34
35 view = soup.findAll('div', **classes('views-field-field-cover-image'))[0]
36 try:
37 self.cover_url = view.find('img', src=True)['src'].split('?')[0].replace('/styles/freeissue/public', '')
38 except Exception:
39 # sometimes they don't add img src
40 self.cover_url = 'https://www.epw.in/sites/default/files/cache/cover_images/2022/Cover_4June2022_Big.gif'
41
42 feeds = OrderedDict()
43
44 div = soup.find('div', attrs={'id': 'block-system-main'})
45 for a in div.findAll('a', href=lambda x: x and x.startswith('/journal/')):
46 articles = []
47 url = absurl(a['href'])
48 title = self.tag_to_string(a)
49 view = a.findParent('h4').findParent('div', **classes('views-field-title'))
50 new = view.find_next_sibling('div')
51 if new:
52 desc = self.tag_to_string(new)
53 h3 = view.findParent('div', **classes('views-row')).find_previous_sibling('h3')
54 section_title = self.tag_to_string(h3)
55 self.log('\t', title)
56 self.log('\t', desc)
57 self.log('\t\t', url)
58 articles.append({'title': title, 'url': url, 'description': desc})
59
60 if articles:
61 if section_title not in feeds:
62 feeds[section_title] = []
63 feeds[section_title] += articles
64 ans = list(feeds.items())
65 return ans