示例#1
0
 def __init__(self):
     # self.needs_alert = [[developer['username'], 0]]
     self.needs_alert = []
     self.errors = []
     self.invalid_users = []
     self.db = DatabaseHandler(accounts['bot'])
     self.reddit = RedditHandler(accounts['developer'])
示例#2
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()
    def __init__(self):
        self.subject = 'Alert_Bot - wiping database - need to resubscribe'
        self.needs_alert = [['XdrummerXboy', 0]]
        self.needs_alert = []
        self.errors = []
        self.invalid_users = []

        self.db = self.connect_to_db()
        self.reddit = self.connect_to_reddit()
        self.db_helper = DatabaseHandler()
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()
 def __init__(self):
     self.start_time = times.get_current_timestamp()
     self.run = True
     self.database = DatabaseHandler()
     self.reddit = RedditHandler()
示例#6
0
 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)
class Notifications:

    def connect_to_reddit(self):
        user_agent = 'tylerbrockett - developer'
        reddit = praw.Reddit(user_agent=user_agent)
        reddit.login(accountinfo.developerusername, accountinfo.developerpassword, disable_warning=True)
        return reddit

    def connect_to_db(self):
        connection = sqlite3.connect(definitions.DB_LOCATION)
        cursor = connection.cursor()
        cursor.execute(database.CREATE_TABLE_SUBSCRIPTIONS)
        cursor.execute(database.CREATE_TABLE_MATCHES)
        cursor.execute(database.CREATE_TABLE_ALERTS)
        cursor.execute(database.CREATE_TABLE_ALL_MATCHES)
        cursor.execute(database.CREATE_TABLE_ALL_USERS)
        return connection

    def __init__(self):
        self.subject = 'Alert_Bot - wiping database - need to resubscribe'
        self.needs_alert = [['XdrummerXboy', 0]]
        self.needs_alert = []
        self.errors = []
        self.invalid_users = []

        self.db = self.connect_to_db()
        self.reddit = self.connect_to_reddit()
        self.db_helper = DatabaseHandler()

    def send_message(self, username):
        subs = self.db_helper.get_subscriptions_by_user(username)
        Logger.log('\n\n' + str(len(subs)) + ' subs for user')
        sub_text = inbox.format_subscription_list(subs, 'Your Old Subscriptions')
        message = inbox.compose_greeting(username) + NOTIFICATION + "\n\n" + sub_text
        self.reddit.send_message(username, self.subject, message)

    def run_alerts(self):
        # if selected_users is empty, send to all, otherwise just send to selected_users
        if not self.needs_alert:
            self.needs_alert = self.db.cursor().execute(database.GET_USERNAMES_THAT_NEED_ALERT).fetchall()
            Logger.log(str(len(self.needs_alert)) + '  USERS NEED ALERT')
            time.sleep(20)
        num = len(self.needs_alert)
        i = 0
        for row in self.needs_alert:
            username = row[0]
            entry = (username, 1)  # 1 == True
            try:
                self.send_message(username)
                self.db.cursor().execute(database.INSERT_ROW_ALERTS, entry)
                self.db.commit()
                Logger.log('message sent to ' + username)
                i += 1
            except InvalidUser:
                self.invalid_users.append(username)
                Logger.log('Invalid User --> ' + username)
            except:
                self.errors.append(username)
                Logger.log('ALERT FAILED: ' + username + '\n\t \n\t \n' + traceback.format_exc())
                self.db.rollback()
                self.db.close()
                exit()

        Logger.log('\n\nInvalid Users')
        for user in self.invalid_users:
            Logger.log(user)

    def finish_up(self):
        self.db.cursor().execute(database.DROP_TABLE_ALERTS)
        self.db.commit()
        self.db.close()

    def handle_crash(self, stacktrace):
        print(stacktrace)
        self.reddit.send_message(accountinfo.developerusername, 'Bot Alerts Crashed', stacktrace)
        self.db.close()
        exit()
示例#8
0
class Notifications:
    def __init__(self):
        # self.needs_alert = [[developer['username'], 0]]
        self.needs_alert = []
        self.errors = []
        self.invalid_users = []
        self.db = DatabaseHandler(accounts['bot'])
        self.reddit = RedditHandler(accounts['developer'])

    def reset(self):
        reset = False
        while not reset:
            self.reddit.reset()
            self.db.reset()
            reset = True

    def run_alerts(self):
        # if selected_users is empty, send to all, otherwise just send to selected_users
        if not self.needs_alert:
            self.needs_alert = self.db.get_redditors_needing_notification()
            Logger.log(str(len(self.needs_alert)) + '  USERS NEED ALERT')
            time.sleep(20)
        i = 0
        for row in self.needs_alert:
            username = row[0]
            try:
                self.reddit.send_message(username,
                                         Notifications.MESSAGE_SUBJECT,
                                         Notifications.MESSAGE_BODY)
                self.db.insert_into_notifications(username, 1)
                Logger.log('message sent to ' + username, Color.CYAN)
                i += 1
            except:
                self.errors.append(username)
                Logger.log(
                    'ALERT FAILED: ' + username + '\n\t \n\t \n' +
                    traceback.format_exc(), Color.RED)
                exit()

        Logger.log('\n\nSent to ' + str(i) + ' / ' +
                   str(len(self.needs_alert)))
        Logger.log('\n\nExceptions')
        for user in self.invalid_users:
            Logger.log(user)

    def finish_up(self):
        self.db.drop_table_notifications()
        self.db.disconnect()

    MESSAGE_SUBJECT = 'Alert_Bot - Message from developer'
    MESSAGE_BODY = \
        'Hi everyone,' \
        '\t \n\t \n' \
        'Just wanted to give you an update on a couple things.' \
        '\t \n\t \n' \
        'First off, last night I finished updating the bot to use a newer version of the PRAW API for the bot, which ' \
        'was a complete re-write on their end, so many things changed. With this updated API, reddit permalinks ' \
        'changed from the format "https://reddit.com/r/subreddit/blah" to "/r/subreddit/blah". The bot uses the ' \
        'permalink to see if you\'ve already been notified of the post, so to the bot it appeared that no one had ' \
        'received alerts for their subscriptions yet. This means you all got duplicate alerts. I apologize for that, ' \
        'but at least it\'s working "as it should". I was puzzled for a while last night (more like this morning) as ' \
        'to why that was happening, but after a few hours of sleep and a clear mind I had a light bulb moment' \
        '\t \n\t \n' \
        'Secondly, earlier this month I made some changes to the subscription lexer (how the bot parses the messages ' \
        'you all send it). In this process, I removed the comma from the list of reserved tokens by accident, which ' \
        'means it was treated just like a normal character. This means messages like "subscribe -title GTX, 1070" ' \
        'would literally check for the comma in between too (i.e. it would check for exactly "GTX, 1070" in the ' \
        'title). Unfortunately, I didn\'t realize that until yesterday, a couple weeks after the bug was introduced. ' \
        'I only found that out after trying to create a subscription to two subreddits within one subscription ' \
        'yesterday, and it threw some weird exceptions. Luckily I was able to trace it back to the root cause ' \
        'though. That bug "only" affected probably 5 of you (still way more than I\'d have liked...) who used that ' \
        'feature in the last two weeks or so. Unfortunately, that means that you could have missed out on awesome ' \
        'deals during the holiday season, so again I\'m very sorry. I went through the bot\'s messages and manually ' \
        'checked to see who it affected, and updated the database accordingly. I\'m pretty positive I got them all, ' \
        'but if you\'re at all concerned, feel free to re-subscribe to any subscription that has a comma for any ' \
        'parameter.' \
        '\t \n\t \n' \
        'As always, thanks for using the bot everyone, and thanks for your patience! If you have ' \
        'any questions feel free to ask. Happy holidays!' \
        '\t \n\t \n' + \
        \
        '\n\t \n\t \n' \
        '-/u/' + accounts['developer']['username'] + \
        '\n\t \n\t \n' + \
        accountinfo.bot_subreddit + ' | [Bot Code](https://github.com/tylerbrockett/Alert-Bot-Reddit)\n'