6 import variables as var
9 FORMAT OF A MUSIC INTO THE PLAYLIST
19 ready (validation, no, downloading, yes, failed)
20 from_playlist (yes,no)
44 version = 0 # increase by one after each change
45 mode = "one-shot" # "repeat", "random"
48 def __init__(self, *args):
49 super().__init__(*args)
52 return True if len(self) == 0 else False
54 def set_mode(self, mode):
55 # modes are "one-shot", "repeat", "random"
61 elif mode == "one-shot" and self.current_index > 0:
62 # remove items before current item
64 for i in range(self.current_index):
65 super().__delitem__(0)
66 self.current_index = 0
68 def append(self, item):
70 item = util.attach_music_tag_info(item)
75 def insert(self, index, item):
79 index = self.current_index
81 item = util.attach_music_tag_info(item)
82 super().insert(index, item)
84 if index <= self.current_index:
85 self.current_index += 1
92 def extend(self, items):
95 lambda item: util.attach_music_tag_info(item),
105 #logging.debug("playlist: Next into the queue")
107 if self.current_index < len(self) - 1:
108 if self.mode == "one-shot" and self.current_index != -1:
109 super().__delitem__(self.current_index)
111 self.current_index += 1
113 return self[self.current_index]
115 self.current_index = 0
116 if self.mode == "one-shot":
119 elif self.mode == "repeat":
121 elif self.mode == "random":
125 raise TypeError("Unknown playlist mode '%s'." % self.mode)
128 for index, item in enumerate(self):
133 def update(self, item, id):
135 index = self.find(id)
141 def __delitem__(self, key):
142 return self.remove(key)
144 def remove(self, index=-1):
146 if index > len(self) - 1:
150 index = self.current_index
152 removed = self[index]
153 super().__delitem__(index)
155 if self.current_index > index:
156 self.current_index -= 1
160 def current_item(self):
164 return self[self.current_index]
166 def current_item_downloading(self):
170 if self[self.current_index]['type'] == 'url' and self[self.current_index]['ready'] == 'downloading':
174 def next_index(self):
175 if len(self) == 0 or (len(self) == 1 and self.mode == 'one_shot'):
178 if self.current_index < len(self) - 1:
179 return self.current_index + 1
184 if len(self) == 0 or (len(self) == 1 and self.mode == 'one_shot'):
187 return self[self.next_index()]
189 def jump(self, index):
190 if self.mode == "one-shot":
191 for i in range(index):
192 super().__delitem__(0)
193 self.current_index = 0
195 self.current_index = index
198 return self[self.current_index]
201 # current_index will lose track after shuffling, thus we take current music out before shuffling
202 #current = self.current_item()
203 #del self[self.current_index]
207 #self.insert(0, current)
208 self.current_index = -1
213 self.current_index = -1
217 var.db.remove_section("playlist_item")
218 var.db.set("playlist", "current_index", self.current_index)
220 for index, music in enumerate(self):
221 if music['type'] == 'url' and music['ready'] == 'downloading':
222 music['ready'] = 'no'
224 var.db.set("playlist_item", str(index), json.dumps(music))
227 current_index = var.db.getint("playlist", "current_index", fallback=-1)
228 if current_index == -1:
231 items = list(var.db.items("playlist_item"))
232 items.sort(key=lambda v: int(v[0]))
233 self.extend(list(map(lambda v: json.loads(v[1]), items)))
235 self.current_index = current_index
237 def _debug_print(self):
238 print("===== Playlist(%d) ====" % self.current_index)
239 for index, item in enumerate(self):
240 if index == self.current_index:
241 print("-> %d %s" % (index, item['title']))
243 print("%d %s" % (index, item['title']))
244 print("===== End ====")