import datetime import json import os import sys import requests OWS_PRODUCT_URL = os.environ.get('OWS_PRODUCT_URL') OWS_TRACK_URL = os.environ.get('OWS_TRACK_URL') OWS_ASSETS_URL = os.environ.get('OWS_ASSETS_URL') OWS_PRICING_URL = os.environ.get('OWS_PRICING_URL') OWS_CARVEOUTS_URL = os.environ.get('OWS_CARVEOUTS_URL') RESULT_FILE = os.path.join(os.path.dirname(__file__), 'result.json') LOG_FILE = os.path.join(os.path.dirname(__file__), 'logs.txt') LOG_INFO = 'INFO' LOG_ERROR = 'ERROR' PRODUCT_IDS = [ 2540889, 2541210, # 2540888, # 2540885, # 2540887, # 2540886, # 1896508, # 2603113, # 1889565, # 2602276 ] STORES = [ { 'name': 'iTunes', 'store_id': 1 }, { 'name': 'Amazon', 'store_id': 187 }, { 'name': 'Google Music', 'store_id': 496 }, { 'name': '7 Digital', 'store_id': 12 }, { 'name': 'Xbox', 'store_id': 287 } ] PRICING_FAMILIES = [ { 'name': 'Album', 'pricing_family_id': 2 }, { 'name': 'Tracks', 'pricing_family_id': 3 } ] def log(log_type, log_msg, final_log=False): with open(LOG_FILE, 'a') as log_file: ts = datetime.datetime.now().isoformat() msg = '[{}] [{}] {}\n'.format(ts, log_type, log_msg) log_file.write(msg) print(msg) if final_log: log_file.write('----------\n') def get_product_result(product_id): url = '{}/product/{}'.format(OWS_PRODUCT_URL, product_id) result = requests.get(url) if result.status_code != 200: result.raise_for_status() return result.json() def get_tracks_result(product_id): url = '{}/product/{}/tracks'.format(OWS_TRACK_URL, product_id) result = requests.get(url) if result.status_code != 200: result.raise_for_status() return result.json() def get_assets_result(product_id): url = '{}/asset/product/{}'.format(OWS_ASSETS_URL, product_id) result = requests.get(url) if result.status_code != 200: result.raise_for_status() return result.json() def get_pricing_result(product_id): pricing_result = {} for store in STORES: for pricing_family in PRICING_FAMILIES: url = '{}/pricing_family/{}/product/{}/store/{}/pricing'.format( OWS_PRICING_URL, pricing_family['pricing_family_id'], product_id, store['store_id']) result = requests.get(url) if result.status_code != 200: result.raise_for_status() if store['name'] not in pricing_result: pricing_result[store['name']] = {} pricing_result[store['name']][pricing_family['name']] = result.json() return pricing_result def get_carveouts_result(upc): url = '{}/carveout/release/{}'.format(OWS_CARVEOUTS_URL, upc) result = requests.get(url) if result.status_code != 200: result.raise_for_status() return result.json() def write_result(result): with open(RESULT_FILE, 'w') as result_file: json.dump(result, result_file) try: log(LOG_INFO, 'Starting') result = {} for product_id in PRODUCT_IDS: log(LOG_INFO, 'About to process product {}'.format(product_id)) product_result = get_product_result(product_id) tracks_result = get_tracks_result(product_id) assets_result = get_assets_result(product_id) pricing_result = get_pricing_result(product_id) carveouts_result = get_carveouts_result(product_result['upc']) result[product_id] = { 'product': product_result, 'tracks': tracks_result, 'assets': assets_result, 'pricing': pricing_result, 'carveouts': carveouts_result } log(LOG_INFO, 'Done processing product {}'.format(product_id)) write_result(result) log(LOG_INFO, 'Done', True) sys.exit() except Exception as e: log(LOG_ERROR, 'Unexpected error: {}'.format(e), True) sys.exit(e)