def botStuff():
    time.sleep(30)

    reddit, classifier, codeVTextClassifier, nb_title_classifier, tdm, userNames, postHistory, phbArcPaths = main.startupBot(
    )
    try:
        main.runBot(reddit,
                    classifier,
                    codeVTextClassifier,
                    nb_title_classifier,
                    tdm,
                    userNames,
                    postHistory,
                    phbArcPaths=phbArcPaths)
    except KeyboardInterrupt:
        print("Concluding Program")
        logging.debug("Keyboard Interrupt: Ending Program")
    except:
        logging.error("\n" + traceback.format_exc())
        fl = open("ERRORLOG.log", 'a')
        fl.write("\n" + "*" * 50 + "\n" + traceback.format_exc())
        fl.close()
        print(traceback.format_exc())
        trc = traceback.format_exc().strip().split("\n")[-1]
        msg = "Program exited on Error:\n" + str(trc)
        if len(msg) > 133:
            msg = msg[:130] + '...'
            print(msg)
        print(msg)
        textSupervision.send_update(msg)
    return
Exemplo n.º 2
0
def xbasicUserClassify(reddit, user, userNames, submission, suggestedTime,
                       antiSpamList):

    # Need to classify a user here
    # Only post if
    DayLimit = 700
    timeDelt = datetime.timedelta(days=DayLimit)
    accountAge = datetime.datetime.utcnow() - user.created_utc

    antiSpamList = xpopOldSpammers(
        antiSpamList, ageLimitHours=12)  # Should stop growing dict memory leak

    # Don't bother commenting if I've talked to the user before
    if str(user.name) in userNames:
        logging.info("\tI've already commented on a post by " + str(user.name))
        msg = "\tI've already commented on a post by " + str(user.name)
        print(msg)
        if submission.id not in antiSpamList:
            antiSpamList[submission.id] = submission.created_utc
            msg = msg.strip(
            ) + "\n\nPost in Question: " + botHelperFunctions.shortenRedditURL(
                submission.url)
            textSupervision.send_update(msg)
        return False, [], antiSpamList

    if accountAge > timeDelt:
        logging.debug('\t' + str(user.name))
        # User is old enough to know better
        # This is emperically false but I need some limits
        # Actually in practice the bot nearly never comments because of this
        logging.debug(
            str(accountAge) + " User Should Know Better, They're old enough")
        #print  '\t', "USER SHOULD KNOW BETTER"
        #return False, []

    subs, directedOthersToLearn, postsInLearningSubs = xgetSubsUsersInteractsIn(
        reddit, user)
    if 'learnpython' in subs:
        # Probably should check if user has posted in the sub more recently than current post
        logging.info("User " + str(user.name) +
                     " has posted in r/learnpython before")

    if directedOthersToLearn:
        logging.info("User " + str(user.name) +
                     " has directed others to r/learnpython")
        return False, [], antiSpamList

    return True, postsInLearningSubs, antiSpamList
Exemplo n.º 3
0
    reddit, classifier, codeVTextClassifier, nb_submission_classifier, tdm, userNames, postHistory, phbArcPaths = startupBot(
    )
    try:
        runBot(reddit,
               classifier,
               codeVTextClassifier,
               nb_submission_classifier,
               tdm,
               userNames,
               postHistory,
               phbArcPaths=phbArcPaths,
               quietMode=quietMode)
    except KeyboardInterrupt:
        print("Concluding Program")
        logging.debug("Keyboard Interrupt: Ending Program")
    except:
        logging.error("\n" + traceback.format_exc())
        fl = open("ERRORLOG.log", 'a')
        fl.write("\n" + "*" * 50 + "\n" + traceback.format_exc())
        fl.close()
        print(traceback.format_exc())
        trc = traceback.format_exc().strip().split("\n")[-1]
        msg = "Program exited on Error:\n" + str(trc)
        if len(msg) > 133:
            msg = msg[:130] + '...'
            print(msg)
        print(msg)
        if not quietMode:
            textSupervision.send_update(msg)
Exemplo n.º 4
0
def handleInbox(reddit,
                codeVTextClassifier,
                phbArcPaths,
                setOfPosts={},
                unreadCount=None,
                sendText=True,
                quietMode=False):
    # Mark all messages as read after notification has been sent
    rawInboxMessages = archiveAndUpdateReddit.checkForMessages(reddit)
    sendText = True

    # Handle Summoning:
    inboxMessages = []
    summonMsgs = []
    summonedIDs = []
    if len(rawInboxMessages) == 1:
        logging.debug("Checking Inbox.. 1 message")
    else:
        logging.debug("Checking Inbox.. " + str(len(rawInboxMessages)) +
                      " message(s)")

    for msg in rawInboxMessages:
        summoned, command = checkForSummons(msg)
        if summoned != None:
            # Reply
            # Pass message off to summoning processor,
            # Act on summons,
            # Reply to summoner
            # mark summoning as read
            summonMsgs.append(msg)
            summonedIDs.append(summoned)
            actOnSummons(reddit,
                         msg,
                         command,
                         codeVTextClassifier,
                         quietMode,
                         setOfPosts,
                         phbArcPaths=phbArcPaths)
        else:
            inboxMessages.append(msg)

    # Commented out during testing:
    if len(summonedIDs) > 0:
        logging.debug(
            str(len(summonedIDs)) + " summoning messages, clearing them..")
        # Not sure if I should clear summons during testing..
        #if not quietMode:
        #    archiveAndUpdateReddit.markSummonsAsReadMessages(reddit, msgIDs =summonedIDs)
        archiveAndUpdateReddit.markSummonsAsReadMessages(reddit,
                                                         msgIDs=summonedIDs)

    # Handle startup unread count
    if unreadCount == None:
        return len(inboxMessages)

    if len(inboxMessages) > unreadCount:
        commentReplies = 0
        directMessages = 0
        userNameMentions = 0
        for msg in inboxMessages:
            if msg.was_comment:
                if msg.subject == "username mention":
                    userNameMentions += 1
                else:
                    commentReplies += 1
            else:
                # Check to see if it was a karma summons
                directMessages += 1
        newMsgCount = len(inboxMessages) - unreadCount

        if sendText:
            if newMsgCount > 1:
                txtmsg = "You have " + str(
                    newMsgCount) + " new unread messages."
            else:
                txtmsg = "You have 1 new unread message."
            txtmsg += "\n\nUnread Replies to your Comments: " + str(
                commentReplies)
            txtmsg += "\nUnread Direct Messages: " + str(directMessages)
            txtmsg += "\nUnread Username Mentions: " + str(userNameMentions)
            textSupervision.send_update(txtmsg)
            logging.info("Text Message sent: \n" + txtmsg)

    unreadCount = len(inboxMessages)

    return unreadCount
        except KeyboardInterrupt:
            print("KBI")
            logging.debug("Keyboard Interrupt: Ending Program from rpiManager")
            kbi = True
            break
        except:
            logging.error("\n" + traceback.format_exc())
            fl = open("ERRORLOG.log", 'a')
            fl.write("\n" + "*" * 50 + "\n" + traceback.format_exc())
            fl.close()
            print(traceback.format_exc())
            trc = traceback.format_exc().strip().split("\n")[-1]
            msg = "Program exited on Error:\n" + str(trc)
            if len(msg) > 133:
                msg = msg[:130] + '...'
                print(msg)
            print(msg)
            #textSupervision.send_update(msg)
        resetTries += 1
        time.sleep(sleepDurationS * resetTries)
    logging.debug(
        "Exited the program, ending rpiManager if __name__=='__main__.")

    logging.debug("Leaving power button alive")
    if not kbi:
        textSupervision.send_update(
            "Exited the program, ending rpiManager\nLeaving power button alive"
        )
    powerButton = restartButtonThread()
    powerButton.start()