Skip to content

Instantly share code, notes, and snippets.

@itochu0523
Created December 5, 2018 01:44
Show Gist options
  • Save itochu0523/e2096b39486615ff3a4050c5a060c191 to your computer and use it in GitHub Desktop.
Save itochu0523/e2096b39486615ff3a4050c5a060c191 to your computer and use it in GitHub Desktop.
#
# Usage: ttd.py <jobid>
#
# This program send ttd_id list with segment_name to The Trade Desk
# by thier APIs.
# Query result of <jobid> requires ttd_id and segment_name.
#
#
import os
import sys
import tdclient
import glob
import time
import csv
### TD Setting ###
apikey = 'TD_MASTER_APIKEY'
### TTD Setting ###
ad_id = 'xxxx'
secret = 'xxxxx'
endpoint = 'http://tok-data.adsrvr.org/data/advertiser'
### Config Parameter ###
lines_per_file = 1000 # Adjust this parameter to make body size of API within 4MB.
sleep_per_api = 5 # API call interval adjsutment between performance and stability.
# Functions
def get_ttdid_list():
jobid = sys.argv[1];
print('jobid')
print(jobid)
with tdclient.Client(apikey) as client:
# get job result, expecting ttd_id and segment_name columns
ids = client.job_result(jobid)
# write ids to local file
f = open('ids.csv', 'w')
for x in ids:
f.write(str(x[0]) + "," + str(x[1]) + "\n")
f.close()
def split_file():
in_file_name = 'ids.csv'
out_file_name_template = 'splitted_%d'
max_lines = lines_per_file
split_index = 1
line_index = 1
out_file = open(out_file_name_template % (split_index,), "w")
in_file = open(in_file_name)
line = in_file.readline()
while line:
if line_index > max_lines:
print("Starting file: %d" % split_index)
out_file.close()
split_index = split_index + 1
line_index = 1
out_file = open(out_file_name_template % (split_index,), "w")
out_file.write(line)
line_index = line_index + 1
line = in_file.readline()
out_file.close()
in_file.close()
def make_body(ids):
body = '{"AdvertiserId":"' + ad_id + '","Items":['
body_id = ''
for x in ids:
body_id += '{"TDID":"' + x[0] + '","Data":[{"Name":"' + x[1] + '"}]},'
body_id = body_id.rstrip(",")
body += body_id
body += ']}'
return body
def make_digest(message, key):
import hashlib
import hmac
import base64
key = bytes(key, 'UTF-8')
message = bytes(message, 'UTF-8')
digester = hmac.new(key, message, hashlib.sha1)
signature1 = digester.digest()
signature2 = base64.standard_b64encode(signature1)
return str(signature2, 'UTF-8')
def send_body(signature, body):
import requests
headers = {
'TtdSignature': signature,
'Content-Length': str(len(body))
}
response = requests.post(endpoint, headers=headers, data=body)
print(response)
print(response.text)
def remove_glob(pathname, recursive=True):
for p in glob.glob(pathname, recursive=recursive):
if os.path.isfile(p):
os.remove(p)
# Main
args = sys.argv
if(len(args) != 2):
print("Usage: python {} <jobid>".format(args[0]))
quit()
get_ttdid_list()
split_file()
files = glob.glob('splitted_*')
for file in files:
csv_file = open(file, "r", encoding="utf_8", errors="", newline="\n" )
f = csv.reader(csv_file, delimiter=",", doublequote=True, lineterminator="\n", quotechar='"', skipinitialspace=True)
ids = []
for x in f:
ids.append(x)
csv_file.close
#print(ids)
body = make_body(ids)
#print('body')
#print(body)
print('length')
print(str(len(body)))
signature = make_digest(body, secret)
print('signature')
print(signature)
send_body(signature, body)
time.sleep(sleep_per_api)
remove_glob('splitted_*')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment