示例#1
0
class AlertBot:
    def __init__(self, username):
        self.bot = accounts[username]
        self.start_time = times.get_current_timestamp()
        self.run = True
        self.database = DatabaseHandler(database.get_db_location(self.bot))
        self.reddit = RedditHandler(self.bot)

    def start(self):
        Logger.log('Starting bot as ' + self.bot['username'] + '...',
                   Color.GREEN)
        while True:
            try:
                self.check_for_commands()
                if self.run:
                    InboxHandler.read_inbox(self.database, self.reddit)
                    subscriptions = self.database.get_subscriptions()
                    Logger.log(str(len(subscriptions)) + ' Subs')
                    matches = MatchFinder.find_matches(subscriptions,
                                                       self.reddit,
                                                       self.database)
                    Logger.log(str(len(matches)) + ' Matches')
                    MatchHandler.send_messages(self.reddit, self.database,
                                               matches, self.bot)
                    self.database.purge_old_matches()
                    Logger.log(times.get_time_passed(self.start_time),
                               Color.YELLOW)
                SleepHandler.sleep(20)
            except KeyboardInterrupt:
                Logger.log('Keyboard Interrupt - Bot killed', Color.RED)
                exit()
            except:
                handle_crash(traceback.format_exc(),
                             self.bot,
                             message_dev=False,
                             reddit=self.reddit,
                             database=self.database)

    def check_for_commands(self):
        Logger.log('Checking for commands')
        commands = CommandHandler.get_commands(self.reddit, bot_username)
        if CommandHandler.PAUSE in commands:
            self.run = False
        if CommandHandler.RUN in commands:
            self.run = True
        if CommandHandler.KILL in commands:
            exit()
class AlertBot:
    def __init__(self):
        self.start_time = times.get_current_timestamp()
        self.run = True
        self.database = DatabaseHandler()
        self.reddit = RedditHandler()

    def start(self):
        Logger.log('Starting bot...', Color.GREEN)
        while True:
            try:
                self.check_for_commands()
                if self.run:
                    InboxHandler.read_inbox(self.database, self.reddit)
                    subscriptions = self.database.get_subscriptions()
                    Logger.log(str(len(subscriptions)) + ' Subs')
                    matches = MatchFinder.find_matches(subscriptions, self.reddit, self.database)
                    Logger.log(str(len(matches)) + ' Matches')
                    MatchHandler.send_messages(self.reddit, self.database, matches)
                    self.database.purge_old_matches()
                    Logger.log(times.get_time_passed(self.start_time), Color.YELLOW)
                SleepHandler.sleep(20)
            except KeyboardInterrupt:
                Logger.log('Keyboard Interrupt - Bot killed', Color.RED)
                exit()
            except:
                handle_crash(traceback.format_exc(), self.reddit, self.database, True)

    def check_for_commands(self):
        Logger.log('Checking for commands')
        commands = CommandHandler.get_commands(self.reddit)
        if CommandHandler.PAUSE in commands:
            self.run = False
        if CommandHandler.RUN in commands:
            self.run = True
        if CommandHandler.KILL in commands:
            exit()