Beispiel #1
0
    def enable(self, bot):
        if not self.bot:
            return

        import apiclient
        from apiclient.discovery import build

        def build_request(_, *args, **kwargs):
            import httplib2

            new_http = httplib2.Http()
            return apiclient.http.HttpRequest(new_http, *args, **kwargs)

        self.youtube = build("youtube",
                             "v3",
                             developerKey=self.settings["youtube_key"],
                             requestBuilder=build_request)

        with DBManager.create_session_scope() as db_session:
            SongrequestQueue._clear_backup_songs(db_session)
            if self.settings["backup_playlist_id"] and self.settings[
                    "backup_playlist_id"] != "":
                backup_songs = self.getBackUpListSongs()
                random.shuffle(backup_songs)
                SongrequestQueue._load_backup_songs(db_session, backup_songs,
                                                    self.youtube,
                                                    self.settings)
            db_session.commit()
        SongrequestQueue._update_queue()
        self.bot.songrequest_manager.enable(self.settings, self.youtube)
        HandlerManager.add_handler(
            "on_stream_stop",
            self.bot.songrequest_manager.close_module_function)
Beispiel #2
0
 def remove_function(self, database_id):
     if not self.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()
     SongrequestQueue._update_queue()
     self._playlist()
     return True
Beispiel #3
0
 def requeue_function(self, database_id, 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
         SongrequestHistory._from_id(db_session, database_id).requeue(
             db_session, requested_by_id)
         db_session.commit()
     SongrequestQueue._update_queue()
     self._playlist()
     return True
Beispiel #4
0
 def play_function(self, database_id, skipped_by):
     if not self.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)
     SongrequestQueue._update_queue()
     return True
Beispiel #5
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
Beispiel #6
0
 def request_function(self, video_id, requested_by, queue=None):
     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
         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(db_session, queue)
         db_session.commit()
     SongrequestQueue._update_queue()
     return True
Beispiel #7
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