# Path: modules/mobi_split.py
# class mobi_read:
#     def __init__(self, infile, width=330, height=470, stretch=False):
# 
#         self.asin = ''
#         self.cdetype = 'PDOC'
#         self.cdecontentkey = ''
#         self.acr = ''
#         self.thumbnail = None
#         self.pagedata = b''
# 
#         datain = b''
#         with open(pathof(infile), 'rb') as f:
#             self.acr = re.sub('[^-A-Za-z0-9 ]+', '_', f.read(32).replace(b'\x00', b'').decode('latin-1'))
#             f.seek(0)
#             datain = f.read()
# 
#         datain_rec0 = readsection(datain, 0)
# 
#         exth121 = read_exth(datain_rec0, 121)
#         self.combo = True
#         if len(exth121) == 0:
#             self.combo = False
#         else:
#             # only pay attention to first exth121
#             # (there should only be one)
#             datain_kf8, = struct.unpack_from(b'>L', exth121[0], 0)
#             if datain_kf8 == 0xffffffff:
#                 self.combo = False
# 
#         # Look for PageMap
#         srcs = getint(datain_rec0, first_non_text)
#         num_srcs = getint(datain, number_of_pdb_records, b'H')
#         if srcs != 0xffffffff and num_srcs > 0:
#             for i in range(srcs, srcs + num_srcs):
#                 data = readsection(datain, i)
#                 if data[0:4] == b"PAGE":
#                     self.pagedata = data
# 
#         exth = read_exth(datain_rec0, exth_asin)
#         if len(exth) > 0:
#             self.asin = exth[0].decode("ascii")
#         exth = read_exth(datain_rec0, exth_cdetype)
#         if len(exth) > 0:
#             self.cdetype = exth[0].decode("ascii")
#         exth = read_exth(datain_rec0, exth_cdecontentkey)
#         if len(exth) > 0:
#             self.cdecontentkey = exth[0].decode("ascii")
# 
#         firstimage = getint(datain_rec0, first_resc_record)
# 
#         exth_cover = read_exth(datain_rec0, exth_cover_offset)
#         if len(exth_cover) > 0:
#             cover_index, = struct.unpack_from('>L', exth_cover[0], 0)
#             cover_index += firstimage
#         else:
#             cover_index = 0xffffffff
# 
#         exth_thumb = read_exth(datain_rec0, exth_thumb_offset)
#         if len(exth_thumb) > 0:
#             thumb_index, = struct.unpack_from('>L', exth_thumb[0], 0)
#             thumb_index += firstimage
#         else:
#             thumb_index = 0xffffffff
# 
#         if cover_index != 0xffffffff:
#             w, h = 0, 0
#             if thumb_index != 0xffffffff:
#                 image = readsection(datain, thumb_index)
#                 self.thumbnail = Image.open(BytesIO(image))
#                 w, h = self.thumbnail.size
#             if (w < width and h < height) or stretch:
#                 image = readsection(datain, cover_index)
#                 self.thumbnail = Image.open(BytesIO(image))
#                 if stretch:
#                     self.thumbnail = self.thumbnail.resize((width, height), Image.LANCZOS)
#                 else:
#                     self.thumbnail.thumbnail((width, height), Image.LANCZOS)
# 
#         if self.combo:
#             # always try to use information from KF8 part
#             datain_kfrec0 = readsection(datain, datain_kf8)
#             exth = read_exth(datain_kfrec0, exth_asin)
#             if len(exth) > 0:
#                 self.asin = exth[0].decode("ascii")
#             exth = read_exth(datain_kfrec0, exth_cdetype)
#             if len(exth) > 0:
#                 self.cdetype = exth[0].decode("ascii")
#             exth = read_exth(datain_kfrec0, exth_cdecontentkey)
#             if len(exth) > 0:
#                 self.cdecontentkey = exth[0].decode("ascii")
# 
#     def getACR(self):
#         return self.acr
# 
#     def getASIN(self):
#         return self.asin
# 
#     def getCdeType(self):
#         return self.cdetype
# 
#     def getCdeContentKey(self):
#         return self.cdecontentkey
# 
#     def getPageData(self):
#         return self.pagedata
# 
#     def getThumbnail(self):
#         return self.thumbnail


import os
import sys
import traceback
import argparse
import version
from modules.mobi_split import mobi_read
            asin = reader.getASIN()
        if asin:
            thumb = reader.getThumbnail()
            if thumb != None:
                thumb.convert('RGB').save(os.path.join(kindle_dir, 'thumbnail_' + asin + '_' + reader.getCdeType() + '_portrait.jpg'), 'JPEG')
                count_processed += 1
                if verbose:
                    print('Written thumbnail for {}'.format(asin))
            else:
                if verbose:
                    print("Skipping - no cover or thumbnail")
        else:
            if verbose:
                print("Skipping - no ASIN")
    except:
        print('ERROR: processing file "{}".'.format(infile))
        # traceback.print_exc()

    return


def process_folder(inputdir, width, height, stretch, verbose):

    if os.path.isdir(inputdir):
        # let's see if we could locate kindle directory
        head = tail = inputdir
        while tail:
            head, tail = os.path.split(head)
            if tail:
                kindle_dir = os.path.join(head, 'system', 'thumbnails')

# Path: modules/mobi_split.py
# class mobi_read:
#     def __init__(self, infile, width=330, height=470, stretch=False):
# 
#         self.asin = ''
#         self.cdetype = 'PDOC'
#         self.cdecontentkey = ''
#         self.acr = ''
#         self.thumbnail = None
#         self.pagedata = b''
# 
#         datain = b''
#         with open(pathof(infile), 'rb') as f:
#             self.acr = re.sub('[^-A-Za-z0-9 ]+', '_', f.read(32).replace(b'\x00', b'').decode('latin-1'))
#             f.seek(0)
#             datain = f.read()
# 
#         datain_rec0 = readsection(datain, 0)
# 
#         exth121 = read_exth(datain_rec0, 121)
#         self.combo = True
#         if len(exth121) == 0:
#             self.combo = False
#         else:
#             # only pay attention to first exth121
#             # (there should only be one)
#             datain_kf8, = struct.unpack_from(b'>L', exth121[0], 0)
#             if datain_kf8 == 0xffffffff:
#                 self.combo = False
# 
#         # Look for PageMap
#         srcs = getint(datain_rec0, first_non_text)
#         num_srcs = getint(datain, number_of_pdb_records, b'H')
#         if srcs != 0xffffffff and num_srcs > 0:
#             for i in range(srcs, srcs + num_srcs):
#                 data = readsection(datain, i)
#                 if data[0:4] == b"PAGE":
#                     self.pagedata = data
# 
#         exth = read_exth(datain_rec0, exth_asin)
#         if len(exth) > 0:
#             self.asin = exth[0].decode("ascii")
#         exth = read_exth(datain_rec0, exth_cdetype)
#         if len(exth) > 0:
#             self.cdetype = exth[0].decode("ascii")
#         exth = read_exth(datain_rec0, exth_cdecontentkey)
#         if len(exth) > 0:
#             self.cdecontentkey = exth[0].decode("ascii")
# 
#         firstimage = getint(datain_rec0, first_resc_record)
# 
#         exth_cover = read_exth(datain_rec0, exth_cover_offset)
#         if len(exth_cover) > 0:
#             cover_index, = struct.unpack_from('>L', exth_cover[0], 0)
#             cover_index += firstimage
#         else:
#             cover_index = 0xffffffff
# 
#         exth_thumb = read_exth(datain_rec0, exth_thumb_offset)
#         if len(exth_thumb) > 0:
#             thumb_index, = struct.unpack_from('>L', exth_thumb[0], 0)
#             thumb_index += firstimage
#         else:
#             thumb_index = 0xffffffff
# 
#         if cover_index != 0xffffffff:
#             w, h = 0, 0
#             if thumb_index != 0xffffffff:
#                 image = readsection(datain, thumb_index)
#                 self.thumbnail = Image.open(BytesIO(image))
#                 w, h = self.thumbnail.size
#             if (w < width and h < height) or stretch:
#                 image = readsection(datain, cover_index)
#                 self.thumbnail = Image.open(BytesIO(image))
#                 if stretch:
#                     self.thumbnail = self.thumbnail.resize((width, height), Image.LANCZOS)
#                 else:
#                     self.thumbnail.thumbnail((width, height), Image.LANCZOS)
# 
#         if self.combo:
#             # always try to use information from KF8 part
#             datain_kfrec0 = readsection(datain, datain_kf8)
#             exth = read_exth(datain_kfrec0, exth_asin)
#             if len(exth) > 0:
#                 self.asin = exth[0].decode("ascii")
#             exth = read_exth(datain_kfrec0, exth_cdetype)
#             if len(exth) > 0:
#                 self.cdetype = exth[0].decode("ascii")
#             exth = read_exth(datain_kfrec0, exth_cdecontentkey)
#             if len(exth) > 0:
#                 self.cdecontentkey = exth[0].decode("ascii")
# 
#     def getACR(self):
#         return self.acr
# 
#     def getASIN(self):
#         return self.asin
# 
#     def getCdeType(self):
#         return self.cdetype
# 
#     def getCdeContentKey(self):
#         return self.cdecontentkey
# 
#     def getPageData(self):
#         return self.pagedata
# 
#     def getThumbnail(self):
#         return self.thumbnail


# Path: synccovers.py
import os
import sys
import traceback
import argparse
import version
from modules.mobi_split import mobi_read

            asin = reader.getASIN()
        if asin:
            thumb = reader.getThumbnail()
            if thumb != None:
                thumb.convert('RGB').save(os.path.join(kindle_dir, 'thumbnail_' + asin + '_' + reader.getCdeType() + '_portrait.jpg'), 'JPEG')
                count_processed += 1
                if verbose:
                    print('Written thumbnail for {}'.format(asin))
            else:
                if verbose:
                    print("Skipping - no cover or thumbnail")
        else:
            if verbose:
                print("Skipping - no ASIN")
    except:
        print('ERROR: processing file "{}".'.format(infile))
        # traceback.print_exc()

    return


def process_folder(inputdir, width, height, stretch, verbose):

    if os.path.isdir(inputdir):
        # let's see if we could locate kindle directory
        head = tail = inputdir
        while tail:
            head, tail = os.path.split(head)
            if tail:
                kindle_dir = os.path.join(head, 'system', 'thumbnails')

                if os.path.isdir(kindle_dir):