Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ZacSweers
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ZacSweers/8877826 to your computer and use it in GitHub Desktop.
Save ZacSweers/8877826 to your computer and use it in GitHub Desktop.
Python Bot Presentation
# http://goo.gl/sxbh65
# Presentbot.py
import praw
import pickle
import time
# log in
# reading login info from a file, it should be username \n password
with open("login.properties", "r") as loginFile:
login_info = loginFile.readlines()
login_info[0] = login_info[0].replace('\n', '')
login_info[1] = login_info[1].replace('\n', '')
# Set up praw
r = praw.Reddit('trollbot')
r.login(login_info[0], login_info[1])
# User to troll
USER="pandanomic"
# Message
# print "Sending message"
# r.send_message(USER, 'Test?', 'sup bro')
# print "Message sent"
# Get submissions from subreddit
subreddit = r.get_subreddit('soccer')
submissions = subreddit.get_hot(limit=10)
for submission in submissions:
print submission.title
# get top post
top_submission = next(subreddit.get_hot(limit=10))
print top_submission.title
print len(top_submission.comments)
# Comment on top post
new_comment = top_submission.add_comment('test')
# Reply to top comment of top post
new_comment2 = top_submission.comments[0].reply('test')
# Delete stuff
new_comment.delete()
new_comment2.delete()
# URL Submission
new_submission = r.submit('reddit_api_test', 'Test', url='http://google.com')
new_submission.delete()
# Self submission
new_submission = r.submit('reddit_api_test', 'Test', text='This is a self post')
new_submission.delete()
# User stuff
user = r.get_redditor(USER)
print user.comment_karma
for comment in user.get_comments(limit=10):
print comment.body
comment.upvote()
# Caching
already_done = dict()
subreddit = r.get_subreddit('aww')
for post in subreddit.get_new(limit=10):
if post.id not in already_done:
# Do whatever you want with the post
# Cache it
print 'New post'
already_done[post.id] = post.url
else:
print 'Already done'
CACHE_FILE = "testcache"
# Save cache
with open(CACHE_FILE, 'w+') as file_save:
pickle.dump(already_done, file_save)
# Load cache
if os.path.isfile(cache_file):
with open("cache_file", 'r+') as file_load:
already_done_copy = pickle.load(file_load)
# Good thing to do is extract this stuff out to methods
# while true, time.sleep(1800) # this is seconds
# link to spursgifsbot
# https://github.com/pandanomic/SpursGifs_xposterbot
# We can also do this with facebook's api using pip install facebook-sdk
# link to gist
# https://gist.github.com/pandanomic/8577232
# Say you moderate a group, and want to automate some price checking
import facebook
access_token = "blah" # https://developers.facebook.com/tools/explorer
group_id = "blah" # 458528044205531
posts_query = "SELECT post_id, message FROM stream WHERE source_id=" + group_id + " LIMIT 50"
graph = facebook.GraphAPI(access_token)
posts = graph.fql(query=posts_query)
already_done2 = []
for post in posts:
message = post['message']
postid = post['post_id']
if postid not in already_done2 and '$' not in message:
# OFF WITH THEIR HEADS!
# ...just kidding, we're going to post a comment
graph.put_object(postid, "comments", message="Bro you didn't mention pricing")
# and cache
already_done2.append(postid)
# link to mod bot
# https://github.com/pandanomic/FB_Mod_Bot
# Git and Heroku time
# Could use cron jobs or other things, but difficult if you have your own laptop
# Automation is lovely
# Same logic applies, but we need to use heroku's tools
# Link to gist, Q/A time?
# https://gist.github.com/pandanomic/8595628
# git init
# touch README.md
# touch .gitignore
# # Stuff we'll need, different for various languages
# requirements.txt
# git add .
# git commit -m 'Yay adding stuff'
# # Github stuff
# # Make a github account and repo (private or public)
# git remote add origin <git@......>
# git push -u origin master
# # Git has some commandline tools you can install
# # Set up your heroku account
# heroku create
# git remote -v
# heroku git:remote -a <the new name>
# git push heroku master
# heroku addons:add memcachedcloud:25
# heroku addons:add scheduler
# # Set up scheduler
# heroku run python
#
#
# We have a problem now though. This pickle stuff won't work here, so we need to use Heroku's stuff instead
# running_on_heroku = True
# # Cache stuff
# # Add python-binary-memcached==0.21 to your requirements.txt
# $ heroku run python 1 ↵
# Running `python` attached to terminal... up, run.9520
# Python 2.7.5 (default, May 17 2013, 06:45:09)
# [GCC 4.4.3] on linux2
# Type "help", "copyright", "credits" or "license" for more information.
# >>> import bmemcached
# >>> import os
# >>> mc = bmemcached.Client(os.environ.get('MEMCACHEDCLOUD_SERVERS').split(','), os.environ.get('MEMCACHEDCLOUD_USERNAME'), os.environ.get('MEMCACHEDCLOUD_PASSWORD'))
# >>> new_dict = {'example': 'yay'}
# >>> new_list = [1, 2, 3, 4, 5]
# >>> new_dict['list_test'] = new_list
# >>> mc.set('dict', new_dict)
# True
# >>> mc.set('list', new_list)
# True
# >>> print mc.get('dict')
# {'example': 'yay', 'list_test': [1, 2, 3, 4, 5]}
# >>> print mc.get('list')
# [1, 2, 3, 4, 5]
# >>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment