Exemplo n.º 1
0
 def extract_embedded_content(self, doc):
     from calibre.ebooks.fb2 import base64_decode
     self.binary_map = {}
     for elem in doc.xpath('./*'):
         if elem.text and 'binary' in elem.tag and elem.attrib.has_key(
                 'id'):
             ct = elem.get('content-type', '')
             fname = elem.attrib['id']
             ext = ct.rpartition('/')[-1].lower()
             if ext in ('png', 'jpeg', 'jpg'):
                 if fname.lower().rpartition('.')[-1] not in {
                         'jpg', 'jpeg', 'png'
                 }:
                     fname += '.' + ext
                 self.binary_map[elem.get('id')] = fname
             raw = elem.text.strip()
             try:
                 data = base64_decode(raw)
             except TypeError:
                 self.log.exception(
                     'Binary data with id=%s is corrupted, ignoring' %
                     (elem.get('id')))
             else:
                 with open(fname, 'wb') as f:
                     f.write(data)
Exemplo n.º 2
0
def _parse_cover_data(root, imgid, mi, ctx):
    from calibre.ebooks.fb2 import base64_decode
    elm_binary = ctx.XPath('//fb:binary[@id="%s"]'%imgid)(root)
    if elm_binary:
        mimetype = elm_binary[0].get('content-type', 'image/jpeg')
        mime_extensions = guess_all_extensions(mimetype)

        if not mime_extensions and mimetype.startswith('image/'):
            mimetype_fromid = guess_type(imgid)[0]
            if mimetype_fromid and mimetype_fromid.startswith('image/'):
                mime_extensions = guess_all_extensions(mimetype_fromid)

        if mime_extensions:
            pic_data = elm_binary[0].text
            if pic_data:
                mi.cover_data = (mime_extensions[0][1:],
                        base64_decode(pic_data.strip()))
        else:
            prints("WARNING: Unsupported coverpage mime-type '%s' (id=#%s)" % (mimetype, imgid))
Exemplo n.º 3
0
def _parse_cover_data(root, imgid, mi, ctx):
    from calibre.ebooks.fb2 import base64_decode
    elm_binary = ctx.XPath('//fb:binary[@id="%s"]'%imgid)(root)
    if elm_binary:
        mimetype = elm_binary[0].get('content-type', 'image/jpeg')
        mime_extensions = guess_all_extensions(mimetype)

        if not mime_extensions and mimetype.startswith('image/'):
            mimetype_fromid = guess_type(imgid)[0]
            if mimetype_fromid and mimetype_fromid.startswith('image/'):
                mime_extensions = guess_all_extensions(mimetype_fromid)

        if mime_extensions:
            pic_data = elm_binary[0].text
            if pic_data:
                mi.cover_data = (mime_extensions[0][1:],
                        base64_decode(pic_data.strip()))
        else:
            prints("WARNING: Unsupported coverpage mime-type '%s' (id=#%s)" % (mimetype, imgid))
Exemplo n.º 4
0
 def extract_embedded_content(self, doc):
     from calibre.ebooks.fb2 import base64_decode
     self.binary_map = {}
     for elem in doc.xpath('./*'):
         if elem.text and 'binary' in elem.tag and 'id' in elem.attrib:
             ct = elem.get('content-type', '')
             fname = elem.attrib['id']
             ext = ct.rpartition('/')[-1].lower()
             if ext in ('png', 'jpeg', 'jpg'):
                 if fname.lower().rpartition('.')[-1] not in {'jpg', 'jpeg',
                         'png'}:
                     fname += '.' + ext
                 self.binary_map[elem.get('id')] = fname
             raw = elem.text.strip()
             try:
                 data = base64_decode(raw)
             except TypeError:
                 self.log.exception('Binary data with id=%s is corrupted, ignoring'%(
                     elem.get('id')))
             else:
                 with open(fname, 'wb') as f:
                     f.write(data)