Debian Codegraph @

recipes/hindustan_times_print.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 import json
4 from collections import defaultdict
5 from datetime import date
6
7 from calibre.web.feeds.news import BasicNewsRecipe
8
9 index = 'https://epaper.hindustantimes.com'
10
11
12 class ht(BasicNewsRecipe):
13 title = 'Hindustan Times Print Edition'
14 language = 'en_IN'
15 __author__ = 'unkn0wn'
16 masthead_url = 'https://www.htmedia.in/wp-content/uploads/2020/08/HT-dot-com-logo-product.png'
17 description = 'Articles from the Hindustan Times epaper, digital edition'
18 encoding = 'utf-8'
19 delay = 1
20 ignore_duplicate_articles = {'title'}
21
22 extra_css = '''
23 .cap { text-align:center; font-size:small; }
24 img { display:block; margin:0 auto; }
25 '''
26
27 recipe_specific_options = {
28 'location': {
29 'short': 'The name of the local edition',
30 'long': (
31 'If The Hindustan Times is available in your local town/city, '
32 'set this to your location, for example, Delhi\nAvailable Editions:'
33 'Delhi, Mumbai, Chandigarh, Lucknow, Patna, Bengaluru, Pune, Gurgaon,'
34 'Ludhiana, Rajasthan, Amritsar,\nEast UP, Haryana, Jammu, Navi Mumbai,'
35 'Noida, Punjab, Ranchi, Thane, Uttarakhand, West UP'
36 ),
37 'default': 'Delhi',
38 },
39 'date': {'short': 'The date of the edition to download (DD/MM/YYYY format)', 'long': 'For example, 22/12/2023'},
40 }
41
42 def parse_index(self):
43 self.log('\n***\nif this recipe fails, report it on: https://www.mobileread.com/forums/forumdisplay.php?f=228\n***\n')
44 local_edition = 'Delhi'
45 d = self.recipe_specific_options.get('location')
46 if d and isinstance(d, str):
47 local_edition = d
48
49 today = date.today().strftime('%d/%m/%Y')
50
51 p = self.recipe_specific_options.get('date')
52 if p and isinstance(p, str):
53 today = p
54
55 day, month, year = (int(x) for x in today.split('/'))
56 dt = date(year, month, day)
57
58 self.timefmt = ' [%s]' % dt.strftime('%b %d, %Y')
59
60 today = today.replace('/', '%2F')
61
62 get_edition = index + '/Home/GetEditionSupplementHierarchy?EditionDate=' + today
63 edi_data = json.loads(self.index_to_soup(get_edition, raw=True))
64 cities = []
65 for edi in edi_data:
66 cities.append(edi['EditionName'])
67 self.log('## For your local_edition, modify this recipe to match your city from the names below\n(', ', '.join(cities), ')\n')
68 for edi in edi_data:
69 if edi['EditionName'] == local_edition:
70 edi_name = edi['EditionName']
71 edi_id = str(edi['EditionId'])
72 self.log('Downloading', edi_name, 'Edition', self.timefmt)
73
74 url = index + '/Home/GetAllpages?editionid=' + edi_id + '&editiondate=' + today
75 main_data = json.loads(self.index_to_soup(url, raw=True))
76
77 feeds_dict = defaultdict(list)
78
79 for page in main_data:
80 page_no = page['PageNumber']
81 sec_name = page['NewsProPageTitle']
82 if sec_name == 'Full Page Ad':
83 continue
84 if sec_name.startswith('Front'):
85 self.cover_url = page['HighResolution']
86 art = index + '/Home/getingRectangleObject?pageid=' + str(page['PageId'])
87 self.log(sec_name, ' ', page_no)
88 art_data = json.loads(self.index_to_soup(art, raw=True))
89 for snaps in art_data:
90 section = sec_name
91 url = str(snaps['OrgId'])
92 title = ' '.join(snaps['StoryTitle'].split()[:15])
93 if not title:
94 continue
95 desc = page_no
96 self.log('\t', title, ' ', desc)
97 feeds_dict[section].append({'title': title, 'description': desc, 'url': url})
98 return list(feeds_dict.items())
99
100 def preprocess_raw_html(self, raw, *a):
101 data = json.loads(raw)
102 body = ''
103 for x in data['StoryContent']:
104 if x['Headlines']:
105 if len(x['Headlines']) > 0:
106 body += '<h1>' + x['Headlines'][0].replace('\n', ' ') + '</h1>'
107 for y in x['Headlines'][1:]:
108 body += '<h4>' + y.replace('\n', ' ') + '</h4>'
109 if data['LinkPicture']:
110 for pics in data['LinkPicture']:
111 if pics['fullpathlinkpic']:
112 body += '<div><img src="{}"></div>'.format(pics['fullpathlinkpic'])
113 if pics['caption']:
114 body += '<div class="cap">' + pics['caption'] + '</div><p>'
115 for x in data['StoryContent']:
116 if x['Body']:
117 body += x['Body']
118 # if data['filepathstorypic']: # this gives you a snap image of the article from page
119 # body += '<div><img src="{}"></div>'.format(data['filepathstorypic'].replace('\\', '/'))
120 return '<html><body><div>' + body + '</div></body></html>'
121
122 def print_version(self, url):
123 return index + '/User/ShowArticleView?OrgId=' + url
124
125 def populate_article_metadata(self, article, soup, first):
126 article.url = '***'