Created
June 18, 2020 23:29
-
-
Save ShawnAzman/91e2d5ebde582e4e9b3b92bf85f83737 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gzip | |
import urllib2 | |
import hashlib | |
import base64 | |
import json | |
import requests | |
import StringIO | |
API_KEY = "<API_KEY>" | |
API_SECRET = "<API_SECRET>" | |
EXPIRES = "<SIGNATURE_EXPIRATION_DATE>" # E.g. "2020-07-01T00:00" | |
SHOES_PIPELINE_ID = 'PIPELINE_1_ID' | |
SHIRTS_PIPELINE_ID = 'PIPELINE_2_ID' | |
PANTS_PIPELINE_ID = 'PIPELINE_3_ID' | |
def compare_results(): | |
with open('Shoes', 'r') as shoes: | |
shoes_probs = [row.replace('"', '').strip().split(',') for row in shoes.readlines()[1:]] | |
combined_probabilities = {} | |
for user_id, conversion_prob in shoes_probs: | |
combined_probabilities[user_id] = [conversion_prob,] | |
with open('Shirts', 'r') as shirts: | |
shirts_prob = [row.replace('"', '').strip().split(',') for row in shrits.readlines()[1:]] | |
for user_id, conversion_prob in shirts_prob: | |
probability_list = combined_probabilities.get(user_id, ['NA']) | |
probability_list.append(conversion_prob) | |
combined_probabilities[user_id] = probability_list | |
with open('Pants', 'r') as pants: | |
pants_prob = [row.replace('"', '').strip().split(',') for row in pants.readlines()[1:]] | |
for user_id, conversion_prob in pants_prob: | |
probability_list = combined_probabilities.get(user_id, ['NA', 'NA']) | |
probability_list.append(conversion_prob) | |
combined_probabilities[user_id] = probability_list | |
with open('Combined.csv', 'w') as combined: | |
combined.write('user_id,shoes,shirts,pants,top_category\n') | |
for user, probability_list in combined_probabilities.iteritems(): | |
max_prob = probability_list.index(max(probability_list)) | |
if max_prob == 0: | |
top_category = "Shoes" | |
elif max_prob == 1: | |
top_category = "Shirts" | |
else: | |
top_category = 'Pants' | |
combined.write('%s,%s,%s\n' % (user, ','.join(probability_list), top_category)) | |
def download(url, filename): | |
response = urllib2.urlopen(url) | |
compressed_file = StringIO.StringIO(response.read()) | |
decompressed_file = gzip.GzipFile(fileobj=compressed_file) | |
with open(filename, 'w') as outfile: | |
outfile.write(decompressed_file.read()) | |
def generate_signature(secret, http_method, request_path, params = {}, body = None): | |
info_list = [ | |
secret, | |
http_method, | |
request_path, | |
'&'.join([key + '=' + value for key, value in sorted(params.iteritems())]), | |
body | |
] | |
string_to_sign = '\n'.join([item for item in info_list if item]) | |
if body == None: | |
string_to_sign += '\n' | |
signature = base64.b64encode(hashlib.sha256(string_to_sign).digest())[:43].rstrip('=') | |
return signature | |
def get_endpoint(request_path): | |
params = { | |
'api_key': API_KEY, | |
'expires': EXPIRES | |
} | |
signature = generate_signature(API_SECRET, 'GET', request_path, params) | |
url = 'http://api.vidora.com%s?api_key=%s&expires=%s&signature=%s' % (request_path, urllib2.quote(API_KEY), urllib2.quote(EXPIRES), urllib2.quote(signature)) | |
import pdb; pdb.set_trace() | |
response = requests.get(url) | |
return response.json() | |
def main(): | |
prediction_exports = get_endpoint('/v1/api/pipelines/%s/prediction_exports' % (SHOES_PIPELINE_ID)) | |
if prediction_exports: | |
export_id = prediction_exports[0]['id'] | |
export_data = get_endpoint('/v1/api/pipelines/%s/prediction_exports/%s' % (SHOES_PIPELINE_ID, export_id) | |
download_url = export_data['download_url'] | |
download(download_url, 'Shoes') | |
prediction_exports = get_endpoint('/v1/api/pipelines/%s/prediction_exports' % (SHIRTS_PIPELINE_ID)) | |
if prediction_exports: | |
export_id = prediction_exports[0]['id'] | |
export_data = get_endpoint('/v1/api/pipelines/%s/prediction_exports/%s' % (SHIRTS_PIPELINE_ID. export_id)) | |
download_url = export_data['download_url'] | |
download(download_url, 'Shirts') | |
prediction_exports = get_endpoint('/v1/api/pipelines/%s/prediction_exports' % (PANTS_PIPELINE_ID)) | |
if prediction_exports: | |
export_id = prediction_exports[0]['id'] | |
export_data = get_endpoint('/v1/api/pipelines/%s/prediction_exports/%s' % (PANTS_PIPELINE_ID. export_id)) | |
download_url = export_data['download_url'] | |
download(download_url, 'Pants') | |
compare_results() | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment