Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Uploads CokeRewards points in a simple text file. Useful if you have a large number and want to make sure you're always uploading something every day so they don't expire!
import time, re, logging
from optparse import OptionParser
import os.path
import subprocess
from mechanize import Browser
from mechanize._form import ControlNotFoundError
USER_NAME = ''
PASSWORD = ''
def submit_code(browser, code):
browser.select_form(nr = 0)
browser.form['code'] = code
resp = browser.submit()
if 'whirl' in resp.get_data():
logging.warning('Already submitted code')
return False
browser.select_form(nr = 0)
try:
browser.form.find_control('selectedBrandId')
except ControlNotFoundError:
logging.warning('Unknown error')
return False
browser.form.find_control('selectedBrandId').get('0').selected = True
resp = browser.submit()
logging.warning('Sucessfully submitted!')
return True
if __name__ == "__main__":
infname = '../rewardcodes.txt'
logfname = '../rewards.log'
wrongfname = '../wrong_rewardcodes.txt'
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(message)s',
filename=logfname,
filemode='a')
num_tries = 5
parser = OptionParser()
(options, args) = parser.parse_args()
USER_NAME = args[0]
PASSWORD = args[1]
logging.warning('Reading Reward Codes')
with open(infname) as handle:
code_list = [x.strip() for x in handle if len(x) > 2]
logging.warning('There are %i codes left!' % len(code_list))
num_submit = max(min(len(code_list)/10, 5),1)
code_gen = iter(code_list)
browser = Browser()
try:
logging.warning('Logging in!')
browser.open('http://www.mycokerewards.com/mobile/signin.do')
browser.select_form(nr = 0)
browser.form['emailAddress'] = USER_NAME
browser.form['passwordText'] = PASSWORD
browser.submit()
not_working = []
for _ in range(num_submit):
for _ in range(num_tries):
browser.open('http://www.mycokerewards.com/mobile/enterCode.do')
next_code = code_gen.next()
if submit_code(browser, next_code):
logging.warning('Code accepted')
break
else:
not_working.append(next_code)
codes_left = list(code_gen)
logging.warning('Outputting Original File ... %i codes left!' % len(codes_left))
with open(infname, 'w') as handle:
handle.write('\n'.join(codes_left))
logging.warning('Outputting Bad File ... %i incorrect codes' % len(not_working))
if len(not_working) > 0:
with open(wrongfname, 'a') as handle:
handle.write('\n'.join(not_working))
except StopIteration:
logging.warning('No more codes left!!!!')
except:
logging.warning('Caught Error!!!')
logging.warning('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.