Use reasonable timeouts when fetching metadata from server

This commit is contained in:
Kovid Goyal 2007-11-15 02:05:20 +00:00
parent 29ecca2c11
commit 3086b89634
2 changed files with 27 additions and 18 deletions

View File

@ -16,7 +16,7 @@
Interface to isbndb.com. My key HLLXQX2A. Interface to isbndb.com. My key HLLXQX2A.
''' '''
import sys, logging, re import sys, logging, re, socket
from urllib import urlopen, quote from urllib import urlopen, quote
from optparse import OptionParser from optparse import OptionParser
@ -33,22 +33,27 @@ def fetch_metadata(url, max=100):
books = [] books = []
page_number = 1 page_number = 1
total_results = sys.maxint total_results = sys.maxint
while len(books) < total_results and max > 0: timeout = socket.getdefaulttimeout()
try: socket.setdefaulttimeout(2.)
raw = urlopen(url).read() try:
except Exception, err: while len(books) < total_results and max > 0:
raise ISBNDBError('Could not fetch ISBNDB metadata. Error: '+str(err)) try:
soup = BeautifulStoneSoup(raw) raw = urlopen(url).read()
book_list = soup.find('booklist') except Exception, err:
if book_list is None: raise ISBNDBError('Could not fetch ISBNDB metadata. Error: '+str(err))
errmsg = soup.find('errormessage').string soup = BeautifulStoneSoup(raw)
raise ISBNDBError('Error fetching metadata: '+errmsg) book_list = soup.find('booklist')
total_results = int(book_list['total_results']) if book_list is None:
np = '&page_number=%s&'%(page_number+1) errmsg = soup.find('errormessage').string
url = re.sub(r'\&page_number=\d+\&', np, url) raise ISBNDBError('Error fetching metadata: '+errmsg)
books.extend(book_list.findAll('bookdata')) total_results = int(book_list['total_results'])
max -= 1 np = '&page_number=%s&'%(page_number+1)
return books url = re.sub(r'\&page_number=\d+\&', np, url)
books.extend(book_list.findAll('bookdata'))
max -= 1
return books
finally:
socket.setdefaulttimeout(timeout)
class ISBNDBMetadata(MetaInformation): class ISBNDBMetadata(MetaInformation):

View File

@ -16,7 +16,7 @@
The dialog used to edit meta information for a book as well as The dialog used to edit meta information for a book as well as
add/remove formats add/remove formats
''' '''
import os, urllib import os, urllib, socket
from PyQt4.QtCore import SIGNAL, QObject, QCoreApplication, Qt from PyQt4.QtCore import SIGNAL, QObject, QCoreApplication, Qt
from PyQt4.QtGui import QPixmap, QListWidgetItem, QErrorMessage, QDialog from PyQt4.QtGui import QPixmap, QListWidgetItem, QErrorMessage, QDialog
@ -207,6 +207,8 @@ class MetadataSingleDialog(QDialog, Ui_MetadataSingleDialog):
self.fetch_cover_button.setEnabled(False) self.fetch_cover_button.setEnabled(False)
self.setCursor(Qt.WaitCursor) self.setCursor(Qt.WaitCursor)
QCoreApplication.instance().processEvents() QCoreApplication.instance().processEvents()
timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(5.)
try: try:
src = urllib.urlopen('http://www.librarything.com/isbn/'+isbn).read() src = urllib.urlopen('http://www.librarything.com/isbn/'+isbn).read()
s = BeautifulSoup(src) s = BeautifulSoup(src)
@ -225,6 +227,8 @@ class MetadataSingleDialog(QDialog, Ui_MetadataSingleDialog):
finally: finally:
self.fetch_cover_button.setEnabled(True) self.fetch_cover_button.setEnabled(True)
self.unsetCursor() self.unsetCursor()
socket.setdefaulttimeout(timeout)
else: else:
error_dialog(self, 'Cannot fetch cover', 'You must specify the ISBN identifier for this book.').exec_() error_dialog(self, 'Cannot fetch cover', 'You must specify the ISBN identifier for this book.').exec_()