Example #1
0
    def enable(self, bot):
        self.bot = bot
        HandlerManager.add_handler('on_message', self.on_message, priority=100)
        HandlerManager.add_handler('on_commit', self.on_commit)
        if bot:
            self.run_later = bot.execute_delayed

            if 'safebrowsingapi' in bot.config['main']:
                # XXX: This should be loaded as a setting instead.
                # There needs to be a setting for settings to have them as "passwords"
                # so they're not displayed openly
                self.safeBrowsingAPI = SafeBrowsingAPI(
                    bot.config['main']['safebrowsingapi'], bot.nickname,
                    bot.version)
            else:
                self.safeBrowsingAPI = None

        if self.db_session is not None:
            self.db_session.commit()
            self.db_session.close()
            self.db_session = None
        self.db_session = DBManager.create_session()
        self.blacklisted_links = []
        for link in self.db_session.query(BlacklistedLink):
            self.blacklisted_links.append(link)

        self.whitelisted_links = []
        for link in self.db_session.query(WhitelistedLink):
            self.whitelisted_links.append(link)
Example #2
0
    def enable(self, bot):
        self.bot = bot
        HandlerManager.add_handler('on_message', self.on_message, priority=100)
        HandlerManager.add_handler('on_commit', self.on_commit)
        if bot:
            self.run_later = bot.execute_delayed

            if 'safebrowsingapi' in bot.config['main']:
                # XXX: This should be loaded as a setting instead.
                # There needs to be a setting for settings to have them as "passwords"
                # so they're not displayed openly
                self.safeBrowsingAPI = SafeBrowsingAPI(bot.config['main']['safebrowsingapi'], bot.nickname, bot.version)
            else:
                self.safeBrowsingAPI = None

        if self.db_session is not None:
            self.db_session.commit()
            self.db_session.close()
            self.db_session = None
        self.db_session = DBManager.create_session()
        self.blacklisted_links = []
        for link in self.db_session.query(BlacklistedLink):
            self.blacklisted_links.append(link)

        self.whitelisted_links = []
        for link in self.db_session.query(WhitelistedLink):
            self.whitelisted_links.append(link)
Example #3
0
def get_user(username):
    session = DBManager.create_session()
    user = session.query(User).filter_by(username=username).one_or_none()
    if user is None:
        return make_response(jsonify({'error': 'Not found'}), 404)

    rank = session.query(func.Count(
        User.id)).filter(User.points > user.points).one()
    rank = rank[0] + 1
    session.close()
    if user:
        accessible_data = {
            'id': user.id,
            'username': user.username,
            'username_raw': user.username_raw,
            'points': user.points,
            'rank': rank,
            'level': user.level,
            'last_seen': user.last_seen,
            'last_active': user.last_active,
            'subscriber': user.subscriber,
            'num_lines': user.num_lines,
            'minutes_in_chat_online': user.minutes_in_chat_online,
            'minutes_in_chat_offline': user.minutes_in_chat_offline,
            'banned': user.banned,
            'ignored': user.ignored,
        }
        return jsonify(accessible_data)

    return make_response(jsonify({'error': 'Not found'}), 404)
Example #4
0
File: api.py Project: coral/pajbot
def get_user(username):
    session = DBManager.create_session()
    user = session.query(User).filter_by(username=username).one_or_none()
    if user is None:
        return make_response(jsonify({'error': 'Not found'}), 404)

    rank = session.query(func.Count(User.id)).filter(User.points > user.points).one()
    rank = rank[0] + 1
    session.close()
    if user:
        accessible_data = {
                'id': user.id,
                'username': user.username,
                'username_raw': user.username_raw,
                'points': user.points,
                'rank': rank,
                'level': user.level,
                'last_seen': user.last_seen,
                'last_active': user.last_active,
                'subscriber': user.subscriber,
                'num_lines': user.num_lines,
                'minutes_in_chat_online': user.minutes_in_chat_online,
                'minutes_in_chat_offline': user.minutes_in_chat_offline,
                'banned': user.banned,
                'ignored': user.ignored,
                }
        return jsonify(accessible_data)

    return make_response(jsonify({'error': 'Not found'}), 404)
Example #5
0
 def decks_warrior():
     session = DBManager.create_session()
     decks = session.query(Deck).filter_by(deck_class='warrior').order_by(Deck.last_used.desc(), Deck.first_used.desc()).all()
     session.close()
     return render_template('decks/by_class.html',
             decks=decks,
             deck_class='Warrior')
Example #6
0
 def decks_warlock():
     session = DBManager.create_session()
     decks = session.query(Deck).filter_by(deck_class='warlock').order_by(
         Deck.last_used.desc(), Deck.first_used.desc()).all()
     session.close()
     return render_template('decks/by_class.html',
                            decks=decks,
                            deck_class='Warlock')
Example #7
0
 def decks():
     session = DBManager.create_session()
     top_decks = []
     for deck in session.query(Deck).order_by(Deck.last_used.desc(), Deck.first_used.desc())[:25]:
         top_decks.append(deck)
     session.close()
     return render_template('decks/all.html',
             top_decks=top_decks,
             deck_class=None)
Example #8
0
    def __init__(self, bot):
        self.bot = bot
        self.banphrases = []
        self.enabled_banphrases = []
        self.db_session = DBManager.create_session(expire_on_commit=False)

        if self.bot:
            self.bot.socket_manager.add_handler('banphrase.update', self.on_banphrase_update)
            self.bot.socket_manager.add_handler('banphrase.remove', self.on_banphrase_remove)
Example #9
0
    def enable(self, bot):
        HandlerManager.add_handler('on_message', self.on_message, priority=200)
        HandlerManager.add_handler('on_commit', self.on_commit)

        if self.db_session is not None:
            self.db_session.commit()
            self.db_session.close()
            self.db_session = None
            self.links = {}
        self.db_session = DBManager.create_session()
Example #10
0
 def decks():
     session = DBManager.create_session()
     top_decks = []
     for deck in session.query(Deck).order_by(Deck.last_used.desc(),
                                              Deck.first_used.desc())[:25]:
         top_decks.append(deck)
     session.close()
     return render_template('decks/all.html',
                            top_decks=top_decks,
                            deck_class=None)
Example #11
0
    def enable(self, bot):
        HandlerManager.add_handler('on_message', self.on_message, priority=200)
        HandlerManager.add_handler('on_commit', self.on_commit)

        if self.db_session is not None:
            self.db_session.commit()
            self.db_session.close()
            self.db_session = None
            self.links = {}
        self.db_session = DBManager.create_session()
Example #12
0
    def __init__(self, bot):
        self.bot = bot
        self.banphrases = []
        self.enabled_banphrases = []
        self.db_session = DBManager.create_session(expire_on_commit=False)

        if self.bot:
            self.bot.socket_manager.add_handler('banphrase.update',
                                                self.on_banphrase_update)
            self.bot.socket_manager.add_handler('banphrase.remove',
                                                self.on_banphrase_remove)
Example #13
0
    def __init__(self, reactor, bot, target, message_limit, time_interval, num_of_conns=30):
        self.db_session = DBManager.create_session()
        self.reactor = reactor
        self.bot = bot
        self.message_limit = message_limit
        self.time_interval = time_interval
        self.num_of_conns = num_of_conns
        self.whisper_thread = None

        self.connlist = []
        self.whispers = Queue()

        self.maintenance_lock = False
Example #14
0
    def __init__(self, bot):
        UserDict.__init__(self)
        self.bot = bot
        self.streamer = bot.streamer
        self.db_session = DBManager.create_session()
        self.custom_data = []
        self.bttv_emote_manager = BTTVEmoteManager(self)

        self.bot.execute_delayed(5, self.bot.action_queue.add, (self.bttv_emote_manager.update_emotes, ))
        self.bot.execute_every(60 * 60 * 2, self.bot.action_queue.add, (self.bttv_emote_manager.update_emotes, ))

        # Used as caching to store emotes
        self.global_emotes = []
Example #15
0
    def __init__(self, socket_manager=None, module_manager=None, bot=None):
        UserDict.__init__(self)
        self.db_session = DBManager.create_session()

        self.internal_commands = {}
        self.db_commands = {}
        self.module_commands = {}

        self.bot = bot
        self.module_manager = module_manager

        if socket_manager:
            socket_manager.add_handler('module.update', self.on_module_reload)
            socket_manager.add_handler('command.update', self.on_command_update)
            socket_manager.add_handler('command.remove', self.on_command_remove)
Example #16
0
 def highlights():
     session = DBManager.create_session()
     dates_with_highlights = []
     highlights = session.query(StreamChunkHighlight).options(joinedload('*')).filter(StreamChunkHighlight.created_at >= (datetime.datetime.utcnow() - datetime.timedelta(days=60))).order_by(StreamChunkHighlight.created_at_with_offset.desc()).all()
     for highlight in highlights:
         dates_with_highlights.append(datetime.datetime(
             year=highlight.created_at.year,
             month=highlight.created_at.month,
             day=highlight.created_at.day))
     try:
         return render_template('highlights.html',
                 highlights=highlights[:10],
                 dates_with_highlights=set(dates_with_highlights))
     finally:
         session.close()
Example #17
0
    def __init__(self, socket_manager=None, module_manager=None, bot=None):
        UserDict.__init__(self)
        self.db_session = DBManager.create_session()

        self.internal_commands = {}
        self.db_commands = {}
        self.module_commands = {}

        self.bot = bot
        self.module_manager = module_manager

        if socket_manager:
            socket_manager.add_handler('module.update', self.on_module_reload)
            socket_manager.add_handler('command.update',
                                       self.on_command_update)
            socket_manager.add_handler('command.remove',
                                       self.on_command_remove)
Example #18
0
    def __init__(self, bot):
        self.bot = bot
        self.db_session = DBManager.create_session()

        self.twitter_client = None
        self.twitter_stream = None
        self.listener = None

        if 'twitter' in bot.config:
            self.use_twitter_stream = 'streaming' in bot.config['twitter'] and bot.config['twitter']['streaming'] == '1'

            try:
                self.twitter_auth = tweepy.OAuthHandler(bot.config['twitter']['consumer_key'], bot.config['twitter']['consumer_secret'])
                self.twitter_auth.set_access_token(bot.config['twitter']['access_token'], bot.config['twitter']['access_token_secret'])

                self.twitter_client = tweepy.API(self.twitter_auth)

                if self.use_twitter_stream:
                    self.connect_to_twitter_stream()
                    bot.execute_every(60 * 5, self.check_twitter_connection)
            except:
                log.exception('Twitter authentication failed.')
                self.twitter_client = None
Example #19
0
    def highlight_list_date(date):
        # Make sure we were passed a valid date
        try:
            parsed_date = datetime.datetime.strptime(date, '%Y-%m-%d')
        except ValueError:
            # Invalid date
            return redirect('/highlights/', 303)
        session = DBManager.create_session()
        dates_with_highlights = []
        highlights = session.query(StreamChunkHighlight).options(joinedload('*')).filter(cast(StreamChunkHighlight.created_at, Date) == parsed_date).order_by(StreamChunkHighlight.created_at.desc()).all()
        for highlight in session.query(StreamChunkHighlight):
            dates_with_highlights.append(datetime.datetime(
                year=highlight.created_at.year,
                month=highlight.created_at.month,
                day=highlight.created_at.day))

        try:
            return render_template('highlights_date.html',
                    highlights=highlights,
                    date=parsed_date,
                    dates_with_highlights=set(dates_with_highlights))
        finally:
            session.close()
Example #20
0
    def __init__(self, config, args=None):
        self.load_config(config)
        self.last_ping = datetime.datetime.now()
        self.last_pong = datetime.datetime.now()

        self.load_default_phrases()

        self.db_session = DBManager.create_session()

        try:
            subprocess.check_call(
                ['alembic', 'upgrade', 'head'] +
                ['--tag="{0}"'.format(' '.join(sys.argv[1:]))])
        except subprocess.CalledProcessError:
            log.exception('aaaa')
            log.error(
                'Unable to call `alembic upgrade head`, this means the database could be out of date. Quitting.'
            )
            sys.exit(1)
        except PermissionError:
            log.error(
                'No permission to run `alembic upgrade head`. This means your user probably doesn\'t have execution rights on the `alembic` binary.'
            )
            log.error(
                'The error can also occur if it can\'t find `alembic` in your PATH, and instead tries to execute the alembic folder.'
            )
            sys.exit(1)
        except FileNotFoundError:
            log.error(
                'Could not found an installation of alembic. Please install alembic to continue.'
            )
            sys.exit(1)
        except:
            log.exception('Unhandled exception when calling db update')
            sys.exit(1)

        # Actions in this queue are run in a separate thread.
        # This means actions should NOT access any database-related stuff.
        self.action_queue = ActionQueue()
        self.action_queue.start()

        self.reactor = irc.client.Reactor(self.on_connect)
        self.start_time = datetime.datetime.now()
        ActionParser.bot = self

        HandlerManager.init_handlers()

        self.socket_manager = SocketManager(self)
        self.stream_manager = StreamManager(self)

        StreamHelper.init_bot(self, self.stream_manager)
        ScheduleManager.init()

        self.users = UserManager()
        self.decks = DeckManager()
        self.module_manager = ModuleManager(self.socket_manager,
                                            bot=self).load()
        self.commands = CommandManager(socket_manager=self.socket_manager,
                                       module_manager=self.module_manager,
                                       bot=self).load()
        self.filters = FilterManager().reload()
        self.banphrase_manager = BanphraseManager(self).load()
        self.timer_manager = TimerManager(self).load()
        self.kvi = KVIManager()
        self.emotes = EmoteManager(self).reload()
        self.twitter_manager = TwitterManager(self)
        self.duel_manager = DuelManager(self)

        HandlerManager.trigger('on_managers_loaded')

        # Reloadable managers
        self.reloadable = {
            'filters': self.filters,
            'emotes': self.emotes,
        }

        # Commitable managers
        self.commitable = {
            'commands': self.commands,
            'filters': self.filters,
            'emotes': self.emotes,
            'users': self.users,
            'banphrases': self.banphrase_manager,
        }

        self.execute_every(10 * 60, self.commit_all)

        try:
            self.admin = self.config['main']['admin']
        except KeyError:
            log.warning(
                'No admin user specified. See the [main] section in config.example.ini for its usage.'
            )
        if self.admin:
            self.users[self.admin].level = 2000

        self.parse_version()

        relay_host = self.config['main'].get('relay_host', None)
        relay_password = self.config['main'].get('relay_password', None)
        if relay_host is None or relay_password is None:
            self.irc = MultiIRCManager(self)
        else:
            self.irc = SingleIRCManager(self)

        self.reactor.add_global_handler('all_events', self.irc._dispatcher,
                                        -10)

        twitch_client_id = None
        twitch_oauth = None
        if 'twitchapi' in self.config:
            twitch_client_id = self.config['twitchapi'].get('client_id', None)
            twitch_oauth = self.config['twitchapi'].get('oauth', None)

        self.twitchapi = TwitchAPI(twitch_client_id, twitch_oauth)

        self.ascii_timeout_duration = 120
        self.msg_length_timeout_duration = 120

        self.data = {}
        self.data_cb = {}
        self.url_regex = re.compile(self.url_regex_str, re.IGNORECASE)

        self.data['broadcaster'] = self.streamer
        self.data['version'] = self.version
        self.data_cb['status_length'] = self.c_status_length
        self.data_cb['stream_status'] = self.c_stream_status
        self.data_cb['bot_uptime'] = self.c_uptime
        self.data_cb['current_time'] = self.c_current_time

        self.silent = True if args.silent else self.silent

        if self.silent:
            log.info('Silent mode enabled')

        self.reconnection_interval = 5
        """
        For actions that need to access the main thread,
        we can use the mainthread_queue.
        """
        self.mainthread_queue = ActionQueue()
        self.execute_every(1, self.mainthread_queue.parse_action)

        self.websocket_manager = WebSocketManager(self)

        try:
            if self.config['twitchapi']['update_subscribers'] == '1':
                self.execute_every(30 * 60, self.action_queue.add,
                                   (self.update_subscribers_stage1, ))
        except:
            pass

        # XXX: TEMPORARY UGLY CODE
        HandlerManager.add_handler('on_user_gain_tokens',
                                   self.on_user_gain_tokens)
Example #21
0
File: deck.py Project: coral/pajbot
 def __init__(self):
     UserList.__init__(self)
     self.db_session = DBManager.create_session()
     self.current_deck = None
Example #22
0
 def __init__(self):
     UserDict.__init__(self)
     self.db_session = DBManager.create_session()
Example #23
0
 def __init__(self):
     UserDict.__init__(self)
     self.db_session = DBManager.create_session()