Failed attempt at getting SCMP to serve full content after login

This commit is contained in:
Kovid Goyal 2019-11-03 13:36:33 +05:30
parent 575a6ea121
commit 0b05e33224
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -2,6 +2,8 @@
scmp.com scmp.com
''' '''
from mechanize import Request
import json
from calibre.web.feeds.news import BasicNewsRecipe from calibre.web.feeds.news import BasicNewsRecipe
from calibre.ebooks.BeautifulSoup import Tag from calibre.ebooks.BeautifulSoup import Tag
@ -45,15 +47,33 @@ class SCMP(BasicNewsRecipe):
def get_browser(self): def get_browser(self):
br = BasicNewsRecipe.get_browser(self) br = BasicNewsRecipe.get_browser(self)
# br.set_debug_http(True)
# br.set_debug_responses(True)
# br.set_debug_redirects(True)
if self.username is not None and self.password is not None: if self.username is not None and self.password is not None:
br.open('https://www.scmp.com/user/login') # br.set_debug_http(True)
br.select_form(nr=0) # br.set_debug_responses(True)
br['name'] = self.username # br.set_debug_redirects(True)
br['pass'] = self.password rq = Request('https://account.scmp.com/login', headers={
br.submit() 'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=UTF-8',
'Referer': 'https://account.scmp.com/login',
}, data=json.dumps({'username': self.username, 'password': self.password}))
self.log('Sending login request...')
try:
res = br.open(rq)
except Exception as err:
if hasattr(err, 'read'):
raise Exception('Login request failed with error: {} and body: {}'.format(err, err.read().decode('utf-8', 'replace')))
raise
if res.code != 200:
raise ValueError('Failed to login, check your username and password')
nonce = json.loads(res.read())['nonce']
rq = Request('https://www.scmp.com/centralize/signin?nonce=' + nonce, headers={
'referer': 'https://account.scmp.com/login',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'same-site',
'sec-fetch-user': '?1'})
res = br.open(rq)
if res.code != 200:
raise ValueError('Failed to login, check your username and password')
return br return br
feeds = [ feeds = [