1 from __future__ import with_statement
8 _moduleLogger = logging.getLogger(__name__)
11 class Audiobook(object):
13 def __init__(self, path, current_chapter = 0):
16 self._chapterPaths = []
17 self.current_chapter = current_chapter
19 if is_playlist_book(path):
20 self._scan_index(path)
21 elif is_dir_book(path):
23 elif is_single_chapter(path):
24 self._scan_chapter(path)
26 _moduleLogger.info("Audiobook not found in path: " + path)
27 raise IOError("Audiobook directory not found")
29 if len(self._chapterPaths) <= self.current_chapter:
30 _moduleLogger.warning(
31 "Audiobook chapter out of range (%s/%s)" % (
32 self.current_chapter, len(self._chapterPaths)
35 self.current_chapter = 0
39 return self._chapterPaths
41 def get_current_chapter(self):
42 return self._chapterPaths[self.current_chapter]
44 def set_chapter(self, chapter):
45 if chapter in self.chapters:
46 self.current_chapter = self.chapters.index(chapter)
48 raise Exception("Unknown chapter set")
50 def get_previous_chapter(self):
52 @returns the file name for the next chapter, without path
54 if 0 == self.current_chapter:
57 self.current_chapter -= 1
58 return self._chapterPaths[self.current_chapter]
60 def get_next_chapter(self):
62 @returns the file name for the next chapter, without path
64 if len(self._chapterPaths) == self.current_chapter:
67 self.current_chapter += 1
68 return self._chapterPaths[self.current_chapter]
70 def get_cover_img(self):
72 return self._coverPath
74 return "%s/NoCover.png" % os.path.dirname(__file__)
76 def _scan_dir(self, root):
77 self.title = os.path.split(root)[-1]
80 for f in os.listdir(root)
81 if not f.startswith(".")
86 for path in dirContent
87 if os.path.isfile(os.path.join(root, path))
93 if path.rsplit(".", 1)[-1] in ["png", "gif", "jpg", "jpeg"]
96 self._coverPath = images[0]
98 self._chapterPaths = [
101 if is_single_chapter(path)
103 self._chapterPaths.sort()
105 def _scan_chapter(self, file):
106 self._chapterPaths = [file]
108 def _scan_playlist(self, file):
109 root = os.path.dirname(file)
110 self.title = os.path.basename(file).rsplit(".")[0]
112 with open(file, 'r') as f:
114 if line.startswith("#"):
117 if not os.path.isabs(path):
118 path = os.path.normpath(os.path.join(root, path))
119 self._chapterPaths.append(path)
120 # Not sorting, assuming the file is in the desired order
122 def _scan_index(self, file):
126 looking_for_title = False
127 looking_for_cover = False
128 looking_for_chapters = False
130 with open(file, 'r') as f:
133 ascii = unicodedata.normalize('NFKD', unicode(line, "latin-1")).encode('ascii', 'ignore')
134 print line[:-1], "PIC\n" in line, line in "#PIC"
136 looking_for_title = True
138 if looking_for_title:
139 self.title = line[:-1]
140 looking_for_title = False
142 looking_for_cover = True
144 if looking_for_cover:
145 self.cover = line[:-1]
146 looking_for_cover = False
147 if "#TRACKS" in line:
148 looking_for_chapters = True
150 if looking_for_chapters:
151 if "#CHAPTERS" in line:
152 break # no further information needed
153 self.chapters.append(line.split(':')[0])
156 def is_dir_book(path):
157 return os.path.isdir(path)
160 def is_playlist_book(path):
161 return path.rsplit(".", 1)[-1] in ["m3u"]
164 def is_single_chapter(path):
165 return path.rsplit(".", 1)[-1] in ["awb", "mp3", "spx", "ogg", "ac3", "wav"]
169 if is_dir_book(path):
171 elif is_playlist_book(path):
173 elif is_single_chapter(path):