Debian Codegraph @

recipes/sportstar.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 from collections import OrderedDict
4
5 from calibre.web.feeds.news import BasicNewsRecipe, classes
6
7
8 class Sportstar(BasicNewsRecipe):
9 title = 'Sportstar'
10 __author__ = 'unkn0wn'
11 description = (
12 'Sportstar began as a Print Only multi-sport weekly on July 15, 1978.'
13 ' In 2018 the periodicity of Sportstar was made fortnightly with a lot of in-depth articles coming into the mix.'
14 ' Our readers have been the fount of inspiration in our attempts at exploring new angles in sports journalism.'
15 )
16 language = 'en_IN'
17 no_stylesheets = True
18 remove_javascript = True
19 use_embedded_content = False
20 encoding = 'utf-8'
21 ignore_duplicate_articles = {'url'}
22 resolve_internal_links = True
23 masthead_url = 'https://assetsss.thehindu.com/theme/images/SSRX/sportstar-logo.svg'
24 remove_attributes = ['height', 'width']
25 extra_css = '''
26 .sub-title {font-style:italic; color:#202020;}
27 .caption {font-size:small; text-align:center;}
28 .author, .publish-time {font-size:small;}
29 '''
30
31 recipe_specific_options = {'issue': {'short': 'Enter the Issue Number you want to download\n(Volume-Issue format)', 'long': 'For example, 47-16'}}
32
33 keep_only_tags = [
34 dict(name='h1', attrs={'class': 'title'}),
35 dict(name='h2', attrs={'class': 'sub-title'}),
36 classes('publish-time author top-pic articlebodycontent'),
37 ]
38
39 remove_tags = [classes('show-mobile inlineAds related-topics related-stories comments-shares share-page title-patch pic-caption slide-mobile also-read')]
40
41 def parse_index(self):
42 d = self.recipe_specific_options.get('issue')
43 if d and isinstance(d, str):
44 issue_url = 'https://sportstar.thehindu.com/magazine/issue/vol' + d
45 else:
46 soup = self.index_to_soup('https://sportstar.thehindu.com/magazine/')
47 issue_url = soup.find('a', href=lambda x: x and x.startswith('https://sportstar.thehindu.com/magazine/issue/'))['href']
48 self.log('Downloading Issue: ', issue_url)
49
50 soup = self.index_to_soup(issue_url)
51
52 feeds = OrderedDict()
53
54 info = soup.find('div', attrs={'class': lambda x: x and 'left-sticky' in x.split()})
55 self.cover_url = info.find('div', attrs={'class': 'sptar-image'}).find('img')['data-original'].replace('FREE_320', 'FREE_1200')
56 self.description = self.tag_to_string(info.find('div', attrs={'class': 'sub-text'})).strip()
57
58 for content in soup.findAll('div', attrs={'class': 'content'}):
59 articles = []
60 h3 = content.find('h3', attrs={'class': 'title'})
61 url = h3.find('a', href=True)['href']
62 title = self.tag_to_string(h3).strip()
63 desc = self.tag_to_string(content.find('div', attrs={'class': 'sub-text'})).strip()
64 section_title = self.tag_to_string(content.find('div', attrs={'class': 'label'})).strip()
65 self.log(section_title)
66 self.log('\t', title)
67 self.log('\t', desc)
68 self.log('\t\t', url)
69 articles.append({'title': title, 'url': url, 'description': desc})
70
71 if articles:
72 if section_title not in feeds:
73 feeds[section_title] = []
74 feeds[section_title] += articles
75 ans = list(feeds.items())
76 return ans
77
78 def preprocess_html(self, soup):
79 if h2 := soup.find('h2'):
80 h2.name = 'p'
81 for img in soup.findAll('img', attrs={'data-original': True}):
82 if img['data-original'].endswith('1x1_spacer.png'):
83 source = img.findPrevious('source', srcset=True)
84 img.extract()
85 if source:
86 source['src'] = source['srcset'].replace('_320', '_1200')
87 source.name = 'img'
88 else:
89 img['src'] = img['data-original']
90 return soup
91
92 def postprocess_html(self, soup, first_fetch):
93 for src in soup.findAll('source'):
94 src.extract()
95 return soup