public
Last active

contest_mode_bot

  • Download Gist
bot.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#!/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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.