Use HEAD request to check for covers

This commit is contained in:
Kovid Goyal 2010-06-27 12:36:12 -06:00
parent 1e137b7ee7
commit d814a9af84

View File

@ -7,6 +7,7 @@ Fetch cover from LibraryThing.com based on ISBN number.
import sys, socket, os, re import sys, socket, os, re
from lxml import html from lxml import html
import mechanize
from calibre import browser, prints from calibre import browser, prints
from calibre.utils.config import OptionParser from calibre.utils.config import OptionParser
@ -14,11 +15,17 @@ from calibre.ebooks.BeautifulSoup import BeautifulSoup
OPENLIBRARY = 'http://covers.openlibrary.org/b/isbn/%s-L.jpg?default=false' OPENLIBRARY = 'http://covers.openlibrary.org/b/isbn/%s-L.jpg?default=false'
class HeadRequest(mechanize.Request):
def get_method(self):
return 'HEAD'
def check_for_cover(isbn, timeout=5.): def check_for_cover(isbn, timeout=5.):
br = browser() br = browser()
br.set_handle_redirect(False) br.set_handle_redirect(False)
try: try:
br.open_novisit(OPENLIBRARY%isbn, timeout=timeout) br.open_novisit(HeadRequest(OPENLIBRARY%isbn), timeout=timeout)
return True
except Exception, e: except Exception, e:
if callable(getattr(e, 'getcode', None)) and e.getcode() == 302: if callable(getattr(e, 'getcode', None)) and e.getcode() == 302:
return True return True