def extractCoverImage(self): log("MobiEBook: Extracting cover for %s" % self.filePath) # Get the location that the book is to be extracted to extractDir = os_path_join(Settings.getTempLocation(), 'mobi_extracted') # Check if the mobi extract directory already exists if dir_exists(extractDir): try: shutil.rmtree(extractDir, True) except: log("MobiEBook: Failed to delete directory %s" % extractDir) # Extract the contents of the book so we can get the cover image try: kindleunpack.unpackBook(self.filePath, extractDir, None, '2', True) except: log("MobiEBook: Failed to extract cover for %s with error: %s" % (self.filePath, traceback.format_exc()), xbmc.LOGERROR) coverTargetName = None if dir_exists(extractDir): coverImages = self._findCoverImage(extractDir) if len(coverImages) > 0: coverImageSrc = coverImages[0] log("MobiEBook: Found cover file %s" % coverImageSrc) coverFileName, oldExt = os.path.splitext(self.fileName) cacheCoverName = "%s.jpg" % coverFileName coverTargetName = os_path_join(Settings.getCoverCacheLocation(), cacheCoverName) # Now move the file to the covers cache directory copy = xbmcvfs.copy(coverImageSrc, coverTargetName) if copy: log("MobiEBook: copy successful for %s" % coverTargetName) else: log("MobiEBook: copy failed from %s to %s" % (coverImageSrc, coverTargetName)) else: log("MobiEBook: No cover image found for %s" % self.filePath) # Now tidy up the extracted data try: shutil.rmtree(extractDir, True) except: log("MobiEBook: Failed to tidy up directory %s" % extractDir) else: log("MobiEBook: Failed to extract Mobi file %s" % self.filePath) return coverTargetName
def getChapterContents(self, chapterLink): log("MobiEBook: Getting chapter contents for %s" % chapterLink) # Find out the name of the page that this chapter is stored in sections = chapterLink.split('#') bookFileName = None chapterStartFlag = None if len(sections) > 0: bookFileName = sections[0] if len(sections) > 1: chapterStartFlag = sections[1] # Get the content of the chapter, this will be in HTML chapterContent = "" # Get the location that the book is to be extracted to extractDir = os_path_join(Settings.getTempLocation(), 'mobi_extracted') # Check if the mobi extract directory already exists if dir_exists(extractDir): try: shutil.rmtree(extractDir, True) except: log("MobiEBook: Failed to delete directory %s" % extractDir) # Extract the contents of the book so we can get the chapter contents try: kindleunpack.unpackBook(self.filePath, extractDir, None, '2', True) except: log("MobiEBook: Failed to unpack book for %s with error: %s" % (self.filePath, traceback.format_exc()), xbmc.LOGERROR) # Find the file containing the book contents bookFileLocation = self._findBookFile(extractDir, bookFileName) bookContents = "" if bookFileLocation not in [None, ""]: # Read the contents of the file try: # Read the contents of the book file into a string bookFile = xbmcvfs.File(bookFileLocation, 'r') bookContents = bookFile.read() bookFile.close() except: log("MobiEBook: Failed to read contents of book %s with error: %s" % (bookFileName, traceback.format_exc()), xbmc.LOGERROR) else: log("MobiEBook: Failed to find book content file %s" % bookFileName) # Cleanup the extract directory if dir_exists(extractDir): try: shutil.rmtree(extractDir, True) except: log("MobiEBook: Failed to delete directory %s" % extractDir) chapterContent = "" if bookContents not in [None, ""]: if chapterStartFlag is not None: # Split out the chapter (For now just add the whole book) # Split based on page markers pageBreaks = bookContents.split('<mbp:pagebreak/>') anchorHtml = "<a id=\"%s\"" % chapterStartFlag # Find which section contains this anchor for page in pageBreaks: if anchorHtml in page.decode("utf-8"): log("MobiEBook: Found page for chapter marker %s" % chapterStartFlag) chapterContent = self._mobiHtmlParsing(page) break else: log("MobiEBook: Chapter start flag, showing whole book") chapterContent = self._mobiHtmlParsing(bookContents) if chapterContent not in [None, ""]: chapterContent = self.convertHtmlIntoKodiText(chapterContent) return chapterContent
def getChapterDetails(self): log("MobiEBook: Extracting chapter list for %s" % self.filePath) # Get the location that the book is to be extracted to extractDir = os_path_join(Settings.getTempLocation(), 'mobi_extracted') # Check if the mobi extract directory already exists if dir_exists(extractDir): try: shutil.rmtree(extractDir, True) except: log("MobiEBook: Failed to delete directory %s" % extractDir) # Extract the contents of the book so we can get the cover image try: kindleunpack.unpackBook(self.filePath, extractDir, None, '2', True) except: log("MobiEBook: Failed to unpack book for %s with error: %s" % (self.filePath, traceback.format_exc()), xbmc.LOGERROR) chapterDetails = [] if dir_exists(extractDir): tocNcx = self._findTocNcx(extractDir) if tocNcx not in [None, ""]: log("MobiEBook: TOC file found: %s" % tocNcx) # Now we have the TOC file, we need to parse it, we already have # a tool for that, as it is the ePub format try: # Read the contents of the TOC file into a string tocFile = xbmcvfs.File(tocNcx, 'r') tocStr = tocFile.read() tocFile.close() # Now load it into the parser toc = epub.ncx.parse_toc(tocStr) # Get all the chapters for navPoint in toc.nav_map.nav_point: # Get each of the chapter labels for aLabelGroup in navPoint.labels: if aLabelGroup not in [None, ""]: for aLabel in aLabelGroup: if aLabel not in [None, ""]: log("MobiEBook: Adding chapter %s with src %s" % (aLabel, navPoint.src)) detail = {'title': aLabel.encode("utf-8"), 'link': navPoint.src} chapterDetails.append(detail) # Only need the first string for this label group break del toc except: log("MobiEBook: Failed to process TOC %s with error: %s" % (tocNcx, traceback.format_exc()), xbmc.LOGERROR) else: log("MobiEBook: Failed to find TOC file") # Check if we have any chapters, if there are none, then we should show the whole book if (len(chapterDetails) < 1) or (not Settings.onlyShowWholeBookIfChapters()): htmlFiles = self._findHtmlFiles(extractDir) # Check if there are any html files if len(htmlFiles) > 0: keyHtmlFile = None for htmlFile in htmlFiles: if htmlFile.endswith('book.html'): keyHtmlFile break if keyHtmlFile is None: keyHtmlFile = htmlFiles[0] detail = {'title': ADDON.getLocalizedString(32016), 'link': keyHtmlFile} chapterDetails.insert(0, detail) # Now tidy up the extracted data try: shutil.rmtree(extractDir, True) except: log("MobiEBook: Failed to tidy up directory %s" % extractDir) else: log("MobiEBook: Failed to extract Mobi file %s" % self.filePath) return chapterDetails