#!/usr/bin/env python
 
import string
import random
import hashlib
import sys, os
import urllib, urllib2, cookielib
import json
import urlparse
 
os.chdir(os.path.dirname(os.path.realpath(__file__)))
 
jackpot_ids = [line.rstrip('\n') for line in open('jackpot_ids.txt')]
 
def random_string(length=20):
        return ''.join(random.choice(string.ascii_letters + string.digits) for x in range(length))
 
def sha512(string):
                return hashlib.sha512(string).hexdigest()
 
class Hasher:
        secretKey = '4ATreS384Epepa2aHAFr' # Insert secretKey, eg. master salt here
       
        def __init__(self):
                self.salt = random_string()
       
        def base_hash(self):
                return self.secretKey + self.salt
       
        def hash_string(self, string):
                return sha512(self.base_hash() + string)
       
        def hash_list(self, list):
                return self.hash_string(''.join(map(str, list)))
 
def get_facebook(id):
        response = urllib2.urlopen('http://graph.facebook.com/' + str(id))
        body = response.read()
        data = json.loads(body)
        return data
 
def is_int(a):
        try:
                dummy = int(a)
                return str(dummy) == str(a)
        except:
                return False
 
class User:
        def __init__(self, fb_id, fb_name=None):
                if fb_name == None or not is_int(fb_id):
                        fb = get_facebook(fb_id)
                        fb_id = fb['id']
                        #fb_name = fb['name']
               
                self.fb_id = fb_id
                self.fb_name = fb_name
               
                self.cookiejar = cookielib.CookieJar()
                opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookiejar))
                #opener = urllib2.build_opener(urllib2.ProxyHandler({'http': 'http://127.0.0.1:8080/'}), urllib2.HTTPCookieProcessor(self.cookiejar))
                urllib2.install_opener(opener)
               
                self.login()
               
        def send_request(self, url, data, hash_list, get=False):
                hasher = Hasher()
                token = hasher.hash_list(hash_list)
               
                data.update({
                        'salt': hasher.salt,
                        'token': token
                })
               
                query = urllib.urlencode(data)
               
                if get:
                        url = url + '?' + query
                        query = None
               
                response = urllib2.urlopen(url, query)
                body = response.read()
                data = json.loads(body)
               
                if type(data) is list and len(data) is 1:
                        data = data[0]
               
                return data
               
        def login(self):
                url = 'http://mcdonalds.kiloo.com/index.php/customer_v3/login'
                data = {
                        'customer_fid': self.fb_id,
                        'customer_name': self.fb_name
                }
                hash_list = [self.fb_id]
               
                response = self.send_request(url, data, hash_list)
                self.user_id = response['customer_id']
               
        def coins(self):
                url = 'http://mcdonalds.kiloo.com/index.php/customer_v3/get/collected_coins/'
                data = {
                        'customer_id': self.user_id,
                        'customer_fid': self.fb_id
                }
                hash_list = [self.user_id, self.fb_id]
               
                response = self.send_request(url, data, hash_list)
                return response['collected_coins']
       
        def collect_coin(self, id, location='nowhere'):
                url = 'http://mcdonalds.kiloo.com/index.php/customer_v3/collect_coin'
               
                data = {
                        'customer_id': self.user_id,
                        'collection_type': id,
                        'gps': location,
                        'customer_fid': self.fb_id
                }
                hash_list = [self.user_id, id, location, self.fb_id]
               
                response = self.send_request(url, data, hash_list)
                return response
       
        def history(self):
                url = 'http://mcdonalds.kiloo.com/index.php/customer_v3/get/history/'
                data = {
                        'customer_id': self.user_id,
                        'customer_fid': self.fb_id
                }
                hash_list = [self.user_id, self.fb_id]
               
                response = self.send_request(url, data, hash_list)
                return response['data']
               
        def get_jackpot_id(self, jackpot_id=None):
                if jackpot_id != None:
                        self.jackpot_id = jackpot_id
                else:
                        self.jackpot_id = getattr(self, 'jackpot_id', random.choice(jackpot_ids))
                       
                return self.jackpot_id
       
        def jackpot_spins(self, jackpot_id=None):
                jackpot_id = self.get_jackpot_id(jackpot_id)
               
                url = 'http://mcdonalds.kiloo.com/index.php/customer_v3/jackpot_status'
                data = {
                        'customer_id': self.user_id,
                        'jackpot_id': jackpot_id
                }
                hash_list = [self.user_id]
               
                response = self.send_request(url, data, hash_list, True)
               
                if response['status'] == 201:
                        return 0
               
                return response['available_spins']
       
        def spin_jackpot(self, jackpot_id=None, location='nowhere'):
                jackpot_id = self.get_jackpot_id(jackpot_id)
               
                url = 'http://mcdonalds.kiloo.com/index.php/customer_v3/jackpot'
               
                data = {
                        'customer_id': self.user_id,
                        'jackpot_id': jackpot_id,
                        'location': location
                }
                hash_list = [self.user_id, jackpot_id, location]
               
                response = self.send_request(url, data, hash_list)
                return response
       
        def buy(self, product_id, location='nowhere'):
                url = 'http://mcdonalds.kiloo.com/index.php/customer_v3/buy_product'
               
                data = {
                        'customer_id': self.user_id,
                        'product_id': product_id,
                        'gps': location,
                        'customer_fid': self.fb_id,
                        'qr_size': '14'
                }
                hash_list = [self.user_id, product_id, location, self.fb_id]
               
                response = self.send_request(url, data, hash_list)
                return response['response']
 
def product_price(product_id):
        prices = {
                1:  15,
                2:  10,
                3:  10,
                4:  10,
                5:  25,
                7:  20,
                8:  10,
                9:  20,
                10: 20
        }
        return prices[product_id]