def get_current_song(self, bot, source, message, **rest): with DBManager.create_session_scope() as db_session: current_song = SongrequestQueue._get_current_song(db_session) if current_song: m, s = divmod(current_song.playing_in(db_session), 60) m = int(m) s = int(s) time_left = f"{m:02d}:{s:02d}" if current_song.requested_by: bot.say( self.settings["message_in_chat_when_song_is_playing"]. format( title=current_song.song_info.title, requestor=current_song.requested_by.username_raw, time_left=time_left, )) return True bot.say(self.settings["message_in_chat_when_song_is_playing"]. format(title=current_song.song_info.title, requestor="Backup Playlist", time_left=time_left)) return True if self.settings["use_spotify"]: is_playing, title, artistsArr = bot.spotify_api.state( bot.spotify_token_manager) if is_playing: bot.say(self.settings[ "message_in_chat_when_song_is_playing_spotify"].format( title=title, artists=", ".join( [str(artist) for artist in artistsArr]))) return True bot.say(self.settings["message_in_chat_no_songs_playing"]) return True
def create_song_request_queue(self, video_id, bot, source): with DBManager.create_session_scope() as db_session: song_info = SongRequestSongInfo._create_or_get( db_session, video_id, self.youtube) if not song_info: log.error("There was an error!") return False if song_info.banned: bot.whisper(source, "That song is banned! FeelsWeirdMan") return False skip_after = (self.settings["max_song_length"] if song_info.duration > self.settings["max_song_length"] else None) songrequest_queue = SongrequestQueue._create( db_session, video_id, skip_after, source.id) db_session.commit() m, s = divmod(int(songrequest_queue.playing_in(db_session)), 60) m = int(m) s = int(s) playing_in = f"{m:02d}:{s:02d}" if self.settings["send_message_in_chat"]: bot.say(self.settings["message_in_chat"].format( username=source.username_raw, title=song_info.title, current_pos=songrequest_queue.queue + (1 if SongrequestQueue._get_current_song(db_session) else 0), playing_in=playing_in, )) self.bot.songrequest_manager._playlist() return True
def songrequest(): with DBManager.create_session_scope() as db_session: playing_in = 0 track_number = 1 songs_queue = [] SongRequestQueueManager.force_reload() queue_ids = SongRequestQueueManager.get_next_songs(50) current_song = SongrequestQueue._get_current_song(db_session) queue = ([current_song] if current_song else []) + SongrequestQueue.sort( queue_ids, SongrequestQueue._from_list_id(db_session, queue_ids)) for song in queue: if song.song_info is None: continue jsonify = song.webjsonify() m, s = divmod(playing_in, 60) m = int(m) s = int(s) jsonify["playing_in"] = (f"{m:02d}:{s:02d}" if playing_in != 0 else ("Currently playing" if current_song else "Song Request Closed")) jsonify["track_number"] = track_number playing_in += song.time_left track_number += 1 songs_queue.append(jsonify) history = (db_session.query(SongrequestHistory).filter( SongrequestHistory.song_info.has(banned=False)).order_by( SongrequestHistory.id.desc()).limit(50).all()) track_number = 1 songs_history = [] for song in history: if song.song_info.banned: continue jsonify = song.webjsonify() jsonify["track_number"] = track_number track_number += 1 songs_history.append(jsonify) return render_template("songrequest.html", songs_queue=songs_queue, songs_history=songs_history, live=StreamManager.online)
def load_song(self, skipped_by_id=None): if not self.module_state["enabled"]: return False if self.current_song_id: with DBManager.create_session_scope() as db_session: current_song = SongrequestQueue._from_id( db_session, self.current_song_id) if current_song: if current_song.current_song_time > 5: self.previous_queue = 0 histroy = current_song._to_histroy( db_session, skipped_by_id) if not histroy: log.info( "History not added because stream is offline!") else: current_song._remove(db_session) self._stop_video() self._hide() db_session.commit() self._playlist_history() self.current_song_id = None self.schedule_job_id = None self.module_state["paused"] = False self._module_state() self.remove_schedule() if not self.module_state["requests_open"]: if self.previously_playing_spotify: self.bot.spotify_api.play(self.bot.spotify_token_manager) log.info("Resumed Spotify") self.previously_playing_spotify = False return False with DBManager.create_session_scope() as db_session: current_song = SongrequestQueue._get_current_song(db_session) if not current_song: current_song = SongrequestQueue._pop_next_song(db_session) if current_song: SongRequestQueueManager.update_song_playing_id(current_song.id) current_song.played_for = 0 current_song.date_resumed = utils.now() self.current_song_id = current_song.id self._volume() self._play(current_song.video_id, current_song.webjsonify()) self.schedule_job_id = random.randint(1, 100000) self.current_song_schedule = ScheduleManager.execute_delayed( current_song.time_left + 10, self.load_song_schedule, args=[self.schedule_job_id]) if self.settings["use_spotify"]: is_playing, song_name, artistsArr = self.bot.spotify_api.state( self.bot.spotify_token_manager) if is_playing: self.bot.spotify_api.pause( self.bot.spotify_token_manager) self.previously_playing_spotify = True if not current_song.requested_by_id: SongrequestQueue._create(db_session, current_song.video_id, current_song.skip_after, None, backup=True) db_session.commit() if current_song.requested_by_id: self._playlist() else: self._backup_playlist() return True if not current_song: SongRequestQueueManager.update_song_playing_id("") if self.settings["use_spotify"]: if self.previously_playing_spotify: self.bot.spotify_api.play(self.bot.spotify_token_manager) log.info("Resumed Spotify") self.previously_playing_spotify = False if self.is_video_showing: self._hide() return False
def load_song(self, skipped_by_id=None): if not self.enabled: return False if self.current_song_id: with DBManager.create_session_scope() as db_session: current_song = SongrequestQueue._from_id( db_session, self.current_song_id) if current_song: if current_song.current_song_time > 5: self.previous_queue = 0 histroy = current_song._to_histroy( db_session, skipped_by_id) if not histroy: log.info( "History not added because stream is offline!") else: current_song._remove(db_session) self._stop_video() self._hide() db_session.commit() self._playlist_history() SongrequestQueue._update_queue() self.current_song_id = None if not self.module_opened: return False with DBManager.create_session_scope() as db_session: current_song = SongrequestQueue._get_current_song(db_session) if not current_song: current_song = SongrequestQueue._get_next_song(db_session) if current_song: current_song.playing = True current_song.queue = 0 current_song.current_song_time = 0 self.current_song_id = current_song.id song_info = current_song.song_info self._play( current_song.video_id, song_info.title, current_song.requested_by.username_raw if current_song.requested_by else "Backup list", ) if self.settings["use_spotify"]: is_playing, song_name, artistsArr = self.bot.spotify_api.state( self.bot.spotify_token_manager) if is_playing: self.bot.spotify_api.pause( self.bot.spotify_token_manager) self.previously_playing_spotify = True if not current_song.requested_by_id: SongrequestQueue._create( db_session, current_song.video_id, current_song.skip_after, None, SongrequestQueue._get_next_queue(db_session), ) db_session.commit() self._playlist() SongrequestQueue._update_queue() return True if self.settings["use_spotify"]: if self.previously_playing_spotify: self.bot.spotify_api.play(self.bot.spotify_token_manager) self.previously_playing_spotify = False if self.isVideoShowing: self._hide() return False