def play(self, fullpath, startTime=0, chapter=0): log("AudioBooksPlugin: Playing %s" % fullpath) audioBookHandler = AudioBookHandler.createHandler(fullpath) bookPlayer = BookPlayer() bookPlayer.playAudioBook(audioBookHandler, startTime, chapter) del bookPlayer del audioBookHandler # After playing we need to update the screen to reflect our progress xbmc.executebuiltin("Container.Refresh")
def showAudiobooks(self, directory=None): # Get the setting for the audio book directory audioBookFolder = Settings.getAudioBookFolder() if audioBookFolder in [None, ""]: # Prompt the user to set the eBooks Folder audioBookFolder = xbmcgui.Dialog().browseSingle(0, ADDON.getLocalizedString(32005), 'files') # Check to make sure the directory is set now if audioBookFolder in [None, ""]: xbmcgui.Dialog().ok(ADDON.getLocalizedString(32001), ADDON.getLocalizedString(32006)) return # Save the directory in settings for future use log("AudioBooksPlugin: Setting Audio Books folder to %s" % audioBookFolder) Settings.setAudioBookFolder(audioBookFolder) # We may be looking at a subdirectory if directory not in [None, ""]: audioBookFolder = directory dirs, files = xbmcvfs.listdir(audioBookFolder) files.sort() dirs.sort() bookDirs = [] # For each directory list allow the user to navigate into it for adir in dirs: if adir.startswith('.'): continue fullDir = os_path_join(audioBookFolder, adir) # Check if this directory is a book directory if self._isAudioBookDir(fullDir): bookDirs.append(fullDir) continue log("AudioBooksPlugin: Adding directory %s" % adir) try: displayName = "[%s]" % adir.encode("utf-8") except: displayName = "[%s]" % adir try: fullDir = fullDir.encode("utf-8") except: pass url = self._build_url({'mode': 'directory', 'directory': fullDir}) li = xbmcgui.ListItem(displayName, iconImage='DefaultFolder.png') li.setProperty("Fanart_Image", FANART) li.addContextMenuItems([], replaceItems=True) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=True) m4bAudioBooks = [] for m4bBookFile in files: # Check to ensure that this is an eBook if not m4bBookFile.lower().endswith('.m4b'): log("AudioBooksPlugin: Skipping non audiobook file: %s" % m4bBookFile) continue fullpath = os_path_join(audioBookFolder, m4bBookFile) m4bAudioBooks.append(fullpath) # Get all the audiobook in a nicely sorted order allAudioBooks = sorted(bookDirs + m4bAudioBooks) audioBookHandlers = [] # Now list all of the books for audioBookFile in allAudioBooks: log("AudioBooksPlugin: Adding audiobook %s" % audioBookFile) audioBookHandlers.append(AudioBookHandler.createHandler(audioBookFile)) # Now sort the list by title audioBookHandlers.sort() # Now list all of the books for audioBookHandler in audioBookHandlers: log("AudioBooksPlugin: Processing audiobook %s" % audioBookHandler.getFile()) title = audioBookHandler.getTitle() coverTargetName = audioBookHandler.getCoverImage(True) isRead = False if Settings.isMarkCompletedItems(): if audioBookHandler.isCompleted(): isRead = True displayString = title try: displayString = title.encode("utf-8") except: displayString = title try: log("AudioBooksPlugin: Display title is %s for %s" % (displayString, audioBookFile)) except: # No need to have an error for logging pass if isRead: displayString = '* %s' % displayString url = self._build_url({'mode': 'chapters', 'filename': audioBookHandler.getFile(True), 'cover': coverTargetName}) li = xbmcgui.ListItem(displayString, iconImage=coverTargetName) li.setProperty("Fanart_Image", FANART) li.addContextMenuItems(self._getContextMenu(audioBookHandler), replaceItems=True) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=True) del audioBookHandler xbmcplugin.endOfDirectory(self.addon_handle)
def listChapters(self, fullpath, defaultImage): log("AudioBooksPlugin: Listing chapters for %s" % fullpath) audioBookHandler = AudioBookHandler.createHandler(fullpath) chapters = audioBookHandler.getChapterDetails() if len(chapters) < 1: url = self._build_url({'mode': 'play', 'filename': audioBookHandler.getFile(True), 'startTime': 0, 'chapter': 0}) li = xbmcgui.ListItem(ADDON.getLocalizedString(32018), iconImage=defaultImage) li.setProperty("Fanart_Image", FANART) li.addContextMenuItems([], replaceItems=True) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=False) secondsIn, chapterPosition = audioBookHandler.getPosition() if (secondsIn > 0) or (chapterPosition > 1): url = self._build_url({'mode': 'play', 'filename': audioBookHandler.getFile(True), 'startTime': secondsIn, 'chapter': chapterPosition}) displayTime = self._getDisplayTimeFromSeconds(secondsIn) displayName = "%s %s" % (ADDON.getLocalizedString(32019), displayTime) # Add the Chapter being read if there are many chapters if (chapterPosition > 0) and (len(chapters) > 1): displayName = "%s (%s: %d)" % (displayName, ADDON.getLocalizedString(32017), chapterPosition) li = xbmcgui.ListItem(displayName, iconImage=defaultImage) li.setProperty("Fanart_Image", FANART) li.addContextMenuItems([], replaceItems=True) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=False) # Add all the chapters to the display chapterNum = 0 for chapter in chapters: chapterNum += 1 url = self._build_url({'mode': 'play', 'filename': audioBookHandler.getFile(True), 'startTime': audioBookHandler.getChapterStart(chapterNum), 'chapter': chapterNum}) displayString = "" if Settings.isShowPlayButtonIfOneChapter() and (len(chapters) == 1): displayString = ADDON.getLocalizedString(32030) else: try: displayString = chapter['title'].encode("utf-8") except: displayString = chapter['title'] # Check if we need to add a number at the start of the chapter if Settings.autoNumberChapters() and (len(displayString) > 0) and (len(chapters) > 1): # Check to make sure that the display chapter does not already # start with a number, or end with a number if not (displayString[0].isdigit() or displayString[-1].isdigit()): displayString = "%d. %s" % (chapterNum, displayString) # Check if the current position means that this chapter has already been played if Settings.isMarkCompletedItems(): if (audioBookHandler.isCompleted()) or ((chapter['endTime'] < secondsIn) and (chapter['endTime'] > 0)) or (chapterNum < chapterPosition): displayString = '* %s' % displayString li = xbmcgui.ListItem(displayString, iconImage=defaultImage) if len(chapters) > 1: durationEntry = chapter['startTime'] # If the duration is set as zero, nothing is displayed if (durationEntry < 1) and (chapter['endTime'] > 0): durationEntry = 1 # Use the start time for the duration display as that will show # how far through the book the chapter is li.setInfo('music', {'Duration': durationEntry}) li.setProperty("Fanart_Image", FANART) li.addContextMenuItems([], replaceItems=True) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=False) del audioBookHandler xbmcplugin.endOfDirectory(self.addon_handle)
def showAudiobooks(self, directory=None): # Get the setting for the audio book directory audioBookFolder = Settings.getAudioBookFolder() if audioBookFolder in [None, ""]: # Prompt the user to set the eBooks Folder audioBookFolder = xbmcgui.Dialog().browseSingle( 0, ADDON.getLocalizedString(32005), 'files') # Check to make sure the directory is set now if audioBookFolder in [None, ""]: xbmcgui.Dialog().ok(ADDON.getLocalizedString(32001), ADDON.getLocalizedString(32006)) return # Save the directory in settings for future use log("AudioBooksPlugin: Setting Audio Books folder to %s" % audioBookFolder) Settings.setAudioBookFolder(audioBookFolder) # We may be looking at a subdirectory if directory not in [None, ""]: audioBookFolder = directory dirs, files = xbmcvfs.listdir(audioBookFolder) files.sort() dirs.sort() bookDirs = [] # For each directory list allow the user to navigate into it for adir in dirs: if adir.startswith('.'): continue fullDir = os_path_join(audioBookFolder, adir) # Check if this directory is a book directory if self._isAudioBookDir(fullDir): bookDirs.append(fullDir) continue log("AudioBooksPlugin: Adding directory %s" % adir) try: displayName = "[%s]" % adir.encode("utf-8") except: displayName = "[%s]" % adir try: fullDir = fullDir.encode("utf-8") except: pass plot = "" try: plot = "[B]%s[/B]" % adir.encode("utf-8") except: plot = adir # Check if there are any images for this directory iconImage = 'DefaultFolder.png' fanartImage = FANART subDirs, subFiles = xbmcvfs.listdir(fullDir) for fileInDir in subFiles: if fileInDir.lower() in ['fanart.jpg', 'fanart.png']: fanartImage = os_path_join(fullDir, fileInDir) elif fileInDir.lower() in ['folder.jpg', 'folder.png']: iconImage = os_path_join(fullDir, fileInDir) url = self._build_url({'mode': 'directory', 'directory': fullDir}) li = xbmcgui.ListItem(displayName, iconImage=iconImage) li.setProperty("Fanart_Image", fanartImage) li.setInfo('video', {'Plot': plot}) li.addContextMenuItems([], replaceItems=True) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=True) m4bAudioBooks = [] for m4bBookFile in files: # Check to ensure that this is an eBook if not m4bBookFile.lower().endswith('.m4b'): log("AudioBooksPlugin: Skipping non audiobook file: %s" % m4bBookFile) continue fullpath = os_path_join(audioBookFolder, m4bBookFile) m4bAudioBooks.append(fullpath) # Get all the audiobook in a nicely sorted order allAudioBooks = sorted(bookDirs + m4bAudioBooks) audioBookHandlers = [] # Now list all of the books for audioBookFile in allAudioBooks: log("AudioBooksPlugin: Adding audiobook %s" % audioBookFile) audioBookHandlers.append( AudioBookHandler.createHandler(audioBookFile)) # Now sort the list by title audioBookHandlers.sort() # Now list all of the books for audioBookHandler in audioBookHandlers: log("AudioBooksPlugin: Processing audiobook %s" % audioBookHandler.getFile()) title = audioBookHandler.getTitle() coverTargetName = audioBookHandler.getCoverImage(True) isRead = False if Settings.isMarkCompletedItems(): if audioBookHandler.isCompleted(): isRead = True displayString = title try: displayString = title.encode("utf-8") except: displayString = title try: log("AudioBooksPlugin: Display title is %s for %s" % (displayString, audioBookFile)) except: # No need to have an error for logging pass plot = "" try: plot = "[B]%s[/B]" % displayString except: plot = displayString if isRead: displayString = '* %s' % displayString url = self._build_url({ 'mode': 'chapters', 'filename': audioBookHandler.getFile(True), 'cover': coverTargetName }) li = xbmcgui.ListItem(displayString, iconImage=coverTargetName) li.setProperty("Fanart_Image", audioBookHandler.getFanArt()) li.setInfo('video', {'Plot': plot}) li.addContextMenuItems(self._getContextMenu(audioBookHandler), replaceItems=True) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=True) del audioBookHandler xbmcplugin.endOfDirectory(self.addon_handle)
def listChapters(self, fullpath, defaultImage): log("AudioBooksPlugin: Listing chapters for %s" % fullpath) audioBookHandler = AudioBookHandler.createHandler(fullpath) plot = "" try: plot = "[B]%s[/B]" % audioBookHandler.getTitle() except: plot = audioBookHandler.getTitle() chapters = audioBookHandler.getChapterDetails() if len(chapters) < 1: url = self._build_url({ 'mode': 'play', 'filename': audioBookHandler.getFile(True), 'startTime': 0, 'chapter': 0 }) li = xbmcgui.ListItem(ADDON.getLocalizedString(32018), iconImage=defaultImage) li.setProperty("Fanart_Image", audioBookHandler.getFanArt()) li.addContextMenuItems([], replaceItems=True) li.setInfo('video', {'Plot': plot}) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=False) secondsIn, chapterPosition = audioBookHandler.getPosition() if (secondsIn > 0) or (chapterPosition > 1): url = self._build_url({ 'mode': 'play', 'filename': audioBookHandler.getFile(True), 'startTime': secondsIn, 'chapter': chapterPosition }) displayTime = self._getDisplayTimeFromSeconds(secondsIn) displayName = "%s %s" % (ADDON.getLocalizedString(32019), displayTime) # Add the Chapter being read if there are many chapters if (chapterPosition > 0) and (len(chapters) > 1): displayName = "%s (%s: %d)" % (displayName, ADDON.getLocalizedString(32017), chapterPosition) li = xbmcgui.ListItem(displayName, iconImage=defaultImage) li.setProperty("Fanart_Image", audioBookHandler.getFanArt()) li.setInfo('video', {'Plot': plot}) li.addContextMenuItems([], replaceItems=True) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=False) # Add all the chapters to the display chapterNum = 0 for chapter in chapters: chapterNum += 1 url = self._build_url({ 'mode': 'play', 'filename': audioBookHandler.getFile(True), 'startTime': audioBookHandler.getChapterStart(chapterNum), 'chapter': chapterNum }) displayString = "" if Settings.isShowPlayButtonIfOneChapter() and (len(chapters) == 1): displayString = ADDON.getLocalizedString(32030) else: try: displayString = chapter['title'].encode("utf-8") except: displayString = chapter['title'] # Check if we need to add a number at the start of the chapter if Settings.autoNumberChapters() and (len(displayString) > 0) and (len(chapters) > 1): # Check to make sure that the display chapter does not already # start with a number, or end with a number if not (displayString[0].isdigit() or displayString[-1].isdigit()): displayString = "%d. %s" % (chapterNum, displayString) # Check if the current position means that this chapter has already been played if Settings.isMarkCompletedItems(): if (audioBookHandler.isCompleted()) or ( (chapter['endTime'] < secondsIn) and (chapter['endTime'] > 0)) or (chapterNum < chapterPosition): displayString = '* %s' % displayString li = xbmcgui.ListItem(displayString, iconImage=defaultImage) if len(chapters) > 1: durationEntry = chapter['startTime'] # If the duration is set as zero, nothing is displayed if (durationEntry < 1) and (chapter['endTime'] > 0): durationEntry = 1 # Use the start time for the duration display as that will show # how far through the book the chapter is li.setInfo('music', {'Duration': durationEntry}) li.setProperty("Fanart_Image", audioBookHandler.getFanArt()) li.setInfo('video', {'Plot': plot}) li.addContextMenuItems([], replaceItems=True) xbmcplugin.addDirectoryItem(handle=self.addon_handle, url=url, listitem=li, isFolder=False) del audioBookHandler xbmcplugin.endOfDirectory(self.addon_handle)