Skip to content

Instantly share code, notes, and snippets.

@nikolak
Created March 9, 2013 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikolak/5124921 to your computer and use it in GitHub Desktop.
Save nikolak/5124921 to your computer and use it in GitHub Desktop.
contest_mode_bot
#!/usr/bin/env python
# Copyright (c) 2013 Nikola Kovacevic <nikolak@outlook.com>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import requests
import json
import time
import os
##################### Data to modify ##########################################
username = 'bot_username'
password = 'bot_password'
post_source="http://www.reddit.com/r/askreddit/new/" + ".json"
bot_user_agent="wub_wub's awesome bot" #write something more descriptive here
simulate=True #won't change anything, just prints out what it would do
refresh_timeout=60 #How often to check for new thread and see if any need status change
contest_time =120 #time in seconds that thread will be in contest mode
###############################################################################
indexed_threads={}
user_pass_dict = {'user': username,
'passwd': password,
'api_type': 'json',}
headers = {'user-agent': bot_user_agent, }
client = requests.session(headers=headers)
client.post(r'http://www.reddit.com/api/login', data=user_pass_dict)
def get_modhash():
"""I'm not sure if new modhash is needed on every change,
so it might be better to put this outside function and get hash
just on startup
"""
mod=client.get("http://www.reddit.com/api/me.json")
j = json.loads(mod.text)
return j['data']['modhash']
def update_db(thread_id,new_status):
indexed_threads[thread_id]={'id':thread_id,'status':new_status,'time':time.time()}
print indexed_threads[thread_id]
with open("threads_file",'w') as out_file:
out_file.write(json.dumps(indexed_threads))
def change_status(thing_name,status=True):
if simulate:
print 'Simulation: setting {}\'s contest mode to {}'.format(thing_name,status)
update_db(thing_name,status)
time.sleep(2)
else:
contest_data={"id":thing_name,
"state":status,
"uh":get_modhash(),
"r":post_source.split('/r/')[4]}
if len(contest_data["uh"])<10:
return None
else:
client.post(r'http://www.reddit.com/api/set_contest_mode', data=contest_data)
update_db(thing_name,status)
time.sleep(2)
def get_new_posts():
posts=client.get(post_source)
threads=json.loads(posts.text)
for thread in threads['data']['children']:
if thread['data']['name'] not in indexed_threads:
change_status(thread['data']['name'],status=True)
else:
'Thread {} already in database'.format(thread['data']['name'])
def disable_contest():
print 'checking for threads to disable'
for item in indexed_threads:
if str(indexed_threads[item]['status'])!='False' and time.time()-float(indexed_threads[item]['time'])>=contest_time:
change_status(indexed_threads[item]['id'],status=False)
if __name__ == '__main__':
while True:
with open('threads_file') as data_file:
indexed_threads = json.load(data_file)
get_new_posts()
disable_contest()
time.sleep(refresh_timeout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment