# -*- coding: utf-8 -*-
import MySQLdb
import time
import json
from hashlib import md5
import urllib2
from threading import Timer
def connMysql():
#Notified idfa this time.
output = open('/Users/tuyou/Desktop/added_idfa.txt', 'w')
#Notified idfa already.
input = open('/Users/tuyou/Desktop/successfull_idfa.txt', 'r')
notifyedIdfaList = input.readlines()
#connection db
conn = MySQLdb.connect(host='10.3.1.254',user='tuyoogame',passwd='tuyoogame',db='ads',port=3306)
sqlstr = 'select iosappid, mac, idfa, userid, clkip, clktime, spname, note FROM clicks WHERE spname="quzhuan" and userid is null'
cur = conn.cursor()
cur.execute(sqlstr)
result = cur.fetchall()
#result = cur.fetchmany(5)
print 'Total has {num} lines, alreade notify {num_notify}'.format(num = len(result), num_notify = len(notifyedIdfaList))
notifyresult = ['0', '0', '0']
failedList = []
for item in result:
if item[2] + '\n' not in notifyedIdfaList:
notifyQuzhuanUserCreated(item, output, notifyresult, failedList)
time.sleep(1)
else:
print 'already notified.'
notifyresult[2] = str(int(notifyresult[2]) + 1)
input.close()
output.close()
addNewIdfaToFile()
addFailIdfaToFile(failedList)
print 'result: ', notifyresult
def addNewIdfaToFile():
input = open('/Users/tuyou/Desktop/added_idfa.txt', 'r')
output = open('/Users/tuyou/Desktop/successfull_idfa.txt', 'a')
#這裡不用加換行符,因為是從檔案中讀出來的
idfaList = input.readlines()
for item in idfaList:
output.write(item)
print 'add success.'
input.close()
output.close()
def addFailIdfaToFile(failedList):
errPut = open('/Users/tuyou/Desktop/failed_idfa.txt', 'a')
#這裡需要加換行符
for item in failedList:
errPut.write(item + '\n')
errPut.close()
def notifyQuzhuanUserCreated(result, fileP, notifyresult, failedList):
iosappid, mac, idfa, _, clkip, clktime, sp, note = result
params = {}
params['iosappid'] = iosappid
params['clkip'] = clkip
if idfa:
params['idfa'] = idfa
if mac:
params['mac'] = mac
params['clktime'] = clktime
acttime = int(time.time())
params['acttime'] = acttime
params['ip'] = 'null'
params['note'] = note
ret = user_created(params)
if ret:
print 'Successed! idfa = ', idfa
notifyresult[0] = str(int(notifyresult[0]) + 1)
fileP.write(idfa + '\n')
else:
notifyresult[1] = str(int(notifyresult[1]) + 1)
print 'Failed! idfa = ', idfa
failedList.append(idfa)
def user_created(params):
iosappid = params['iosappid']
acttime = params['acttime']
noteStr = params['note']
noteJson = json.loads(noteStr)
notifyUrl = noteJson['callback']
ip = params.get('ip', 'null')
userId = params.get('userId', 'null')
signStr = str(iosappid) + str(acttime) + str(ip) + str('123456')
signature = md5(signStr).hexdigest()
requestParamAdd = '&wifiMac={wifiMac}&sign={sign}&acttime={acttime}&ip={ip}&appVersion={appVersion}&appUserId={appUserId}'
requestParamAdd = requestParamAdd.format(wifiMac = 'null', sign = signature, acttime = acttime, ip = ip, appVersion = 'null', appUserId = userId)
requestUrl = 'http://' + notifyUrl + requestParamAdd
print 'Auser_activated requestUrl:', requestUrl
request = urllib2.Request(requestUrl)
response = urllib2.urlopen(request)
data1 = response.read()
ponse = json.loads(data1)
print ponse
if int(ponse['code']) != 0:
return False
else:
return True
connMysql()