def __clean_playlist(self): status = MPC().status() plinfo = MPC().playlistinfo() playtime = int(MPC().stats()['playtime']) try: old_playtime = StateVar.objects.get(key='playtime') except StateVar.DoesNotExist: # fresh db? old_playtime = StateVar(key='playtime', val=str(playtime)) old_playtime.save() if status['playlistlength'] == '0': # no queue has been synced to mpd yet return None elif not status.has_key('songid'): # either play has not started, or the play queue was finished # if play hasn't started, we'll return the first song if playtime <= int(old_playtime.val): return plinfo[0]['id'] # otherwise, we'll nuke the mpd queue and return None else: status['songid'] = None # delete everything up to the current song for song in plinfo: songid = song['id'] if songid == status['songid']: break MPC().deleteid(songid) Track.objects.filter(playlist_id = songid).delete() old_playtime.val = str(playtime) old_playtime.save() return status['songid']
def ajax_createblock(request): songfiles = json.loads(request.raw_post_data) b = Block(length=0, author=request.user.username) b.save() i = 0 if len(songfiles) == 0: return HttpResponse("No Songs. Wtf mate?") for s in songfiles: dat = MPC().search('file', str(s))[0] if 'album' not in dat: dat['album'] = '' if 'artist' not in dat: dat['artist'] = '' if 'title' not in dat: dat['title'] = '' Track(block=b, filename=s, track_number=i, artist=dat['artist'], album=dat['album'], title=dat['title']).save() i = i + 1 b.update_length() Vote(block=b, user=request.user.username).save() Queue().save_queue() if MPC().status()['state'] == 'stop': MPC().play() return HttpResponse("OK")
def save_queue(self): queue = self.compute_queue() for i in range(len(queue)): if queue[i].playlist_id is not None: MPC().moveid(queue[i].playlist_id, i) else: queue[i].playlist_id = MPC().addid(queue[i].filename, i) queue[i].save() return queue
def ajax_mpd_status(request): # first set some sane defaults c = {} c['elapsed'] = 0 c['cursong'] = {'time': 1} # give them real values c.update(MPC().status()) c['cursong'].update(MPC().currentsong()) return HttpResponse(json.dumps(c))
def update_length(self): tracks = self.track_set.all() time = 0 for t in tracks: time += int(MPC().find('file', t.filename)[0]['time']) self.length = time self.save()
def unfuckdb(request): Vote.objects.all().delete() Block.objects.all().delete() Track.objects.all().delete() StateVar.objects.all().delete() MPC().clear() return index(request)
def __clean_playlist(self): status = MPC().status() plinfo = MPC().playlistinfo() playtime = int(MPC().stats()['playtime']) try: old_playtime = StateVar.objects.get(key='playtime') except StateVar.DoesNotExist: # fresh db? old_playtime = StateVar(key='playtime', val=str(playtime)) old_playtime.save() if status['playlistlength'] == '0': # no queue has been synced to mpd yet return None elif not status.has_key('songid'): # either play has not started, or the play queue was finished # if play hasn't started, we'll return the first song if playtime <= int(old_playtime.val): return plinfo[0]['id'] # otherwise, we'll nuke the mpd queue and return None else: status['songid'] = None # delete everything up to the current song for song in plinfo: songid = song['id'] if songid == status['songid']: break MPC().deleteid(songid) Track.objects.filter(playlist_id = songid).delete() old_playtime.val = str(playtime) old_playtime.save() return status['songid']
def compute_queue(self, update=False): # 1. determine current block # 2. calculate scores for all other blocks # 3. output tracks in order: current block, then remaining blocks by descending score play_queue = [] blocks_to_score = [] if update: cursong_id = self.__clean_playlist() else: status = MPC().status() if status.has_key('songid'): cursong_id = status['songid'] else: cursong_id = None if cursong_id == None: # play queue has never been saved to mpd # calculate scores for all blocks blocks_to_score = list(Block.objects.all()) else: # add tracks in the current block to the queue curblock = Track.objects.filter(playlist_id = cursong_id)[0].block curblock_tracks = curblock.track_set.all().order_by('track_number') play_queue += list(curblock.track_set.all().order_by('track_number')) # and score the rest... blocks_to_score = list(Block.objects.exclude(id = curblock.id)) # sort them by score d = datetime.now() blocks_to_score.sort(key=lambda x : Block.priority_score(x, d)) # add remaining tracks to the queue for b in blocks_to_score: play_queue += list(b.track_set.all().order_by('track_number')) return play_queue
def ajax_random(request): count = 20 if 'count' in request.POST: count = int(request.POST['count']) c = MPC().random_songs(count) return HttpResponse(json.dumps(c))
def ajax_search(request): if not request.POST['field'] in ['artist', 'title', 'album', 'any']: raise HttpResponseBadRequest() c = MPC().search(request.POST['field'], str(request.POST['value'])) return HttpResponse(json.dumps(c))
def next(request): MPC().next() return index(request)
def updatedb(request): MPC().update() return index(request)
def playpause(request): if MPC().status()['state'] == 'stop': MPC().play() else: MPC().pause() return index(request)
def setvolume(request, level): MPC().setvol(level) return index(request)