Update Caravan Magazine

Add support for optional login
This commit is contained in:
Kovid Goyal 2020-07-04 15:38:13 +05:30
parent ccf23b7b86
commit 8b7b02cc41
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,4 +1,11 @@
# coding: utf-8
#!/usr/bin/env python2
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
import json
from mechanize import Request
from calibre.web.feeds.recipes import BasicNewsRecipe
@ -16,6 +23,7 @@ class CaravanMagazine(BasicNewsRecipe):
language = 'en_IN'
timefmt = ' [%b, %Y]'
encoding = 'utf-8'
needs_subscription = 'optional'
no_stylesheets = True
@ -29,6 +37,32 @@ class CaravanMagazine(BasicNewsRecipe):
dict(attrs={'class': ['share-with']}),
]
def get_browser(self, *args, **kw):
br = BasicNewsRecipe.get_browser(self, *args, **kw)
if not self.username or not self.password:
return br
data = json.dumps({'email': self.username, 'name': '', 'password': self.password})
if not isinstance(data, bytes):
data = data.encode('utf-8')
rq = Request(
url='https://caravanmagazine.in/api/users/login',
data=data,
headers={
'Accept': 'application/json, text/plain, */*',
'Origin': 'https://caravanmagazine.in',
'Referer': 'https://caravanmagazine.in/',
'Content-type': 'application/json;charset=UTF-8',
},
method='POST'
)
res = br.open(rq).read()
res = res.decode('utf-8')
self.log('Login request response: {}'.format(res))
res = json.loads(res)
if res['code'] != 200 or res['message'] != "Login success":
raise ValueError('Login failed, check your username and password')
return br
# To parse artice toc
def parse_index(self):
base_url = 'https://www.caravanmagazine.in/'