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

View File

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