Make user agent consistant. Remove debugging code.

This commit is contained in:
John Schember 2011-02-26 20:39:35 -05:00
parent 5d92612f2e
commit 3883a1d6a2
2 changed files with 8 additions and 13 deletions

View File

@ -240,6 +240,8 @@ def get_parsed_proxy(typ='http', debug=True):
prints('Using http proxy', str(ans)) prints('Using http proxy', str(ans))
return ans return ans
USER_AGENT = 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101210 Gentoo Firefox/3.6.13'
USER_AGENT_MOBILE = 'Mozilla/5.0 (Windows; U; Windows CE 5.1; rv:1.8.1a3) Gecko/20060610 Minimo/0.016'
def browser(honor_time=True, max_time=2, mobile_browser=False, user_agent=None): def browser(honor_time=True, max_time=2, mobile_browser=False, user_agent=None):
''' '''
@ -254,8 +256,7 @@ def browser(honor_time=True, max_time=2, mobile_browser=False, user_agent=None):
opener.set_handle_refresh(True, max_time=max_time, honor_time=honor_time) opener.set_handle_refresh(True, max_time=max_time, honor_time=honor_time)
opener.set_handle_robots(False) opener.set_handle_robots(False)
if user_agent is None: if user_agent is None:
user_agent = ' Mozilla/5.0 (Windows; U; Windows CE 5.1; rv:1.8.1a3) Gecko/20060610 Minimo/0.016' if mobile_browser else \ user_agent = USER_AGENT_MOBILE if mobile_browser else USER_AGENT
'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101210 Gentoo Firefox/3.6.13'
opener.addheaders = [('User-agent', user_agent)] opener.addheaders = [('User-agent', user_agent)]
http_proxy = get_proxies().get('http', None) http_proxy = get_proxies().get('http', None)
if http_proxy: if http_proxy:

View File

@ -8,19 +8,20 @@ from cookielib import Cookie, CookieJar
from PyQt4.Qt import QWebView, QWebPage, QNetworkCookieJar, QNetworkRequest, QString from PyQt4.Qt import QWebView, QWebPage, QNetworkCookieJar, QNetworkRequest, QString
from calibre import USER_AGENT
class NPWebView(QWebView): class NPWebView(QWebView):
def __init__(self, *args): def __init__(self, *args):
QWebView.__init__(self, *args) QWebView.__init__(self, *args)
self.gui = None self.gui = None
#self.setPage(NPWebPage()) self.setPage(NPWebPage())
self.page().networkAccessManager().setCookieJar(QNetworkCookieJar()) self.page().networkAccessManager().setCookieJar(QNetworkCookieJar())
self.page().setForwardUnsupportedContent(True) self.page().setForwardUnsupportedContent(True)
self.page().unsupportedContent.connect(self.start_download) self.page().unsupportedContent.connect(self.start_download)
self.page().downloadRequested.connect(self.start_download) self.page().downloadRequested.connect(self.start_download)
self.page().networkAccessManager().sslErrors.connect(self.ignore_ssl_errors) self.page().networkAccessManager().sslErrors.connect(self.ignore_ssl_errors)
#self.page().networkAccessManager().finished.connect(self.fin)
def createWindow(self, type): def createWindow(self, type):
if type == QWebPage.WebBrowserWindow: if type == QWebPage.WebBrowserWindow:
@ -33,7 +34,6 @@ class NPWebView(QWebView):
def start_download(self, request): def start_download(self, request):
if not self.gui: if not self.gui:
print 'no gui'
return return
url = unicode(request.url().toString()) url = unicode(request.url().toString())
@ -41,13 +41,7 @@ class NPWebView(QWebView):
def ignore_ssl_errors(self, reply, errors): def ignore_ssl_errors(self, reply, errors):
reply.ignoreSslErrors(errors) reply.ignoreSslErrors(errors)
def fin(self, reply):
if reply.error():
print 'error'
print reply.error()
#print reply.attribute(QNetworkRequest.HttpStatusCodeAttribute).toInt()
def get_cookies(self): def get_cookies(self):
cj = CookieJar() cj = CookieJar()
@ -99,4 +93,4 @@ class NPWebView(QWebView):
class NPWebPage(QWebPage): class NPWebPage(QWebPage):
def userAgentForUrl(self, url): def userAgentForUrl(self, url):
return 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.102 Safari/534.13' return USER_AGENT