Example #1
0
    def create_stream_chunk(self, status):
        if self.current_stream_chunk is not None:
            # There's already a stream chunk started!
            self.current_stream_chunk.chunk_end = datetime.datetime.now()
            DBManager.session_add_expunge(self.current_stream_chunk)

        stream_chunk = None

        with DBManager.create_session_scope(
                expire_on_commit=False) as db_session:
            stream_chunk = db_session.query(StreamChunk).filter_by(
                broadcast_id=status['broadcast_id']).one_or_none()
            if stream_chunk is None:
                log.info('Creating stream chunk, from create_stream_chunk')
                stream_chunk = StreamChunk(self.current_stream,
                                           status['broadcast_id'],
                                           status['created_at'])
                self.current_stream_chunk = stream_chunk
                db_session.add(stream_chunk)
                db_session.commit()
            else:
                log.info('We already have a stream chunk!')
                self.current_stream_chunk = stream_chunk
                stream_chunk = None
            db_session.expunge_all()

        if stream_chunk:
            self.current_stream.stream_chunks.append(stream_chunk)
Example #2
0
    def add_banphrase(bot, source, message, **rest):
        """Method for creating and editing banphrases.
        Usage: !add banphrase BANPHRASE [options]
        Multiple options available:
        --length LENGTH
        --perma/--no-perma
        --notify/--no-notify
        """

        if message:
            options, phrase = bot.banphrase_manager.parse_banphrase_arguments(message)

            if options is False:
                bot.whisper(source, "Invalid banphrase")
                return False

            options["added_by"] = source.id
            options["edited_by"] = source.id

            banphrase, new_banphrase = bot.banphrase_manager.create_banphrase(phrase, **options)

            if new_banphrase is True:
                bot.whisper(source, f"Added your banphrase (ID: {banphrase.id})")
                AdminLogManager.post("Banphrase added", source, banphrase.id, banphrase.phrase)
                return True

            banphrase.set(**options)
            banphrase.data.set(edited_by=options["edited_by"])
            DBManager.session_add_expunge(banphrase)
            bot.banphrase_manager.commit()
            bot.whisper(
                source,
                f"Updated your banphrase (ID: {banphrase.id}) with ({', '.join([key for key in options if key != 'added_by'])})",
            )
            AdminLogManager.post("Banphrase edited", source, banphrase.id, banphrase.phrase)
Example #3
0
    def create_stream_chunk(self, status: UserStream):
        if self.current_stream is None:
            log.warn(
                "create_stream_chunk called with current_stream being None")
            return

        if self.current_stream_chunk is not None:
            # There's already a stream chunk started!
            self.current_stream_chunk.chunk_end = utils.now()
            DBManager.session_add_expunge(self.current_stream_chunk)

        stream_chunk: Optional[StreamChunk] = None

        with DBManager.create_session_scope(
                expire_on_commit=False) as db_session:
            stream_chunk = db_session.query(StreamChunk).filter_by(
                broadcast_id=status.id).one_or_none()
            if stream_chunk is None:
                log.info("Creating stream chunk, from create_stream_chunk")
                stream_chunk = StreamChunk(self.current_stream, status.id,
                                           status.started_at)
                self.current_stream_chunk = stream_chunk
                db_session.add(stream_chunk)
                db_session.commit()
            else:
                log.info("We already have a stream chunk!")
                self.current_stream_chunk = stream_chunk
                stream_chunk = None
            db_session.expunge_all()

        if stream_chunk:
            self.current_stream.stream_chunks.append(stream_chunk)
Example #4
0
    def add_autoresponse(self, **options):
        """Method for creating and editing autoresponses.
        Usage: !add autoresponse TRIGGER RESPONSE [options]
        Multiple options available:
        --whisper/--no-whisper
        """

        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            options, phrase = bot.autoresponse_manager.parse_autoresponse_arguments(message)

            if options is False:
                bot.whisper(source.username, 'Invalid autoresponse')
                return False

            options['added_by'] = source.id
            options['edited_by'] = source.id

            autoresponse, new_autoresponse = bot.autoresponse_manager.create_autoresponse(phrase, **options)

            if new_autoresponse is True:
                bot.whisper(source.username, 'Added your autoresponse (ID: {autoresponse.id})'.format(autoresponse=autoresponse))
                AdminLogManager.post('Banphrase added', source, phrase)
                return True

            autoresponse.set(**options)
            autoresponse.data.set(edited_by=options['edited_by'])
            DBManager.session_add_expunge(autoresponse)
            bot.autoresponse_manager.commit()
            bot.whisper(source.username, 'Updated your autoresponse (ID: {autoresponse.id}) with ({what})'.format(autoresponse=autoresponse, what=', '.join([key for key in options if key != 'added_by'])))
            AdminLogManager.post('Banphrase edited', source, phrase)
Example #5
0
    def refresh_video_url_stage2(self, data):
        if self.online is False:
            return

        if self.current_stream_chunk is None or self.current_stream is None:
            return

        log.info('Attempting to fetch video url for broadcast {0}'.format(
            self.current_stream_chunk.broadcast_id))
        stream_chunk = self.current_stream_chunk if self.current_stream_chunk.video_url is None else None
        video_url, video_preview_image_url, video_recorded_at = self.fetch_video_url_stage2(
            data)
        if video_url is not None:
            log.info('Successfully fetched a video url: {0}'.format(video_url))
            if self.current_stream_chunk is None or self.current_stream_chunk.video_url is None:
                with DBManager.create_session_scope(
                        expire_on_commit=False) as db_session:
                    self.current_stream_chunk.video_url = video_url
                    self.current_stream_chunk.video_preview_image_url = video_preview_image_url

                    db_session.add(self.current_stream_chunk)

                    db_session.commit()

                    db_session.expunge_all()
                log.info('Successfully commited video url data.')
            elif self.current_stream_chunk.video_url != video_url:
                # End current stream chunk
                self.current_stream_chunk.chunk_end = datetime.datetime.now()
                DBManager.session_add_expunge(self.current_stream_chunk)

                with DBManager.create_session_scope(
                        expire_on_commit=False) as db_session:
                    stream_chunk = StreamChunk(
                        self.current_stream,
                        self.current_stream_chunk.broadcast_id,
                        video_recorded_at)
                    self.current_stream_chunk = stream_chunk
                    self.current_stream_chunk.video_url = video_url
                    self.current_stream_chunk.video_preview_image_url = video_preview_image_url

                    db_session.add(self.current_stream_chunk)

                    db_session.commit()

                    db_session.expunge_all()
                log.info(
                    'Successfully commited video url data in a new chunk.')
        else:
            log.info('Not video for broadcast found')
Example #6
0
    def refresh_video_url_stage2(self, data):
        if self.online is False:
            return

        if self.current_stream_chunk is None or self.current_stream is None:
            return

        log.info(
            f"Attempting to fetch video url for broadcast {self.current_stream_chunk.broadcast_id}"
        )
        video_url, video_preview_image_url, video_recorded_at = self.fetch_video_url_stage2(
            data)

        if video_url is None:
            log.info("No video for broadcast found")
            return

        log.info(f"Successfully fetched a video url: {video_url}")
        if self.current_stream_chunk is None or self.current_stream_chunk.video_url is None:
            with DBManager.create_session_scope(
                    expire_on_commit=False) as db_session:
                self.current_stream_chunk.video_url = video_url
                self.current_stream_chunk.video_preview_image_url = video_preview_image_url

                db_session.add(self.current_stream_chunk)

                db_session.commit()

                db_session.expunge_all()
            log.info("Successfully commited video url data.")
        elif self.current_stream_chunk.video_url != video_url:
            # End current stream chunk
            self.current_stream_chunk.chunk_end = utils.now()
            DBManager.session_add_expunge(self.current_stream_chunk)

            with DBManager.create_session_scope(
                    expire_on_commit=False) as db_session:
                stream_chunk = StreamChunk(
                    self.current_stream,
                    self.current_stream_chunk.broadcast_id, video_recorded_at)
                self.current_stream_chunk = stream_chunk
                self.current_stream_chunk.video_url = video_url
                self.current_stream_chunk.video_preview_image_url = video_preview_image_url

                db_session.add(self.current_stream_chunk)

                db_session.commit()

                db_session.expunge_all()
            log.info("Successfully commited video url data in a new chunk.")
Example #7
0
    def add_banphrase(**options):
        """Method for creating and editing banphrases.
        Usage: !add banphrase BANPHRASE [options]
        Multiple options available:
        --length LENGTH
        --perma/--no-perma
        --notify/--no-notify
        """

        message = options["message"]
        bot = options["bot"]
        source = options["source"]

        if message:
            options, phrase = bot.banphrase_manager.parse_banphrase_arguments(
                message)

            if options is False:
                bot.whisper(source.username, "Invalid banphrase")
                return False

            options["added_by"] = source.id
            options["edited_by"] = source.id

            banphrase, new_banphrase = bot.banphrase_manager.create_banphrase(
                phrase, **options)

            if new_banphrase is True:
                bot.whisper(
                    source.username,
                    "Added your banphrase (ID: {banphrase.id})".format(
                        banphrase=banphrase))
                AdminLogManager.post("Banphrase added", source, phrase)
                return True

            banphrase.set(**options)
            banphrase.data.set(edited_by=options["edited_by"])
            DBManager.session_add_expunge(banphrase)
            bot.banphrase_manager.commit()
            bot.whisper(
                source.username,
                "Updated your banphrase (ID: {banphrase.id}) with ({what})".
                format(banphrase=banphrase,
                       what=", ".join(
                           [key for key in options if key != "added_by"])),
            )
            AdminLogManager.post("Banphrase edited", source, phrase)
Example #8
0
    def add_banphrase(self, **options):
        """Method for creating and editing banphrases.
        Usage: !add banphrase BANPHRASE [options]
        Multiple options available:
        --length LENGTH
        --perma/--no-perma
        --notify/--no-notify
        """

        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            options, phrase = bot.banphrase_manager.parse_banphrase_arguments(
                message)

            if options is False:
                bot.whisper(source.username, 'Invalid banphrase')
                return False

            options['added_by'] = source.id
            options['edited_by'] = source.id

            banphrase, new_banphrase = bot.banphrase_manager.create_banphrase(
                phrase, **options)

            if new_banphrase is True:
                bot.whisper(
                    source.username,
                    'Added your banphrase (ID: {banphrase.id})'.format(
                        banphrase=banphrase))
                AdminLogManager.post('Banphrase added', source, phrase)
                return True

            banphrase.set(**options)
            banphrase.data.set(edited_by=options['edited_by'])
            DBManager.session_add_expunge(banphrase)
            bot.banphrase_manager.commit()
            bot.whisper(
                source.username,
                'Updated your banphrase (ID: {banphrase.id}) with ({what})'.
                format(banphrase=banphrase,
                       what=', '.join(
                           [key for key in options if key != 'added_by'])))
            AdminLogManager.post('Banphrase edited', source, phrase)
Example #9
0
    def refresh_video_url_stage2(self, data) -> None:
        if self.online is False:
            return

        if self.current_stream_chunk is None or self.current_stream is None:
            return

        video: Optional[TwitchVideo] = self.fetch_video_url_stage2(data)

        if video is None:
            return

        log.info(f"Successfully fetched a video url: {video.url}")
        if self.current_stream_chunk is None or self.current_stream_chunk.video_url is None:
            with DBManager.create_session_scope(
                    expire_on_commit=False) as db_session:
                self.current_stream_chunk.video_url = video.url
                self.current_stream_chunk.video_preview_image_url = video.thumbnail_url

                db_session.add(self.current_stream_chunk)

                db_session.commit()

                db_session.expunge_all()
            log.info("Successfully commited video url data.")
        elif self.current_stream_chunk.video_url != video.url:
            # End current stream chunk
            self.current_stream_chunk.chunk_end = utils.now()
            DBManager.session_add_expunge(self.current_stream_chunk)

            with DBManager.create_session_scope(
                    expire_on_commit=False) as db_session:
                stream_chunk = StreamChunk(
                    self.current_stream,
                    self.current_stream_chunk.broadcast_id, video.created_at)
                self.current_stream_chunk = stream_chunk
                self.current_stream_chunk.video_url = video.url
                self.current_stream_chunk.video_preview_image_url = video.thumbnail_url

                db_session.add(self.current_stream_chunk)

                db_session.commit()

                db_session.expunge_all()
            log.info("Successfully commited video url data in a new chunk.")
Example #10
0
    def refresh_video_url_stage2(self, data):
        if self.online is False:
            return

        if self.current_stream_chunk is None or self.current_stream is None:
            return

        log.info('Attempting to fetch video url for broadcast {0}'.format(self.current_stream_chunk.broadcast_id))
        stream_chunk = self.current_stream_chunk if self.current_stream_chunk.video_url is None else None
        video_url, video_preview_image_url, video_recorded_at = self.fetch_video_url_stage2(data)
        if video_url is not None:
            log.info('Successfully fetched a video url: {0}'.format(video_url))
            if self.current_stream_chunk is None or self.current_stream_chunk.video_url is None:
                with DBManager.create_session_scope(expire_on_commit=False) as db_session:
                    self.current_stream_chunk.video_url = video_url
                    self.current_stream_chunk.video_preview_image_url = video_preview_image_url

                    db_session.add(self.current_stream_chunk)

                    db_session.commit()

                    db_session.expunge_all()
                log.info('Successfully commited video url data.')
            elif self.current_stream_chunk.video_url != video_url:
                # End current stream chunk
                self.current_stream_chunk.chunk_end = datetime.datetime.now()
                DBManager.session_add_expunge(self.current_stream_chunk)

                with DBManager.create_session_scope(expire_on_commit=False) as db_session:
                    stream_chunk = StreamChunk(self.current_stream, self.current_stream_chunk.broadcast_id, video_recorded_at)
                    self.current_stream_chunk = stream_chunk
                    self.current_stream_chunk.video_url = video_url
                    self.current_stream_chunk.video_preview_image_url = video_preview_image_url

                    db_session.add(self.current_stream_chunk)

                    db_session.commit()

                    db_session.expunge_all()
                log.info('Successfully commited video url data in a new chunk.')
        else:
            log.info('Not video for broadcast found')
Example #11
0
    def create_stream_chunk(self, status):
        if self.current_stream_chunk is not None:
            # There's already a stream chunk started!
            self.current_stream_chunk.chunk_end = datetime.datetime.now()
            DBManager.session_add_expunge(self.current_stream_chunk)

        stream_chunk = None

        with DBManager.create_session_scope(expire_on_commit=False) as db_session:
            stream_chunk = db_session.query(StreamChunk).filter_by(broadcast_id=status['broadcast_id']).one_or_none()
            if stream_chunk is None:
                log.info('Creating stream chunk, from create_stream_chunk')
                stream_chunk = StreamChunk(self.current_stream, status['broadcast_id'], status['created_at'])
                self.current_stream_chunk = stream_chunk
                db_session.add(stream_chunk)
                db_session.commit()
            else:
                log.info('We already have a stream chunk!')
                self.current_stream_chunk = stream_chunk
                stream_chunk = None
            db_session.expunge_all()

        if stream_chunk:
            self.current_stream.stream_chunks.append(stream_chunk)
Example #12
0
    def add_banphrase(self, **options):
        """Method for creating and editing banphrases.
        Usage: !add banphrase BANPHRASE [options]
        Multiple options available:
        --length LENGTH
        --perma/--no-perma
        --notify/--no-notify
        """

        message = options['message']
        bot = options['bot']
        source = options['source']

        if message:
            options, phrase = bot.banphrase_manager.parse_banphrase_arguments(message)

            if options is False:
                bot.whisper(source.username, 'Invalid banphrase')
                return False

            options['added_by'] = source.id
            options['edited_by'] = source.id

            banphrase, new_banphrase = bot.banphrase_manager.create_banphrase(phrase, **options)

            if new_banphrase is True:
                bot.whisper(source.username, 'Added your banphrase (ID: {banphrase.id})'.format(banphrase=banphrase))
                AdminLogManager.post('Banphrase added', source, phrase)
                return True

            banphrase.set(**options)
            banphrase.data.set(edited_by=options['edited_by'])
            DBManager.session_add_expunge(banphrase)
            bot.banphrase_manager.commit()
            bot.whisper(source.username, 'Updated your banphrase (ID: {banphrase.id}) with ({what})'.format(banphrase=banphrase, what=', '.join([key for key in options if key != 'added_by'])))
            AdminLogManager.post('Banphrase edited', source, phrase)
Example #13
0
 def edit_command(self, command_to_edit, **options):
     command_to_edit.set(**options)
     command_to_edit.data.set(**options)
     DBManager.session_add_expunge(command_to_edit)
     self.commit()
Example #14
0
 def edit_command(self, command_to_edit, **options):
     command_to_edit.set(**options)
     command_to_edit.data.set(**options)
     DBManager.session_add_expunge(command_to_edit)
     self.commit()