def refresh_track(): """ For now the interface isn't refreshed :return: """ try: url = request.form["url"] except KeyError: return "nok" with app.database_lock: Track.refresh_by_url(app.config["DATABASE_PATH"], url) return "ok"
def create_html_tracks(database, date=0, nbr=10): items = [] trackcounts = Track.getTrackCounts(database, nbr, date=date) for couple in trackcounts: # we get user, count track = couple[0] count = couple[1] items.append(StatsTracksItem(name=track, count=count)) return StatsTracksTable(items).__html__()
def add(): """ Adds a song to the playlist. Song information are stored in request.form.to_dict(). This dict generally comes from the search. """ track_dict = request.form.to_dict() app.logger.info("Adding track %s", track_dict["url"]) # track["user"] = session["user"] with app.database_lock: if not Track.does_track_exist(app.config["DATABASE_PATH"], track_dict["url"]): Track.insert_track(app.config["DATABASE_PATH"], track_dict) track = Track.import_from_url(app.config["DATABASE_PATH"], track_dict["url"]) track.insert_track_log(app.config["DATABASE_PATH"], session['user']) else: track = Track.import_from_url(app.config["DATABASE_PATH"], track_dict["url"]) track.insert_track_log(app.config["DATABASE_PATH"], session['user']) # we refresh the track in database track = Track.refresh_by_url(app.config["DATABASE_PATH"], track_dict["url"], obsolete=0) track.user = session['user'] app.logger.info(track) with app.playlist_lock: app.playlist.append(track.serialize()) if len(app.playlist) == 1: threading.Thread(target=app.player_worker).start() return "ok"
def player_worker(self): """Function called in a separate thread managing the mpv player. """ while len(self.playlist) > 0: url = self.playlist[0]["url"] self.currently_played = url with app.mpv_lock: if hasattr(self, 'mpv') and self.mpv: del self.mpv self.mpv = MyMPV(None) # we start the track start = time.time() end = start with self.database_lock: track = Track.import_from_url(app.config["DATABASE_PATH"], url) max_count = 5 min_duration = 2 counter = 0 # this while is a little hack, as sometimes, mpv fails mysteriously but work fine on a second or third track # so we check that enough time has passed between play start and end while counter < max_count and track.duration is not None and end - start < min( track.duration, min_duration): # 1 is not enough # note for the future : what if track is passed with a timestamp ? It could be nice to allow it. start = time.time() with app.mpv_lock: self.mpv.play(self.currently_played) # the next instruction should be the only one without a lock # but it causes a segfault when there is a lock # I fear fixing it may be dirty # we could switch to mpv playlists though self.mpv.wait_for_playback( ) # it's stuck here while it's playing end = time.time() counter += 1 """ if counter == max_count and end - start < min(track.duration, min_duration) and track.source == "youtube": # we mark the track as obsolete track.set_obsolete_value(app.config["DATABASE_PATH"], 1) app.logger.info("Marking track {} as obsolete".format(track.url)) """ with self.mpv_lock: del self.mpv with self.playlist_lock: if len(self.playlist) > 0: # and url == self.currently_played: del self.playlist[0]
def suggest(): n = 5 # number of songs to display in the suggestions if "n" in request.args: n = int(request.args.get("n")) result = [] nbr = 0 while nbr < n: # we use a while to be able not to add a song # if it is blacklisted with app.database_lock: track = Track.get_random_track(app.config["DATABASE_PATH"]) if track is None: nbr += 1 elif track.blacklisted == 0 and track.obsolete == 0 and track.source in app.config[ "SEARCH_BACKENDS"]: result.append(track.serialize()) nbr += 1 return jsonify(result)