| 1 |
#!/usr/bin/env python |
| 2 |
import json |
| 3 |
import re |
| 4 |
|
| 5 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 6 |
|
| 7 |
|
| 8 |
def absurl(url): |
| 9 |
if url.startswith('/'): |
| 10 |
return 'https://swarajyamag.com' + url |
| 11 |
return url |
| 12 |
|
| 13 |
|
| 14 |
html_entities = {'"': '"', ''': "'", '<': '<', '>': '>', '&': '&'} |
| 15 |
|
| 16 |
|
| 17 |
class SwarajyaMag(BasicNewsRecipe): |
| 18 |
title = 'Swarajya Magazine' |
| 19 |
__author__ = 'unkn0wn' |
| 20 |
description = 'Swarajya - a big tent for liberal right of centre discourse that reaches out, engages and caters to the new India.' |
| 21 |
language = 'en_IN' |
| 22 |
remove_javascript = True |
| 23 |
use_embedded_content = False |
| 24 |
encoding = 'utf-8' |
| 25 |
|
| 26 |
recipe_specific_options = { |
| 27 |
'issue': { |
| 28 |
'short': 'The edition URL ', |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
def preprocess_html(self, soup): |
| 33 |
for img in soup.findAll('img', attrs={'src': True}): |
| 34 |
img['src'] = img['src'].split('?')[0] + '?w=600' |
| 35 |
return soup |
| 36 |
|
| 37 |
def parse_index(self): |
| 38 |
d = self.recipe_specific_options.get('issue') |
| 39 |
if d and isinstance(d, str): |
| 40 |
url = d |
| 41 |
else: |
| 42 |
soup = self.index_to_soup('https://swarajyamag.com/all-issues') |
| 43 |
a = soup.find('a', href=lambda x: x and x.startswith('/issue/')) |
| 44 |
url = absurl(a['href']) |
| 45 |
self.log('Downloading issue:', url) |
| 46 |
|
| 47 |
soup = self.index_to_soup(url) |
| 48 |
ans = [] |
| 49 |
|
| 50 |
cont = soup.find(attrs={'id': 'container'}) |
| 51 |
self.cover_url = cont.find('a', href=lambda x: x and x.startswith('/issue/')).img['src'].split('?')[0] + '?w=600' |
| 52 |
for div in cont.findAll('div', attrs={'class': 'rounded'}): |
| 53 |
url = div.findParent('a')['href'] |
| 54 |
if url.startswith('/'): |
| 55 |
url = 'https://swarajyamag.com' + url |
| 56 |
h4 = div.find('h4') |
| 57 |
title = self.tag_to_string(h4) |
| 58 |
d = h4.next_sibling.div |
| 59 |
desc = 'By ' + self.tag_to_string(d).strip() |
| 60 |
self.log(title, ' at ', url, '\n', desc) |
| 61 |
ans.append({'title': title, 'url': url, 'description': desc}) |
| 62 |
return [('Articles', ans)] |
| 63 |
|
| 64 |
def preprocess_raw_html(self, raw, url): |
| 65 |
app = re.search(r'<script type=\"application/ld\+json\">({\"headline.+})', raw).group(1) |
| 66 |
data = json.JSONDecoder().raw_decode(app)[0] |
| 67 |
|
| 68 |
title = f'<h1>{data["headline"]}</h1>' |
| 69 |
|
| 70 |
pattern = re.compile('|'.join(html_entities.keys())) |
| 71 |
body = pattern.sub(lambda m: html_entities[m.group(0)], data['articleBody']) |
| 72 |
|
| 73 |
image = desc = auth = sec = '' |
| 74 |
if data.get('articleSection'): |
| 75 |
sec = f'<div style="font-size: small;">{data["articleSection"]}</div>' |
| 76 |
if data.get('author'): |
| 77 |
auth = f'<p style="font-size: small;>By {", ".join(x["name"] for x in data["author"])}</p>' |
| 78 |
if data.get('image'): |
| 79 |
image_url = data['image']['url'] |
| 80 |
image = f'<div><img src="{image_url}"></div>' |
| 81 |
if data.get('description'): |
| 82 |
desc = '<p>' + data['description'] + '</p>' |
| 83 |
html = '<html><body>' + sec + title + desc + image + auth + body |
| 84 |
return html |