From aa3ce26ff874b4eb61994555593a8d13872733ba Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 24 Mar 2011 09:54:36 -0600 Subject: [PATCH] ... --- src/calibre/ebooks/metadata/sources/amazon.py | 2 ++ src/calibre/ebooks/metadata/sources/base.py | 21 ++++++++++++++++++- src/calibre/ebooks/metadata/sources/google.py | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/metadata/sources/amazon.py b/src/calibre/ebooks/metadata/sources/amazon.py index c180850915..abf35a9465 100644 --- a/src/calibre/ebooks/metadata/sources/amazon.py +++ b/src/calibre/ebooks/metadata/sources/amazon.py @@ -172,6 +172,8 @@ class Worker(Thread): # {{{ self.plugin.cache_identifier_to_cover_url(self.amazon_id, self.cover_url) + self.plugin.clean_downloaded_metadata(mi) + self.result_queue.put(mi) def parse_asin(self, root): diff --git a/src/calibre/ebooks/metadata/sources/base.py b/src/calibre/ebooks/metadata/sources/base.py index 349ff9462b..345bdc2f8c 100644 --- a/src/calibre/ebooks/metadata/sources/base.py +++ b/src/calibre/ebooks/metadata/sources/base.py @@ -13,6 +13,7 @@ from calibre import browser, random_user_agent from calibre.customize import Plugin from calibre.utils.logging import ThreadSafeLog, FileStream from calibre.utils.config import JSONConfig +from calibre.utils.titlecase import titlecase msprefs = JSONConfig('metadata_sources.json') @@ -128,7 +129,7 @@ class Source(Plugin): # }}} - # Utility functions {{{ + # Caching {{{ def get_related_isbns(self, id_): with self.cache_lock: @@ -152,6 +153,10 @@ class Source(Plugin): with self.cache_lock: return self._identifier_to_cover_url_cache.get(id_, None) + # }}} + + # Utility functions {{{ + def get_author_tokens(self, authors, only_first_author=True): ''' Take a list of authors and return a list of tokens useful for an @@ -216,6 +221,20 @@ class Source(Plugin): elif mi.is_null(key): return key + def clean_downloaded_metadata(self, mi): + ''' + Call this method in your plugin's identify method to normalize metadata + before putting the Metadata object into result_queue. You can of + course, use a custom algorithm suited to your metadata source. + ''' + def fixcase(x): + if x: + x = titlecase(x) + return x + if mi.title: + mi.title = fixcase(mi.title) + mi.authors = list(map(fixcase, mi.authors)) + mi.tags = list(map(fixcase, mi.tags)) # }}} diff --git a/src/calibre/ebooks/metadata/sources/google.py b/src/calibre/ebooks/metadata/sources/google.py index b03dccb766..989320f710 100644 --- a/src/calibre/ebooks/metadata/sources/google.py +++ b/src/calibre/ebooks/metadata/sources/google.py @@ -256,6 +256,7 @@ class GoogleBooks(Source): if ans.has_google_cover: self.cache_identifier_to_cover_url(goog, self.GOOGLE_COVER%goog) + self.clean_downloaded_metadata(ans) result_queue.put(ans) except: log.exception(