Change how cookies are transfered. Use a Mozilla cookie txt file for moving cookies. This fixes and issue with nook library files not downloading from B&N.

This commit is contained in:
John Schember 2011-03-11 21:01:00 -05:00
parent 5a3667e701
commit 4a34af3ad6
3 changed files with 54 additions and 18 deletions

View File

@ -481,13 +481,16 @@ def url_slash_cleaner(url):
'''
return re.sub(r'(?<!:)/{2,}', '/', url)
def get_download_filename(url, cookie_jar=None):
def get_download_filename(url, cookie_file=None):
filename = ''
br = browser()
if cookie_jar:
br.set_cookiejar(cookie_jar)
if cookie_file:
from mechanize import MozillaCookieJar
cj = MozillaCookieJar()
cj.load(cookie_file)
br.set_cookiejar(cj)
with closing(br.open(url)) as r:
disposition = r.info().get('Content-disposition', '')
for p in disposition.split(';'):

View File

@ -16,6 +16,7 @@ from PyQt4.Qt import QWebView, QWebPage, QNetworkCookieJar, QNetworkRequest, QSt
from calibre import USER_AGENT, browser, get_proxies, get_download_filename
from calibre.ebooks import BOOK_EXTENSIONS
from calibre.ptempfile import PersistentTemporaryFile
class NPWebView(QWebView):
@ -60,9 +61,9 @@ class NPWebView(QWebView):
return
url = unicode(request.url().toString())
cj = self.get_cookies()
cf = self.get_cookies()
filename = get_download_filename(url, cj)
filename = get_download_filename(url, cf)
ext = os.path.splitext(filename)[1][1:].lower()
if ext not in BOOK_EXTENSIONS:
home = os.path.expanduser('~')
@ -71,13 +72,42 @@ class NPWebView(QWebView):
os.path.join(home, filename),
'*.*')
if name:
self.gui.download_from_store(url, cj, name, name, False)
self.gui.download_from_store(url, cf, name, name, False)
else:
self.gui.download_from_store(url, cj, filename, tags=self.tags)
self.gui.download_from_store(url, cf, filename, tags=self.tags)
def ignore_ssl_errors(self, reply, errors):
reply.ignoreSslErrors(errors)
def get_cookies(self):
'''
Writes QNetworkCookies to Mozilla cookie .txt file.
:return: The file path to the cookie file.
'''
cf = PersistentTemporaryFile(suffix='.txt')
cf.write('# Netscape HTTP Cookie File\n\n')
for c in self.page().networkAccessManager().cookieJar().allCookies():
cookie = []
domain = unicode(c.domain())
cookie.append(domain)
cookie.append('TRUE' if domain.startswith('.') else 'FALSE')
cookie.append(unicode(c.path()))
cookie.append('TRUE' if c.isSecure() else 'FALSE')
cookie.append(unicode(c.expirationDate().toTime_t()))
cookie.append(unicode(c.name()))
cookie.append(unicode(c.value()))
cf.write('\t'.join(cookie))
cf.write('\n')
cf.close()
return cf.name
'''
def get_cookies(self):
cj = CookieJar()
@ -125,6 +155,7 @@ class NPWebView(QWebView):
cj.set_cookie(cookie)
return cj
'''
class NPWebPage(QWebPage):

View File

@ -10,9 +10,8 @@ import cStringIO
import os
import shutil
import time
import urllib2
from cookielib import CookieJar
from contextlib import closing
from mechanize import MozillaCookieJar
from threading import Thread
from Queue import Queue
@ -24,12 +23,12 @@ from calibre.utils.ipc.job import BaseJob
class StoreDownloadJob(BaseJob):
def __init__(self, callback, description, job_manager, db, cookie_jar, url='', filename='', save_as_loc='', add_to_lib=True, tags=[]):
def __init__(self, callback, description, job_manager, db, cookie_file=None, url='', filename='', save_as_loc='', add_to_lib=True, tags=[]):
BaseJob.__init__(self, description)
self.exception = None
self.job_manager = job_manager
self.db = db
self.cookie_jar = cookie_jar
self.cookie_file = cookie_file
self.args = (url, filename, save_as_loc, add_to_lib, tags)
self.tmp_file_name = ''
self.callback = callback
@ -151,10 +150,13 @@ class StoreDownloader(Thread):
return
if not filename:
filename = get_download_filename(url, job.cookie_jar)
filename = get_download_filename(url, job.cookie_file)
br = browser()
br.set_cookiejar(job.cookie_jar)
if job.cookie_file:
cj = MozillaCookieJar()
cj.load(job.cookie_file)
br.set_cookiejar(cj)
with closing(br.open(url)) as r:
tf = PersistentTemporaryFile(suffix=filename)
tf.write(r.read())
@ -183,9 +185,9 @@ class StoreDownloader(Thread):
shutil.copy(job.tmp_file_name, save_loc)
def download_from_store(self, callback, db, cookie_jar, url='', filename='', save_as_loc='', add_to_lib=True, tags=[]):
def download_from_store(self, callback, db, cookie_file=None, url='', filename='', save_as_loc='', add_to_lib=True, tags=[]):
description = _('Downloading %s') % filename if filename else url
job = StoreDownloadJob(callback, description, self.job_manager, db, cookie_jar, url, filename, save_as_loc, add_to_lib, tags)
job = StoreDownloadJob(callback, description, self.job_manager, db, cookie_file, url, filename, save_as_loc, add_to_lib, tags)
self.job_manager.add_job(job)
self.jobs.put(job)
@ -195,13 +197,13 @@ class StoreDownloadMixin(object):
def __init__(self):
self.store_downloader = StoreDownloader(self.job_manager)
def download_from_store(self, url='', cookie_jar=CookieJar(), filename='', save_as_loc='', add_to_lib=True, tags=[]):
def download_from_store(self, url='', cookie_file=None, filename='', save_as_loc='', add_to_lib=True, tags=[]):
if not self.store_downloader.is_alive():
self.store_downloader.start()
if tags:
if isinstance(tags, basestring):
tags = tags.split(',')
self.store_downloader.download_from_store(Dispatcher(self.downloaded_from_store), self.library_view.model().db, cookie_jar, url, filename, save_as_loc, add_to_lib, tags)
self.store_downloader.download_from_store(Dispatcher(self.downloaded_from_store), self.library_view.model().db, cookie_file, url, filename, save_as_loc, add_to_lib, tags)
self.status_bar.show_message(_('Downloading') + ' ' + filename if filename else url, 3000)
def downloaded_from_store(self, job):