Skip to content

Instantly share code, notes, and snippets.

@MrP123
Last active August 29, 2015 14:02
Show Gist options
  • Save MrP123/05197994ff76880cbd3e to your computer and use it in GitHub Desktop.
Save MrP123/05197994ff76880cbd3e to your computer and use it in GitHub Desktop.
A reddit (reddit.com) bot that gets free games of cheapshark.com and posts a link to a subreddit
import json
import urllib
import praw
from time import sleep
#All the necessary urls
jsonUrl = "http://www.cheapshark.com/api/1.0/deals?upperPrice=0.0001"
redirectPage = "http://www.cheapshark.com/redirect?dealID={id}"
#All available stores in an array for generating tags
STORES = ['Steam', 'Gamer\'s Gate', 'Green Man Gaming', 'Amazon', 'Game Stop', 'Game Fly', 'GoG', 'Origin', 'Get Games', 'Shiny Loot', 'Humble Store', 'Desura']
#Login and subreddit information
USERNAME = "username"
PASSWORD = "password"
USERAGENT = "useragent - should contain /u/yourRedditName as credit to reddit"
SUBREDDIT = "subreddit"
#global variables
data = None
link = None
title = None
subreddit = None
redditInstance = None
#logs into reddit account and gets the used subreddit
def login():
global redditInstance
redditInstance = praw.Reddit(user_agent=USERAGENT)
redditInstance.login(USERNAME, PASSWORD)
global subreddit
subreddit = redditInstance.get_subreddit(SUBREDDIT)
print "Logging in as " + USERNAME + ", and submitting to /r/" + SUBREDDIT
#gets a JSON object from cheapshark.com
def getJsonData():
response = urllib.urlopen(jsonUrl)
global data
data = json.loads(response.read())
jsonString = json.dumps(data, sort_keys=True, indent=4)
print "Getting JSON data from cheapshark.com"
#checks the JSON object
def checkJson(shouldPrint):
if len(data) <=0:
if shouldPrint:
print "No free games"
return False
else:
for i in range(0, len(data)):
if shouldPrint:
print "\n"
print "Title: ", data[i]['title']
print "Link to:", redirectPage.replace("{id}", data[i]['dealID'])
print "Store:", STORES[int(data[i]['storeID']) - 1]
print "Headline: ", "[" + STORES[int(data[i]['storeID']) - 1] + "]", "(Game)", data[i]['title']
print "\n"
global link
link = redirectPage.replace("{id}", data[i]['dealID'])
global title
title = "[" + STORES[int(data[i]['storeID']) - 1] + "](Game)" + data[i]['title']
print "Handling JSON data"
return True
#submits a link to a subreddit
def submitLink(subr, headline, url):
redditInstance.submit(subr, headline, text=None, url=url, captcha=None, save=False, send_replies=False)
print "Submitting link to " + subr
login()
running = True
shouldPrint = True
print "Main loop started - to stop press ctrl + c"
while running:
try:
getJsonData()
shouldSubmit = checkJson(shouldPrint)
if shouldSubmit:
try:
submitLink(subreddit, title, link)
except praw.errors.AlreadySubmitted, e:
print "Post already submitted"
continue
else:
print "Nothing to submit"
#sleeping 10min after each cycle
print "Sleeping for 10 min"
sleep(600)
#stop program by pressing ctrl + c
except KeyboardInterrupt:
print "Stopping program"
running = False
#catches any reddit API errors and sleeps for 30 sec and then retrys
except praw.errors.APIException, e:
print "\n"
print "[ERROR]:", e
print "sleeping 30 sec"
print "\n"
sleep(30)
#blindly handling any exception that gets thrown
except Exception, e:
print "\n"
print "[ERROR]:", e
print "blindly handling any error"
print "\n"
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment