diff --git a/recipes/caravan_magazine.recipe b/recipes/caravan_magazine.recipe index 7b9d9de901..2fd3475bb3 100644 --- a/recipes/caravan_magazine.recipe +++ b/recipes/caravan_magazine.recipe @@ -1,4 +1,11 @@ -# coding: utf-8 +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +# License: GPLv3 Copyright: 2015, Kovid Goyal + +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/'