Sep/080
Automatic My Coke Rewards Code Entry
Until recently I had a large backlog of My Coke Rewards points. In addition to all the codes I collected myself, I also received quite a few from my friends and family (Thanks!). The problem is that the site only allows you to redeem 10 codes per day, and I had a hard time remembering to enter them every day.
To help, I created a Ruby script for automatically processing codes. Every day, it reads 10 codes from a text file, then submits them to mycokerewards.com and logs the result. That way, I could just add all my codes to a file and it would take care of the rest.
I’ve provided the script below, as well as instructions for configuring it on a Mac (should be similar on Unix/Linux). If you’re using Windows, you can do the same thing by downloading Ruby and setting up a scheduled task. This isn’t the most user-friendly process, so a little development and/or command line experience would be helpful, but if you don’t have that you should be able to work it out by stepping through the instructions. Good luck and happy redeeming!
Here’s the code
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 | require 'xmlrpc/client' require 'logger' class McrSender @@SERVICE_URL = "https://secure.mycokerewards.com/xmlrpc" def initialize(codeFile, logFile, readCount, username, password) @codeFile = codeFile @logFile = logFile @readCount = readCount @username = username @password = password @logger = Logger.new(@logFile) end def send_codes i = 1 # read file into an array lines = File.readlines(@codeFile) lineCount = lines.length if(lineCount > 0 and lines[0].strip.length > 0) # login if(login == true) @logger.info "Successfully logged in." puts lineCount # process items and remove them from the array i = 0 while (i < @readCount && i < lineCount) code = lines[0].chomp send_code(code) lines.delete_at(0) i += 1 end # write array back to file File.open(@codeFile, "w") do |file| file.puts(lines.join()) end else @logger.error "Could not login with username '#{@username}' and password '#{@password}'." end else @logger.info "No codes found in code file (or the first line is empty). Exiting." end end private def send_code(code) args = {'emailAddress' => @username, 'password' => @password, 'screenName' => ' ', 'capCode' => code, 'VERSION' => '3.0'} service = XMLRPC::Client.new2(@@SERVICE_URL) result = service.call('points.enterCode', args) if(result[0]['ENTER_CODE_RESULT'] == "true") earned = result[0]['POINTS_EARNED'] @logger.info "Successfully entered code '#{code}' Points earned '#{earned}'. " else message = result[0]['MESSAGES'] @logger.error "Error entering code '#{code}'. Error message: '#{message}'." end end def login args = {'emailAddress' => @username, 'password' => @password, 'screenName' => ' ', 'VERSION' => '3.0'} service = XMLRPC::Client.new2(@@SERVICE_URL) result = service.call('points.pointsBalance', args) result[0]['LOGIN_RESULT'] == "true" end end sender = McrSender.new("codes.txt", "log.txt", 10, "YOUR_EMAIL", "YOUR_PASSWORD") sender.send_codes |
How to install
-
Make sure Ruby is installed on your Mac. (It is by default in Leopard). You can open terminal and type the following to check. It will return the Ruby version if it’s installed.
ruby -v
- Download the zip file containing the script, code file and log file.
- Extract the archive, the move the McrSender folder to the desired location. I’ll use /Users/doug for the remaining instructions. Replace /Users/doug with your own path.
- In the bottom two lines of McrSender.rb, change “codes.txt” to the full path to your codes.txt file, “log.txt” to the full path to your log.txt file, “YOUR_EMAIL” to the email address you use for My Coke Rewards, and “YOUR_PASSWORD” to the password you use for My Coke Rewards. (NOTE: All of these values should be surrounded in double quotes)
- Add your My Coke Rewards Codes to the codes.txt file. There should be one code per line.
-
Open terminal then type the following commands to change permissions on the Ruby script to make it executable and on the codes.txt and log.txt files to make them readable and writable.
cd /Users/doug chmod 755 McrSender.rb chmod 666 codes.txt chmod 666 log.txt
-
Create a cron job to run the script every day. Again, in Terminal type:
EDITOR=nano crontab -e
Your cron file will open up in the nano command line text editor. Add the following line to run the script at 6:00 am every morning:
0 6 * * * ruby /Users/dwburns/scripts/McrSender/McrSender.rb
(Again, change the path to your own path).
You can test it the first time by running (something like) the following in Terminal:
/Users/doug/McrSender/McrSender.rb