Created
July 29, 2014 21:11
-
-
Save 1328/3759cc6932bca7dd3a6a to your computer and use it in GitHub Desktop.
comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Trollcheckbot 1.1a is a reddit bot written by (utter novice) /u/tingmakpuk. | |
| In theory, this bot will be summoned by a user responding to an exising comment using | |
| a trigger phrase. The bot will report on the user who posted the parent comment: | |
| Version 1.0: Replies with target username and comment karma. | |
| Version 1.1: Replies with target username and comment karma. Makes a judgement. | |
| Version 1.1a: Fixed signature and related bug | |
| WIP -- Version 2.0 will log the work and not duplicate the effort. | |
| """ | |
| #known bugs: | |
| #If trollcheckbot checks itself, it will pick up on its own reported username as a trigger | |
| #on the next cycle. | |
| #Not currently logging work, so duplicates effort, although reddit blocks dupl comments. | |
| #currently handles one sub at a time. | |
| #config: | |
| import praw | |
| import time #need for .sleep | |
| import datetime #to be used later to log | |
| user_agent = ("Troll Check Bot 1.1a by /u/tingmakpuk" | |
| "https://github.com/tingmakpuk/") | |
| me = 'trollcheckbot' | |
| r = praw.Reddit(user_agent=user_agent) | |
| r.login(me, '') | |
| runtime = 0 | |
| successtime = 0 | |
| # set lookups are a lot faster than list lookups | |
| # and order does not matter here | |
| # (speed doesn't either, but migth as well get used to it!) | |
| check_words = set(["trollcheckbot", "trollcheck"]) #Setting up for checkit | |
| subreddit = r.get_subreddit('pics') #test with 1 sub, then expand | |
| # let's kill those long lines | |
| signature = ("\n\n" | |
| "[Sourcecode](https://github.com/tingmakpuk/) |" | |
| "[Mods may unsubscribe here.]" | |
| "(http://www.reddit.com/message/compose/?to=tingmakpuk&" | |
| "subject=bot&message=%2Bunsub%20/r/%28subname%29)") | |
| """ | |
| Saving/crediting resources: | |
| Reddit 1 - http://praw.readthedocs.org/en/latest/pages | |
| /getting_started.html#breaking-down-redditor-karma-by-subreddit | |
| Reddit 2 - https://github.com/reddit/reddit/wiki/API | |
| Inspire 1 https://github.com/Damgaard/Reddit-Bots/blob/master/karma_breakdown.py | |
| Inspire 2 https://github.com/kaare8p/Dogecoin-tipper/blob/master/Dogecoin-tipper.py | |
| """ | |
| def get_comments(subreddit): | |
| ''' | |
| could use more functions generally, so here is an example of one | |
| that I would break out | |
| ''' | |
| try: | |
| print "Checking comments." #debug checkpoint | |
| comments = subreddit.get_comments(limit = 100) | |
| return comments | |
| except someerror: | |
| # only catch errors that you expect to catch | |
| while True: #checking for summons | |
| for post in get_comments(subreddit): | |
| # good use of any | |
| # but let's cut down on the extra variables | |
| checkit = any(string in post.body.lower() for string in check_words) | |
| if not checkit: | |
| # skip those without troll words | |
| # no need for another indentiation here | |
| continue | |
| print "Found one." #debug checkpoint | |
| summon_author = post.author #use later to create a more personalized response? | |
| parent_author = r.get_info(thing_id=post.parent_id).author | |
| str_parent = str(parent_author) | |
| print str_parent #debug checkpoint | |
| get_karma = parent_author.comment_karma | |
| # should use int instead of str otherwise the compares will not work | |
| str_karma = str(get_karma) | |
| print str_karma #debug checkpoint | |
| if get_karma > 200: | |
| judgement = "Not a professional troll." | |
| elif get_karma >= 0: | |
| judgement = "You make the call." | |
| else: | |
| judgement = "Don't feed the troll." | |
| # let's use the new formating style to make this line shorter | |
| comment_reply = '/u/{} has {} comment karma. {}'.format( | |
| str_parent, | |
| str_karma, | |
| judgement) | |
| print comment_reply #+ signature #debug checkpoint | |
| try: | |
| post.reply(comment_reply + signature) #pull for debug | |
| except someerror: | |
| # only catch those errors you expect | |
| # and only catch where you expect them to happen | |
| successtime += 1 | |
| time.sleep(15) | |
| print "Slept 15." #debug checkpoint | |
| runtime += 1 | |
| if runtime == 100: | |
| print "Success/Run: " + str(successtime) + "/" + str(runtime) | |
| break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment