mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
...
This commit is contained in:
parent
6ca1bf64ef
commit
d374b36e97
@ -120,7 +120,9 @@ class Query(object):
|
|||||||
if callable(getattr(e, 'getcode', None)) and \
|
if callable(getattr(e, 'getcode', None)) and \
|
||||||
e.getcode() == 404:
|
e.getcode() == 404:
|
||||||
return None
|
return None
|
||||||
if isinstance(getattr(e, 'args', [None])[0], socket.timeout):
|
attr = getattr(e, 'args', [None])
|
||||||
|
attr = attr if attr else [None]
|
||||||
|
if isinstance(attr[0], socket.timeout):
|
||||||
raise FictionwiseError(_('Fictionwise timed out. Try again later.'))
|
raise FictionwiseError(_('Fictionwise timed out. Try again later.'))
|
||||||
raise FictionwiseError(_('Fictionwise encountered an error.'))
|
raise FictionwiseError(_('Fictionwise encountered an error.'))
|
||||||
if '<title>404 - ' in raw:
|
if '<title>404 - ' in raw:
|
||||||
@ -329,7 +331,9 @@ class ResultList(list):
|
|||||||
if callable(getattr(e, 'getcode', None)) and \
|
if callable(getattr(e, 'getcode', None)) and \
|
||||||
e.getcode() == 404:
|
e.getcode() == 404:
|
||||||
return None
|
return None
|
||||||
if isinstance(getattr(e, 'args', [None])[0], socket.timeout):
|
attr = getattr(e, 'args', [None])
|
||||||
|
attr = attr if attr else [None]
|
||||||
|
if isinstance(attr[0], socket.timeout):
|
||||||
raise FictionwiseError(_('Fictionwise timed out. Try again later.'))
|
raise FictionwiseError(_('Fictionwise timed out. Try again later.'))
|
||||||
raise FictionwiseError(_('Fictionwise encountered an error.'))
|
raise FictionwiseError(_('Fictionwise encountered an error.'))
|
||||||
if '<title>404 - ' in raw:
|
if '<title>404 - ' in raw:
|
||||||
@ -407,6 +411,9 @@ def search(title=None, author=None, publisher=None, isbn=None,
|
|||||||
entries, islink = Query(title=title, author=author, publisher=publisher,
|
entries, islink = Query(title=title, author=author, publisher=publisher,
|
||||||
keywords=keywords, max_results=max_results)(br, verbose, timeout = 15.)
|
keywords=keywords, max_results=max_results)(br, verbose, timeout = 15.)
|
||||||
|
|
||||||
|
if entries is None or len(entries) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
#List of entry
|
#List of entry
|
||||||
ans = ResultList(islink)
|
ans = ResultList(islink)
|
||||||
ans.populate(entries, br, verbose)
|
ans.populate(entries, br, verbose)
|
||||||
|
@ -3,7 +3,7 @@ __license__ = 'GPL 3'
|
|||||||
__copyright__ = '2010, sengian <sengian1@gmail.com>'
|
__copyright__ = '2010, sengian <sengian1@gmail.com>'
|
||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import sys, textwrap, re, traceback, socket
|
import sys, re
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from Queue import Queue
|
from Queue import Queue
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
@ -35,6 +35,7 @@ class NiceBooks(MetadataSource):
|
|||||||
self.results = search(self.title, self.book_author, self.publisher,
|
self.results = search(self.title, self.book_author, self.publisher,
|
||||||
self.isbn, max_results=10, verbose=self.verbose)
|
self.isbn, max_results=10, verbose=self.verbose)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
import traceback
|
||||||
self.exception = e
|
self.exception = e
|
||||||
self.tb = traceback.format_exc()
|
self.tb = traceback.format_exc()
|
||||||
|
|
||||||
@ -70,6 +71,7 @@ class NiceBooksCovers(CoverDownload):
|
|||||||
ext = 'jpg'
|
ext = 'jpg'
|
||||||
result_queue.put((True, cover_data, ext, self.name))
|
result_queue.put((True, cover_data, ext, self.name))
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
import traceback
|
||||||
result_queue.put((False, self.exception_to_string(e),
|
result_queue.put((False, self.exception_to_string(e),
|
||||||
traceback.format_exc(), self.name))
|
traceback.format_exc(), self.name))
|
||||||
|
|
||||||
@ -96,6 +98,7 @@ class ThreadwithResults(Thread):
|
|||||||
|
|
||||||
def report(verbose):
|
def report(verbose):
|
||||||
if verbose:
|
if verbose:
|
||||||
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
@ -124,18 +127,21 @@ class Query(object):
|
|||||||
q = q.encode('utf-8')
|
q = q.encode('utf-8')
|
||||||
self.urldata = 'search?' + urlencode({'q':q,'s':'Rechercher'})
|
self.urldata = 'search?' + urlencode({'q':q,'s':'Rechercher'})
|
||||||
|
|
||||||
def __call__(self, browser, verbose, timeout = 5.):
|
def brcall(self, browser, url, verbose, timeout):
|
||||||
if verbose:
|
if verbose:
|
||||||
print _('Query: %s') % self.BASE_URL+self.urldata
|
print _('Query: %s') % url
|
||||||
|
|
||||||
try:
|
try:
|
||||||
raw = browser.open_novisit(self.BASE_URL+self.urldata, timeout=timeout).read()
|
raw = browser.open_novisit(url, timeout=timeout).read()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
import socket
|
||||||
report(verbose)
|
report(verbose)
|
||||||
if callable(getattr(e, 'getcode', None)) and \
|
if callable(getattr(e, 'getcode', None)) and \
|
||||||
e.getcode() == 404:
|
e.getcode() == 404:
|
||||||
return None
|
return None
|
||||||
if isinstance(getattr(e, 'args', [None])[0], socket.timeout):
|
attr = getattr(e, 'args', [None])
|
||||||
|
attr = attr if attr else [None]
|
||||||
|
if isinstance(attr[0], socket.timeout):
|
||||||
raise NiceBooksError(_('Nicebooks timed out. Try again later.'))
|
raise NiceBooksError(_('Nicebooks timed out. Try again later.'))
|
||||||
raise NiceBooksError(_('Nicebooks encountered an error.'))
|
raise NiceBooksError(_('Nicebooks encountered an error.'))
|
||||||
if '<title>404 - ' in raw:
|
if '<title>404 - ' in raw:
|
||||||
@ -143,14 +149,19 @@ class Query(object):
|
|||||||
raw = xml_to_unicode(raw, strip_encoding_pats=True,
|
raw = xml_to_unicode(raw, strip_encoding_pats=True,
|
||||||
resolve_entities=True)[0]
|
resolve_entities=True)[0]
|
||||||
try:
|
try:
|
||||||
feed = soupparser.fromstring(raw)
|
return soupparser.fromstring(raw)
|
||||||
except:
|
except:
|
||||||
try:
|
try:
|
||||||
#remove ASCII invalid chars
|
#remove ASCII invalid chars
|
||||||
feed = soupparser.fromstring(clean_ascii_chars(raw))
|
return soupparser.fromstring(clean_ascii_chars(raw))
|
||||||
except:
|
except:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def __call__(self, browser, verbose, timeout = 5.):
|
||||||
|
feed = self.brcall(browser, self.BASE_URL+self.urldata, verbose, timeout)
|
||||||
|
if feed is None:
|
||||||
|
return None
|
||||||
|
|
||||||
#nb of page to call
|
#nb of page to call
|
||||||
try:
|
try:
|
||||||
nbresults = int(feed.xpath("//div[@id='topbar']/b")[0].text)
|
nbresults = int(feed.xpath("//div[@id='topbar']/b")[0].text)
|
||||||
@ -162,22 +173,9 @@ class Query(object):
|
|||||||
pages =[feed]
|
pages =[feed]
|
||||||
if nbpagetoquery > 1:
|
if nbpagetoquery > 1:
|
||||||
for i in xrange(2, nbpagetoquery + 1):
|
for i in xrange(2, nbpagetoquery + 1):
|
||||||
try:
|
|
||||||
urldata = self.urldata + '&p=' + str(i)
|
urldata = self.urldata + '&p=' + str(i)
|
||||||
raw = browser.open_novisit(self.BASE_URL+urldata, timeout=timeout).read()
|
feed = self.brcall(browser, self.BASE_URL+urldata, verbose, timeout)
|
||||||
except Exception, e:
|
if feed is None:
|
||||||
continue
|
|
||||||
if '<title>404 - ' in raw:
|
|
||||||
continue
|
|
||||||
raw = xml_to_unicode(raw, strip_encoding_pats=True,
|
|
||||||
resolve_entities=True)[0]
|
|
||||||
try:
|
|
||||||
feed = soupparser.fromstring(raw)
|
|
||||||
except:
|
|
||||||
try:
|
|
||||||
#remove ASCII invalid chars
|
|
||||||
feed = soupparser.fromstring(clean_ascii_chars(raw))
|
|
||||||
except:
|
|
||||||
continue
|
continue
|
||||||
pages.append(feed)
|
pages.append(feed)
|
||||||
|
|
||||||
@ -270,11 +268,14 @@ class ResultList(list):
|
|||||||
try:
|
try:
|
||||||
raw = br.open_novisit(url).read()
|
raw = br.open_novisit(url).read()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
import socket
|
||||||
report(verbose)
|
report(verbose)
|
||||||
if callable(getattr(e, 'getcode', None)) and \
|
if callable(getattr(e, 'getcode', None)) and \
|
||||||
e.getcode() == 404:
|
e.getcode() == 404:
|
||||||
return None
|
return None
|
||||||
if isinstance(getattr(e, 'args', [None])[0], socket.timeout):
|
attr = getattr(e, 'args', [None])
|
||||||
|
attr = attr if attr else [None]
|
||||||
|
if isinstance(attr[0], socket.timeout):
|
||||||
raise NiceBooksError(_('NiceBooks timed out. Try again later.'))
|
raise NiceBooksError(_('NiceBooks timed out. Try again later.'))
|
||||||
raise NiceBooksError(_('NiceBooks encountered an error.'))
|
raise NiceBooksError(_('NiceBooks encountered an error.'))
|
||||||
if '<title>404 - ' in raw:
|
if '<title>404 - ' in raw:
|
||||||
@ -372,7 +373,10 @@ class Covers(object):
|
|||||||
self.urlimg.rpartition('.')[-1]
|
self.urlimg.rpartition('.')[-1]
|
||||||
return cover, ext if ext else 'jpg'
|
return cover, ext if ext else 'jpg'
|
||||||
except Exception, err:
|
except Exception, err:
|
||||||
if isinstance(getattr(err, 'args', [None])[0], socket.timeout):
|
import socket
|
||||||
|
attr = getattr(e, 'args', [None])
|
||||||
|
attr = attr if attr else [None]
|
||||||
|
if isinstance(attr[0], socket.timeout):
|
||||||
raise NiceBooksError(_('Nicebooks timed out. Try again later.'))
|
raise NiceBooksError(_('Nicebooks timed out. Try again later.'))
|
||||||
if not len(self.urlimg):
|
if not len(self.urlimg):
|
||||||
if not self.isbnf:
|
if not self.isbnf:
|
||||||
@ -407,6 +411,7 @@ def cover_from_isbn(isbn, timeout = 5.):
|
|||||||
|
|
||||||
|
|
||||||
def option_parser():
|
def option_parser():
|
||||||
|
import textwrap
|
||||||
parser = OptionParser(textwrap.dedent(\
|
parser = OptionParser(textwrap.dedent(\
|
||||||
_('''\
|
_('''\
|
||||||
%prog [options]
|
%prog [options]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user