import datetime import json import os import sys from multiprocessing.pool import ThreadPool import requests OWS_PRICING_URL = os.environ.get('OWS_PRICING_URL', 'https://qa-ows-pricing.theorchard.io') STORE_ID = os.environ.get('STORE_ID', '1') ALBUM_PRICING_FAMILY = 2 TRACK_PRICING_FAMILY = 3 PRODUCT_PRICING_URL = '{0}/pricing_family/{1}/product/{2}/store/{3}/pricing' results = [] def get_product_ids(): path = os.path.dirname(os.path.realpath(__file__)) + \ '/data/product_ids.json' with open(path, 'r') as input_file: return json.load(input_file) def get_product_pricing(product_id): print('About to get pricing result for product {0}'.format(str(product_id))) album_url = PRODUCT_PRICING_URL.format( OWS_PRICING_URL, ALBUM_PRICING_FAMILY, product_id, STORE_ID) album_response = requests.get(album_url) if album_response.status_code != 200: album_response.raise_for_status() track_url = PRODUCT_PRICING_URL.format( OWS_PRICING_URL, TRACK_PRICING_FAMILY, product_id, STORE_ID) track_response = requests.get(track_url) if track_response.status_code != 200: track_response.raise_for_status() album_result = album_response.json() album_result['product_id'] = product_id album_result['pricing_family_id'] = ALBUM_PRICING_FAMILY track_result = track_response.json() track_result['product_id'] = product_id track_result['pricing_family_id'] = TRACK_PRICING_FAMILY results.append(album_result) results.append(track_result) print('Got pricing result for product {0}'.format(str(product_id))) def write_results(results): file_name = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") path = os.path.dirname(os.path.realpath(__file__)) + \ '/' + file_name + '.json' with open(path, 'w') as output_file: json.dump(results, output_file) try: print('About to get product IDs') product_ids = get_product_ids() print('Got {0} product IDs'.format(len(product_ids))) print('About to get pricing results') with ThreadPool(32) as p: p.map(get_product_pricing, product_ids) print('Got pricing {0} pricing results'.format(len(results))) print('About to write results to file') write_results(results) print('Wrote results to file') sys.exit() except Exception as e: sys.exit(e)