| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
import json |
| 4 |
|
| 5 |
from html5_parser import parse |
| 6 |
|
| 7 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 8 |
|
| 9 |
|
| 10 |
def get_story(story): |
| 11 |
str_type = story.get('type', '') |
| 12 |
if str_type == 'text': |
| 13 |
yield '\n' + story['text'] |
| 14 |
elif str_type == 'image': |
| 15 |
yield ''.join(img(story)) |
| 16 |
elif 'story-elements' in story: |
| 17 |
for x in story['story-elements']: |
| 18 |
yield from get_story(x) |
| 19 |
|
| 20 |
|
| 21 |
def img(img): |
| 22 |
yield '<p>' |
| 23 |
if 'image-s3-key' in img: |
| 24 |
yield '<img src="{}">'.format('https://media.assettype.com/' + img['image-s3-key']) |
| 25 |
if 'title' in img: |
| 26 |
yield '<div class="cap">' + img['title'] + '</div>' |
| 27 |
yield '</p>' |
| 28 |
|
| 29 |
|
| 30 |
class himal(BasicNewsRecipe): |
| 31 |
title = 'Himal Southasian' |
| 32 |
__author__ = 'unkn0wn' |
| 33 |
description = ( |
| 34 |
'Himal Southasian is Southasia’s first and only regional magazine of politics and culture.' |
| 35 |
' For over 30 years, Himal Southasian has challenged nationalist orthodoxies, and covered the region with ' |
| 36 |
'imagination, rigour and irreverence, with contributions from some of the most interesting writers in the region.' |
| 37 |
) |
| 38 |
language = 'en_IN' |
| 39 |
no_stylesheets = True |
| 40 |
remove_attributes = ['height', 'width', 'style'] |
| 41 |
ignore_duplicate_articles = {'url'} |
| 42 |
masthead_url = 'https://gumlet.assettype.com/himalmag/2024-01/4ecc5615-eceb-4497-87c7-4e013083ba17/logo_.png' |
| 43 |
encoding = 'utf-8' |
| 44 |
resolve_internal_links = True |
| 45 |
oldest_article = 30 # days |
| 46 |
|
| 47 |
recipe_specific_options = { |
| 48 |
'days': { |
| 49 |
'short': 'Oldest article to download from this news source. In days ', |
| 50 |
'long': 'For example, 0.5, gives you articles from the past 12 hours', |
| 51 |
'default': str(oldest_article), |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
def __init__(self, *args, **kwargs): |
| 56 |
BasicNewsRecipe.__init__(self, *args, **kwargs) |
| 57 |
d = self.recipe_specific_options.get('days') |
| 58 |
if d and isinstance(d, str): |
| 59 |
self.oldest_article = float(d) |
| 60 |
|
| 61 |
extra_css = ''' |
| 62 |
.cap, .auth {font-size:small;} |
| 63 |
em, blockquote {color:#404040;} |
| 64 |
.subhead { font-style:italic; color:#202020; } |
| 65 |
''' |
| 66 |
|
| 67 |
feeds = [('Articles', 'https://www.himalmag.com/feed')] |
| 68 |
|
| 69 |
def preprocess_raw_html(self, raw, *a): |
| 70 |
root = parse(raw) |
| 71 |
m = root.xpath('//script[@id="static-page"]') |
| 72 |
data = json.loads(m[0].text)['qt']['data']['story'] |
| 73 |
|
| 74 |
title = '<h1>' + data['headline'] + '</h1>' |
| 75 |
|
| 76 |
subhead = auth = caption = lede = '' |
| 77 |
|
| 78 |
if 'subheadline' in data: |
| 79 |
subhead = '\n<p class="subhead">' + data['subheadline'] + '</p>' |
| 80 |
|
| 81 |
if 'author-name' in data: |
| 82 |
auth = '\n<div class="auth">' + data['author-name'] + '</div>' |
| 83 |
|
| 84 |
if 'hero-image-s3-key' in data: |
| 85 |
lede = '\n<p><img src="{}">'.format('https://media.assettype.com/' + data['hero-image-s3-key']) |
| 86 |
|
| 87 |
if 'hero-image-caption' in data: |
| 88 |
caption = '<div class="cap">' + data['hero-image-caption'] + '</div>' |
| 89 |
|
| 90 |
body = '' |
| 91 |
for ele in data['cards']: |
| 92 |
for story in ele.get('story-elements', {}): |
| 93 |
body += '\n'.join(get_story(story)) |
| 94 |
|
| 95 |
return '<html><body>\n' + title + subhead + auth + lede + caption + '<div>' + body + '\n</div></body></html>' |