From 2960781bb022dd7757def1b966091402d1e77393 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 17 Jul 2008 09:46:36 -0700 Subject: [PATCH] Add support for extracting cover images to fb2-meta --- src/calibre/ebooks/metadata/fb2.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/calibre/ebooks/metadata/fb2.py b/src/calibre/ebooks/metadata/fb2.py index 5bffb47409..672fc3e9ee 100644 --- a/src/calibre/ebooks/metadata/fb2.py +++ b/src/calibre/ebooks/metadata/fb2.py @@ -5,7 +5,8 @@ __copyright__ = '2008, Anatoly Shipitsin ' '''Read meta information from fb2 files''' -import sys, os +import sys, os, mimetypes +from base64 import b64decode from calibre.ebooks.BeautifulSoup import BeautifulStoneSoup from calibre.ebooks.metadata import MetaInformation @@ -18,15 +19,30 @@ def get_metadata(stream): author= [firstname+" "+lastname] title = soup.find("book-title").string comments = soup.find("annotation") + cp = soup.find('coverpage') + cdata = None + if cp: + cimage = cp.find('image', attrs={'l:href':True}) + if cimage: + id = cimage['l:href'].replace('#', '') + binary = soup.find('binary', id=id, attrs={'content-type':True}) + if binary: + mt = binary['content-type'] + exts = mimetypes.guess_all_extensions(mt) + if not exts: + exts = ['.jpg'] + cdata = (exts[0][1:], b64decode(binary.string.strip())) + if comments and len(comments) > 1: - comments = comments.p.contents[0] + comments = comments.p.contents[0] series = soup.find("sequence") - # series_index = series.index mi = MetaInformation(title, author) mi.comments = comments + mi.author_sort = lastname+'; '+firstname if series: mi.series = series.get('name', None) - # mi.series_index = series_index + if cdata: + mi.cover_data = cdata return mi def main(args=sys.argv):