コード例 #1
0
 def request_function(self, video_id, requested_by, queue=None):
     if not self.module_state["enabled"]:
         return False
     with DBManager.create_session_scope() as db_session:
         requested_by = User.find_by_user_input(db_session, requested_by)
         if not requested_by:
             return False
         requested_by_id = requested_by.id
         song_info = SongRequestSongInfo._create_or_get(
             db_session, video_id, self.youtube)
         if not song_info:
             log.error("There was an error!")
             return False
         skip_after = (self.settings["max_song_length"] if
                       song_info.duration > self.settings["max_song_length"]
                       else None)
         song = SongrequestQueue._create(db_session, video_id, skip_after,
                                         requested_by_id)
         if queue:
             song._move_song(queue)
         db_session.commit()
         current_song = SongrequestQueue._from_id(db_session,
                                                  self.current_song_id)
         if not current_song or not current_song.requested_by:
             self.load_song()
     return True
コード例 #2
0
 def inc_current_song(self):
     while True:
         if not self.enabled:
             break
         if self.current_song_id:
             if not self.paused:
                 try:
                     with DBManager.create_session_scope() as db_session:
                         current_song = SongrequestQueue._from_id(
                             db_session, self.current_song_id)
                         next_song = SongrequestQueue._get_next_song(
                             db_session)
                         if not current_song or (
                                 current_song.skip_after
                                 and current_song.skip_after <
                                 current_song.current_song_time + 10):
                             self.load_song()
                         else:
                             if (not current_song.requested_by
                                 ) and next_song and next_song.requested_by:
                                 self.load_song()
                             current_song.current_song_time += 1
                 except Exception as e:
                     log.error(e)
         elif self.module_opened:
             self.load_song()
         time.sleep(1)
コード例 #3
0
    def unban_function(self,
                       database_id=None,
                       hist_database_id=None,
                       songinfo_database_id=None):
        if not self.module_state["enabled"]:
            return False

        if not songinfo_database_id and not database_id and not hist_database_id:
            return False

        with DBManager.create_session_scope() as db_session:
            if database_id:
                song = SongrequestQueue._from_id(db_session, int(database_id))
                song_info = song.song_info
            elif songinfo_database_id:
                song_info = SongRequestSongInfo._get(db_session,
                                                     songinfo_database_id)
            else:
                song = SongrequestHistory._from_id(db_session,
                                                   int(hist_database_id))
                song_info = song.song_info

            if not song_info.banned:
                return False

            song_info.banned = False
            db_session.merge(song_info)
            db_session.commit()
        self._playlist()
        self._backup_playlist()
        self._playlist_history()
        self._favourite_list()
        self._banned_list()
        return True
コード例 #4
0
 def _dump_state(self, db_session):
     song = (SongrequestQueue._from_id(
         db_session,
         manager_ext.bot.songrequest_manager.current_song_id)
             if manager_ext.bot.songrequest_manager.current_song_id
             else None)
     data = {
         "volume":
         manager_ext.bot.songrequest_manager.volume_val,
         "current_song":
         song.webjsonify() if song else {},
         "module_state":
         manager_ext.bot.songrequest_manager.module_state,
         "playlist":
         SongrequestQueue._get_playlist(db_session, limit=30),
         "backup_playlist":
         SongrequestQueue._get_backup_playlist(db_session,
                                               limit=30),
         "history_list":
         SongrequestHistory._get_history(db_session, limit=30),
         "banned_list":
         SongRequestSongInfo._get_banned_list(db_session),
         "favourite_list":
         SongRequestSongInfo._get_favourite_list(db_session),
         "current_timestamp":
         str(utils.now().timestamp()),
     }
     payload = {"event": "initialize", "data": data}
     self.sendMessage(json.dumps(payload).encode("utf8"), False)
コード例 #5
0
 def remove_function(self, database_id):
     if not self.module_state["enabled"]:
         return False
     with DBManager.create_session_scope() as db_session:
         song = SongrequestQueue._from_id(db_session, database_id)
         song._remove(db_session)
         db_session.commit()
     self._playlist()
     return True
コード例 #6
0
 def move_function(self, database_id, to_id):
     if not self.enabled:
         return False
     with DBManager.create_session_scope() as db_session:
         song = SongrequestQueue._from_id(db_session, database_id)
         song._move_song(db_session, to_id)
         db_session.commit()
     self._playlist()
     SongrequestQueue._update_queue()
     return True
コード例 #7
0
 def seek_function(self, _time):
     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)
             current_song.current_song_time = _time
             self._seek(_time)
         return True
     return False
コード例 #8
0
 def play_function(self, database_id, skipped_by):
     if not self.module_state["enabled"]:
         return False
     with DBManager.create_session_scope() as db_session:
         skipped_by = User.find_by_user_input(db_session, skipped_by)
         if not skipped_by:
             return
         skipped_by_id = skipped_by.id
         song = SongrequestQueue._from_id(db_session, database_id)
         song._move_song(db_session, 1)
         db_session.commit()
     self.load_song(skipped_by_id)
     return True
コード例 #9
0
 def replay_function(self, requested_by):
     if not self.enabled:
         return False
     with DBManager.create_session_scope() as db_session:
         requested_by = User.find_by_user_input(db_session, requested_by)
         if not requested_by:
             return False
         requested_by_id = requested_by.id
         current_song = SongrequestQueue._from_id(db_session,
                                                  self.current_song_id)
         self.request_function(current_song.video_id,
                               current_song.requested_by_id, 1)
         db_session.commit()
     self.load_song(requested_by_id)
     SongrequestQueue._update_queue()
     return True
コード例 #10
0
    def pause_function(self):
        if not self.module_state["enabled"] or not self.current_song_id:
            return False
        if not self.module_state["paused"]:
            self.module_state["paused"] = True
            self._module_state()
            self._pause()
            self.remove_schedule()
            with DBManager.create_session_scope() as db_session:
                song = SongrequestQueue._from_id(db_session,
                                                 self.current_song_id)
                song.played_for = (utils.now() -
                                   song.date_resumed).total_seconds()
                song.date_resumed = None
            return True

        return False
コード例 #11
0
 def requeue_function(self, database_id, requested_by):
     if not self.module_state["enabled"]:
         return False
     with DBManager.create_session_scope() as db_session:
         requested_by = User.find_by_user_input(db_session, requested_by)
         if not requested_by:
             return False
         requested_by_id = requested_by.id
         SongrequestHistory._from_id(db_session, database_id).requeue(
             db_session, requested_by_id)
         db_session.commit()
         current_song = SongrequestQueue._from_id(db_session,
                                                  self.current_song_id)
         if not current_song or not current_song.requested_by:
             self.load_song()
     self._playlist()
     return True
コード例 #12
0
 def seek_function(self, _time):
     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)
             current_song.played_for += _time - current_song.current_song_time
             db_session.merge(current_song)
             db_session.commit()
             self.remove_schedule()
             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])
             self._seek(_time, current_song.webjsonify())
         return True
     return False
コード例 #13
0
    def resume_function(self):
        if not self.module_state["enabled"] or not self.current_song_id:
            return False
        if self.module_state["paused"]:
            self.module_state["paused"] = False
            self._module_state()
            self._resume()
            with DBManager.create_session_scope() as db_session:
                song = SongrequestQueue._from_id(db_session,
                                                 self.current_song_id)
                song.date_resumed = utils.now()
                self.schedule_job_id = random.randint(1, 100000)
                self.current_song_schedule = ScheduleManager.execute_delayed(
                    song.time_left + 10,
                    self.load_song_schedule,
                    args=[self.schedule_job_id])

            return True
        return False
コード例 #14
0
    def favourite_function(self, database_id=None, hist_database_id=None):
        if not self.module_state["enabled"] or (not database_id
                                                and not hist_database_id):
            return False
        with DBManager.create_session_scope() as db_session:
            if database_id:
                song = SongrequestQueue._from_id(db_session, int(database_id))
            else:
                song = SongrequestHistory._from_id(db_session,
                                                   int(hist_database_id))

            if song.song_info.favourite:
                return False
            song.song_info.favourite = True
            db_session.merge(song.song_info)
            db_session.commit()
        self._playlist()
        self._backup_playlist()
        self._playlist_history()
        self._favourite_list()
        self._banned_list()
        return True
コード例 #15
0
 def _dump_state(self, db_session):
     current_song = SongrequestQueue._from_id(
         db_session,
         manager_ext.bot.songrequest_manager.current_song_id)
     data = {
         "event": "initialize",
         "data": {
             "currentSong":
             current_song.webjsonify() if current_song else None,
             "playlist":
             SongrequestQueue._get_playlist(db_session, 15),
             "history":
             SongrequestHistory._get_history(db_session, 15),
             "paused":
             manager_ext.bot.songrequest_manager.paused,
             "open":
             manager_ext.bot.songrequest_manager.module_opened,
             "volume":
             manager_ext.bot.songrequest_manager.volume_val(),
         },
     }
     payload = json.dumps(data).encode("utf8")
     self.sendMessage(payload, False)
コード例 #16
0
ファイル: songrequest.py プロジェクト: metrize/troybot
    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}"
            current_song = SongrequestQueue._from_id(
                db_session, self.bot.songrequest_manager.current_song_id)
            if not current_song or not current_song.requested_by:
                self.bot.songrequest_manager.load_song()
            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
コード例 #17
0
    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
コード例 #18
0
    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