mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
This commit is contained in:
parent
0ed6ddf783
commit
634bd7d203
@ -58,13 +58,16 @@ class MetaInformation(object):
|
|||||||
self.series = None
|
self.series = None
|
||||||
self.series_index = None
|
self.series_index = None
|
||||||
self.rating = None
|
self.rating = None
|
||||||
|
self.isbn = None
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
ans = ''
|
ans = ''
|
||||||
ans += 'Title : ' + str(self.title) + '\n'
|
ans += 'Title : ' + unicode(self.title) + '\n'
|
||||||
ans += 'Author : ' + (', '.join(self.authors) if self.authors is not None else 'None') + '\n'
|
ans += 'Author : ' + (', '.join(self.authors) if self.authors is not None else 'None')
|
||||||
ans += 'Category: ' + str(self.category) + '\n'
|
ans += ((' (' + self.author_sort + ')') if self.author_sort else '') + '\n'
|
||||||
ans += 'Comments: ' + str(self.comments) + '\n'
|
ans += 'Category: ' + unicode(self.category) + '\n'
|
||||||
|
ans += 'Comments: ' + unicode(self.comments) + '\n'
|
||||||
|
ans += 'ISBN : ' + unicode(self.isbn) + '\n'
|
||||||
return ans.strip()
|
return ans.strip()
|
||||||
|
|
||||||
def __nonzero__(self):
|
def __nonzero__(self):
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
## with this program; if not, write to the Free Software Foundation, Inc.,
|
## with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
'''
|
'''
|
||||||
Interface to isbndb.com
|
Interface to isbndb.com. My key HLLXQX2A.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import sys, logging, re
|
import sys, logging, re
|
||||||
@ -21,12 +21,52 @@ from urllib import urlopen, quote
|
|||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
from libprs500 import __appname__, __version__, __author__, setup_cli_handlers
|
from libprs500 import __appname__, __version__, __author__, setup_cli_handlers
|
||||||
|
from libprs500.ebooks.metadata import MetaInformation
|
||||||
|
from libprs500.ebooks.BeautifulSoup import BeautifulStoneSoup
|
||||||
|
|
||||||
BASE_URL = 'http://isbndb.com/api/books.xml?access_key=%(key)s&results=subjects,authors&'
|
BASE_URL = 'http://isbndb.com/api/books.xml?access_key=%(key)s&page_number=1&results=subjects,authors&'
|
||||||
|
|
||||||
class ISNDBError(Exception):
|
class ISBNDBError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
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')
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
class ISBNDBMetadata(MetaInformation):
|
||||||
|
def __init__(self, book):
|
||||||
|
MetaInformation.__init__(self, None, [])
|
||||||
|
|
||||||
|
self.isbn = book['isbn']
|
||||||
|
self.title = book.find('titlelong').string
|
||||||
|
if not self.title:
|
||||||
|
self.title = book.find('title').string
|
||||||
|
self.title = self.title.strip
|
||||||
|
au = book.find('authorstext').string
|
||||||
|
temp = au.split(',')
|
||||||
|
self.authors = []
|
||||||
|
for au in temp:
|
||||||
|
self.authors.extend([a.strip() for a in au.split('&')])
|
||||||
|
|
||||||
|
self.author_sort = book.find('authors').find('person').string
|
||||||
|
self.publisher = book.find('publishertext').string
|
||||||
|
|
||||||
|
|
||||||
def build_isbn(base_url, opts):
|
def build_isbn(base_url, opts):
|
||||||
return base_url + 'index1=isbn&value1='+opts.isbn
|
return base_url + 'index1=isbn&value1='+opts.isbn
|
||||||
|
|
||||||
@ -37,9 +77,9 @@ def build_combined(base_url, opts):
|
|||||||
query += ' ' + e
|
query += ' ' + e
|
||||||
query = query.strip()
|
query = query.strip()
|
||||||
if len(query) == 0:
|
if len(query) == 0:
|
||||||
raise ISNDBError('You must specify at least one of --author, --title or --publisher')
|
raise ISBNDBError('You must specify at least one of --author, --title or --publisher')
|
||||||
|
|
||||||
query = re.sub('\s+', '+', query)
|
query = re.sub(r'\s+', '+', query)
|
||||||
return base_url+'index1=combined&value1='+quote(query, '+')
|
return base_url+'index1=combined&value1='+quote(query, '+')
|
||||||
|
|
||||||
|
|
||||||
@ -69,13 +109,7 @@ key is the account key you generate after signing up for a free account from isb
|
|||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def main(args=sys.argv, logger=None):
|
def create_books(opts, args, logger=None):
|
||||||
parser = option_parser()
|
|
||||||
opts, args = parser.parse_args(args)
|
|
||||||
if len(args) != 2:
|
|
||||||
parser.print_help()
|
|
||||||
print('You must supply the isbndb.com key')
|
|
||||||
return 1
|
|
||||||
if logger is None:
|
if logger is None:
|
||||||
level = logging.DEBUG if opts.verbose else logging.INFO
|
level = logging.DEBUG if opts.verbose else logging.INFO
|
||||||
logger = logging.getLogger('isbndb')
|
logger = logging.getLogger('isbndb')
|
||||||
@ -89,6 +123,18 @@ def main(args=sys.argv, logger=None):
|
|||||||
|
|
||||||
logger.info('ISBNDB query: '+url)
|
logger.info('ISBNDB query: '+url)
|
||||||
|
|
||||||
|
return [ISBNDBMetadata(book) for book in fetch_metadata(url)]
|
||||||
|
|
||||||
|
def main(args=sys.argv):
|
||||||
|
parser = option_parser()
|
||||||
|
opts, args = parser.parse_args(args)
|
||||||
|
if len(args) != 2:
|
||||||
|
parser.print_help()
|
||||||
|
print('You must supply the isbndb.com key')
|
||||||
|
return 1
|
||||||
|
|
||||||
|
for book in create_books(opts, args):
|
||||||
|
print book
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user