Modify amazon to get social meta and split in 2 plugins

This commit is contained in:
Sengian 2010-12-11 22:07:35 +01:00
parent 34c6caeeec
commit d5bc18b5c2
3 changed files with 143 additions and 100 deletions

View File

@ -480,10 +480,10 @@ from calibre.devices.misc import PALMPRE, AVANT, SWEEX, PDNOVEL, KOGAN, \
from calibre.devices.folder_device.driver import FOLDER_DEVICE_FOR_CONFIG from calibre.devices.folder_device.driver import FOLDER_DEVICE_FOR_CONFIG
from calibre.devices.kobo.driver import KOBO from calibre.devices.kobo.driver import KOBO
from calibre.ebooks.metadata.fetch import GoogleBooks, ISBNDB, Amazon, \ from calibre.ebooks.metadata.fetch import GoogleBooks, ISBNDB, LibraryThing
LibraryThing
from calibre.ebooks.metadata.douban import DoubanBooks from calibre.ebooks.metadata.douban import DoubanBooks
from calibre.ebooks.metadata.nicebooks import NiceBooks, NiceBooksCovers from calibre.ebooks.metadata.nicebooks import NiceBooks, NiceBooksCovers
from calibre.ebooks.metadata.amazonbis import Amazon, AmazonSocial
from calibre.ebooks.metadata.fictionwise import Fictionwise from calibre.ebooks.metadata.fictionwise import Fictionwise
from calibre.ebooks.metadata.covers import OpenLibraryCovers, \ from calibre.ebooks.metadata.covers import OpenLibraryCovers, \
LibraryThingCovers, DoubanCovers LibraryThingCovers, DoubanCovers
@ -491,7 +491,7 @@ from calibre.library.catalog import CSV_XML, EPUB_MOBI, BIBTEX
from calibre.ebooks.epub.fix.unmanifested import Unmanifested from calibre.ebooks.epub.fix.unmanifested import Unmanifested
from calibre.ebooks.epub.fix.epubcheck import Epubcheck from calibre.ebooks.epub.fix.epubcheck import Epubcheck
plugins = [HTML2ZIP, PML2PMLZ, ArchiveExtract, GoogleBooks, ISBNDB, Amazon, plugins = [HTML2ZIP, PML2PMLZ, ArchiveExtract, GoogleBooks, ISBNDB, Amazon, AmazonSocial,
LibraryThing, DoubanBooks, NiceBooks, Fictionwise, CSV_XML, EPUB_MOBI, BIBTEX, LibraryThing, DoubanBooks, NiceBooks, Fictionwise, CSV_XML, EPUB_MOBI, BIBTEX,
Unmanifested, Epubcheck, OpenLibraryCovers, LibraryThingCovers, DoubanCovers, Unmanifested, Epubcheck, OpenLibraryCovers, LibraryThingCovers, DoubanCovers,
NiceBooksCovers] NiceBooksCovers]

View File

@ -19,73 +19,56 @@ from calibre.utils.config import OptionParser
from calibre.library.comments import sanitize_comments_html from calibre.library.comments import sanitize_comments_html
class AmazonFr(MetadataSource): # class AmazonFr(MetadataSource):
name = 'Amazon French' # name = 'Amazon French'
description = _('Downloads metadata from amazon.fr') # description = _('Downloads metadata from amazon.fr')
supported_platforms = ['windows', 'osx', 'linux'] # supported_platforms = ['windows', 'osx', 'linux']
author = 'Sengian' # author = 'Sengian'
version = (1, 0, 0) # version = (1, 0, 0)
has_html_comments = True # has_html_comments = True
def fetch(self): # def fetch(self):
try: # try:
self.results = search(self.title, self.book_author, self.publisher, # self.results = search(self.title, self.book_author, self.publisher,
self.isbn, max_results=10, verbose=self.verbose, lang='fr') # self.isbn, max_results=10, verbose=self.verbose, lang='fr')
except Exception, e: # except Exception, e:
self.exception = e # self.exception = e
self.tb = traceback.format_exc() # self.tb = traceback.format_exc()
class AmazonEs(MetadataSource): # class AmazonEs(MetadataSource):
name = 'Amazon Spanish' # name = 'Amazon Spanish'
description = _('Downloads metadata from amazon.com in spanish') # description = _('Downloads metadata from amazon.com in spanish')
supported_platforms = ['windows', 'osx', 'linux'] # supported_platforms = ['windows', 'osx', 'linux']
author = 'Sengian' # author = 'Sengian'
version = (1, 0, 0) # version = (1, 0, 0)
has_html_comments = True # has_html_comments = True
def fetch(self): # def fetch(self):
try: # try:
self.results = search(self.title, self.book_author, self.publisher, # self.results = search(self.title, self.book_author, self.publisher,
self.isbn, max_results=10, verbose=self.verbose, lang='es') # self.isbn, max_results=10, verbose=self.verbose, lang='es')
except Exception, e: # except Exception, e:
self.exception = e # self.exception = e
self.tb = traceback.format_exc() # self.tb = traceback.format_exc()
class AmazonEn(MetadataSource): # class AmazonDe(MetadataSource):
name = 'Amazon English' # name = 'Amazon German'
description = _('Downloads metadata from amazon.com in english') # description = _('Downloads metadata from amazon.de')
supported_platforms = ['windows', 'osx', 'linux'] # supported_platforms = ['windows', 'osx', 'linux']
author = 'Sengian' # author = 'Sengian'
version = (1, 0, 0) # version = (1, 0, 0)
has_html_comments = True # has_html_comments = True
def fetch(self): # def fetch(self):
try: # try:
self.results = search(self.title, self.book_author, self.publisher, # self.results = search(self.title, self.book_author, self.publisher,
self.isbn, max_results=10, verbose=self.verbose, lang='en') # self.isbn, max_results=10, verbose=self.verbose, lang='de')
except Exception, e: # except Exception, e:
self.exception = e # self.exception = e
self.tb = traceback.format_exc() # self.tb = traceback.format_exc()
class AmazonDe(MetadataSource):
name = 'Amazon German'
description = _('Downloads metadata from amazon.de')
supported_platforms = ['windows', 'osx', 'linux']
author = 'Sengian'
version = (1, 0, 0)
has_html_comments = True
def fetch(self):
try:
self.results = search(self.title, self.book_author, self.publisher,
self.isbn, max_results=10, verbose=self.verbose, lang='de')
except Exception, e:
self.exception = e
self.tb = traceback.format_exc()
class Amazon(MetadataSource): class Amazon(MetadataSource):
@ -93,15 +76,31 @@ class Amazon(MetadataSource):
description = _('Downloads metadata from amazon.com') description = _('Downloads metadata from amazon.com')
supported_platforms = ['windows', 'osx', 'linux'] supported_platforms = ['windows', 'osx', 'linux']
author = 'Kovid Goyal & Sengian' author = 'Kovid Goyal & Sengian'
version = (1, 1, 0) version = (1, 0, 0)
has_html_comments = True has_html_comments = True
def fetch(self): def fetch(self):
# if not self.site_customization:
# return
try: try:
self.results = search(self.title, self.book_author, self.publisher, self.results = search(self.title, self.book_author, self.publisher,
self.isbn, max_results=10, verbose=self.verbose, lang='all') self.isbn, max_results=5, verbose=self.verbose, lang='all')
except Exception, e:
self.exception = e
self.tb = traceback.format_exc()
class AmazonSocial(MetadataSource):
name = 'AmazonSocial'
metadata_type = 'social'
description = _('Downloads social metadata from amazon.com')
supported_platforms = ['windows', 'osx', 'linux']
author = 'Kovid Goyal & Sengian'
version = (1, 0, 1)
has_html_comments = True
def fetch(self):
try:
self.results = search(self.title, self.book_author, self.publisher,
self.isbn, max_results=5, verbose=self.verbose, lang='all')
except Exception, e: except Exception, e:
self.exception = e self.exception = e
self.tb = traceback.format_exc() self.tb = traceback.format_exc()
@ -450,7 +449,6 @@ class ResultList(list):
return None return None
def populate(self, entries, br, verbose=False): def populate(self, entries, br, verbose=False):
#multiple entries
for x in entries: for x in entries:
entry = self.get_individual_metadata(x, br, verbose) entry = self.get_individual_metadata(x, br, verbose)
if entry is not None: if entry is not None:
@ -471,13 +469,40 @@ def search(title=None, author=None, publisher=None, isbn=None,
keywords=keywords, max_results=max_results,rlang=lang)(br, verbose) keywords=keywords, max_results=max_results,rlang=lang)(br, verbose)
if entries is None or len(entries) == 0: if entries is None or len(entries) == 0:
return return None
#List of entry #List of entry
ans = ResultList(baseurl, lang) ans = ResultList(baseurl, lang)
ans.populate(entries, br, verbose) ans.populate(entries, br, verbose)
return [x for x in ans if x is not None] return [x for x in ans if x is not None]
def get_social_metadata(title, authors, publisher, isbn, verbose=False,
max_results=1, lang='all'):
mi = MetaInformation(title, authors)
if not isbn or not check_isbn(isbn):
return [mi]
amazresults = search(isbn=isbn, verbose=verbose,
max_results=max_results, lang='all')
if amazresults is None or amazresults[0] is None:
from calibre.ebooks.metadata.xisbn import xisbn
for i in xisbn.get_associated_isbns(isbn):
amazresults = search(isbn=i, verbose=verbose,
max_results=max_results, lang='all')
if amazresults is not None and amazresults[0] is not None:
break
if amazresults is None or amazresults[0] is None:
return [mi]
miaz = amazresults[0]
if miaz.rating is not None:
mi.rating = miaz.rating
if miaz.comments is not None:
mi.comments = miaz.comments
if miaz.tags is not None:
mi.tags = miaz.tags
return [mi]
def option_parser(): def option_parser():
parser = OptionParser(textwrap.dedent(\ parser = OptionParser(textwrap.dedent(\
_('''\ _('''\
@ -490,41 +515,59 @@ def option_parser():
All & english & french & german & spanish All & english & french & german & spanish
''' '''
))) )))
parser.add_option('-t', '--title', help='Book title') parser.add_option('-t', '--title', help=_('Book title'))
parser.add_option('-a', '--author', help='Book author(s)') parser.add_option('-a', '--author', help=_('Book author(s)'))
parser.add_option('-p', '--publisher', help='Book publisher') parser.add_option('-p', '--publisher', help=_('Book publisher'))
parser.add_option('-i', '--isbn', help='Book ISBN') parser.add_option('-i', '--isbn', help=_('Book ISBN'))
parser.add_option('-k', '--keywords', help='Keywords') parser.add_option('-k', '--keywords', help=_('Keywords'))
parser.add_option('-s', '--social', default=0, action='count',
help=_('Get social data only'))
parser.add_option('-m', '--max-results', default=10, parser.add_option('-m', '--max-results', default=10,
help='Maximum number of results to fetch') help=_('Maximum number of results to fetch'))
parser.add_option('-l', '--lang', default='all', parser.add_option('-l', '--lang', default='all',
help='Chosen language for metadata search (all, en, fr, es, de)') help=_('Chosen language for metadata search (all, en, fr, es, de)'))
parser.add_option('-v', '--verbose', default=0, action='count', parser.add_option('-v', '--verbose', default=0, action='count',
help='Be more verbose about errors') help=_('Be more verbose about errors'))
return parser return parser
def main(args=sys.argv): def main(args=sys.argv):
parser = option_parser() parser = option_parser()
opts, args = parser.parse_args(args) opts, args = parser.parse_args(args)
try: try:
results = search(opts.title, opts.author, isbn=opts.isbn, publisher=opts.publisher, if opts.social:
keywords=opts.keywords, verbose=opts.verbose, max_results=opts.max_results, results = get_social_metadata(opts.title, opts.author,
lang=opts.lang) opts.publisher, opts.isbn, verbose=opts.verbose, lang=opts.lang)
else:
results = search(opts.title, opts.author, isbn=opts.isbn,
publisher=opts.publisher, keywords=opts.keywords, verbose=opts.verbose,
max_results=opts.max_results, lang=opts.lang)
except AssertionError: except AssertionError:
report(True) report(True)
parser.print_help() parser.print_help()
return 1 return 1
if results is None or len(results) == 0: if results is None and len(results) == 0:
print _('No result found for this search!') print _('No result found for this search!')
return 0 return 0
for result in results: for result in results:
print unicode(result).encode(preferred_encoding, 'replace') print unicode(result).encode(preferred_encoding, 'replace')
print print
#test social
# '''Test xisbn'''
# print get_social_metadata('Learning Python', None, None, '8324616489')[0]
# print
# '''Test sophisticated comment formatting'''
# print get_social_metadata('Angels & Demons', None, None, '9781416580829')[0]
# print
# '''Random tests'''
# print get_social_metadata('Star Trek: Destiny: Mere Mortals', None, None, '9781416551720')[0]
# print
# print get_social_metadata('The Great Gatsby', None, None, '0743273567')[0]
if __name__ == '__main__': if __name__ == '__main__':
# sys.exit(main()) sys.exit(main())
import cProfile # import cProfile
# sys.exit(cProfile.run("import calibre.ebooks.metadata.amazonfr; calibre.ebooks.metadata.amazonfr.main()")) # sys.exit(cProfile.run("import calibre.ebooks.metadata.amazonbis; calibre.ebooks.metadata.amazonbis.main()"))
sys.exit(cProfile.run("import calibre.ebooks.metadata.amazonfr; calibre.ebooks.metadata.amazonfr.main()", "profile_tmp_2")) # sys.exit(cProfile.run("import calibre.ebooks.metadata.amazonbis; calibre.ebooks.metadata.amazonbis.main()", "profile_tmp_2"))
# calibre-debug -e "H:\Mes eBooks\Developpement\calibre\src\calibre\ebooks\metadata\amazonfr.py" -m 5 -a gore -v>data.html # calibre-debug -e "H:\Mes eBooks\Developpement\calibre\src\calibre\ebooks\metadata\amazonbis.py" -m 5 -a gore -v>data.html

View File

@ -226,24 +226,24 @@ class ISBNDB(MetadataSource): # {{{
# }}} # }}}
class Amazon(MetadataSource): # {{{ # class Amazon(MetadataSource): # {{{
name = 'Amazon' # name = 'Amazon'
metadata_type = 'social' # metadata_type = 'social'
description = _('Downloads social metadata from amazon.com') # description = _('Downloads social metadata from amazon.com')
has_html_comments = True # has_html_comments = True
def fetch(self): # def fetch(self):
if not self.isbn: # if not self.isbn:
return # return
from calibre.ebooks.metadata.amazon import get_social_metadata # from calibre.ebooks.metadata.amazon import get_social_metadata
try: # try:
self.results = get_social_metadata(self.title, self.book_author, # self.results = get_social_metadata(self.title, self.book_author,
self.publisher, self.isbn) # self.publisher, self.isbn)
except Exception, e: # except Exception, e:
self.exception = e # self.exception = e
self.tb = traceback.format_exc() # self.tb = traceback.format_exc()
# }}} # }}}