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,6 +33,9 @@ def fetch_metadata(url, max=100):
books = []
page_number = 1
total_results = sys.maxint
timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(2.)
try:
while len(books) < total_results and max > 0:
try:
raw = urlopen(url).read()
@ -49,6 +52,8 @@ def fetch_metadata(url, max=100):
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_()