Update himal_southasian.recipe

This commit is contained in:
unkn0w7n 2024-03-26 12:57:33 +05:30
parent 66689df291
commit 0bd99e0635

View File

@ -1,7 +1,27 @@
from calibre.web.feeds.news import BasicNewsRecipe, classes from calibre.web.feeds.news import BasicNewsRecipe
from datetime import datetime, timezone, timedelta from html5_parser import parse
from calibre.utils.date import parse_date import json
def get_story(story):
str_type = story.get('type', '')
if str_type == 'text':
yield '\n' + story['text']
elif str_type == 'image':
yield ''.join(img(story))
elif str_type == 'composite':
for x in story.get('story-elements', {}):
yield from get_story(x)
elif 'story-elements' in story:
for x in story.get('story-elements', {}):
yield from get_story(x)
def img(img):
yield '<p>'
if 'image-s3-key' in img:
yield '<img src="{}">'.format('https://media.assettype.com/' + img['image-s3-key'])
if 'title' in img:
yield '<div class="cap">' + img['title'] + '</div>'
yield '</p>'
class himal(BasicNewsRecipe): class himal(BasicNewsRecipe):
title = 'Himal Southasian' title = 'Himal Southasian'
@ -13,66 +33,45 @@ class himal(BasicNewsRecipe):
no_stylesheets = True no_stylesheets = True
remove_attributes = ['height', 'width', 'style'] remove_attributes = ['height', 'width', 'style']
ignore_duplicate_articles = {'url'} ignore_duplicate_articles = {'url'}
masthead_url = 'https://www.himalmag.com/wp-content/themes/himaltheme-child/images/logo.svg' masthead_url = 'https://gumlet.assettype.com/himalmag/2024-01/4ecc5615-eceb-4497-87c7-4e013083ba17/logo_.png'
encoding = 'utf-8' encoding = 'utf-8'
remove_empty_feeds = True
resolve_internal_links = True resolve_internal_links = True
oldest_article = 30 # days oldest_article = 30 # days
extra_css = ''' extra_css = '''
.sub-row, .img-caption, .wp-caption-text, .comments-info-box {font-size:small;} .cap, .auth {font-size:small;}
em, blockquote {color:#404040;} em, blockquote {color:#404040;}
.subhead { font-style:italic; color:#202020; }
''' '''
remove_tags = [ feeds = [
dict(name='header'), ('Articles', 'https://www.himalmag.com/feed')
dict(name='footer'),
classes('skip-link single-btm share-info title-info post-categories comment-btn comment-line'),
] ]
def parse_index(self): def preprocess_raw_html(self, raw, *a):
sel = self.index_to_soup('https://www.himalmag.com/category/regions/') root = parse(raw)
nav_div = sel.find('div', attrs={'class':'category-sublist'}) m = root.xpath('//script[@id="static-page"]')
section_list = [] data = json.loads(m[0].text)['qt']['data']['story']
for a in nav_div.findAll('a', href=True): title = '<h1>' + data['headline'] + '</h1>'
section_list.append(
(self.tag_to_string(a).strip(), a['href'])
)
feeds = []
# For each section title, fetch the article urls subhead = auth = caption = lede = ''
for section in section_list:
section_title = section[0]
section_url = section[1]
self.log(section_title, section_url)
soup = self.index_to_soup(section_url)
articles = self.articles_from_soup(soup)
if articles:
feeds.append((section_title, articles))
return feeds
def articles_from_soup(self, soup): if 'subheadline' in data:
ans = [] subhead = '\n<p class="subhead">' + data['subheadline'] + '</p>'
div = soup.find('div', attrs={'id':'loadmore-wrap'})
for h3 in div.findAll('h3'):
a = h3.find('a', href=True)
url = a['href']
title = self.tag_to_string(a)
desc = ''
exp = h3.findNext('div', attrs={'class':'content-except'})
if exp:
desc = self.tag_to_string(exp)
h4 = h3.findNext('h4')
if h4:
date = parse_date(self.tag_to_string(h4).split('|')[1].strip())
today = (datetime.now(timezone.utc)).replace(microsecond=0)
if (today - date) > timedelta(self.oldest_article):
url = ''
if not url or not title: if 'author-name' in data:
continue auth = '\n<div class="auth">' + data['author-name'] + '</div>'
self.log('\t', title, '\n\t', desc, '\n\t\t', url) if 'hero-image-s3-key' in data:
ans.append({'title': title, 'description':desc, 'url': url}) lede = '\n<p><img src="{}">'.format('https://media.assettype.com/' + data['hero-image-s3-key'])
return ans
if 'hero-image-caption' in data:
caption = '<div class="cap">' + data['hero-image-caption'] + '</div>'
body = ''
for ele in data['cards']:
for story in ele.get('story-elements', {}):
body += '\n'.join(get_story(story))
return '<html><body>\n' + title + subhead + auth + lede + caption + '<div>' + body + '\n</div></body></html>'