""" API data snapshot script. This quickly snapshots the API output for all whitelisted UPCs. """ from __future__ import print_function import os.path import sys # create import path for ETL source files. Needed to load utils. _here = os.path.dirname(os.path.realpath(__file__)) _etl_path = os.path.join(os.path.dirname(_here), 'etls') sys.path.append(_etl_path) from datetime import datetime import json import os from urllib.request import Request, urlopen from flows.constant import FILM_WHITELIST API_URL_FORMAT = 'https://workstation.theorchard.com/api/film-transparency/' \ 'release/{upc}/profit-loss' def ask(prompt): """Backwards compatible input prompt. Args: prompt (str): terminal prompt message. Returns: str: user provided string value. """ if sys.version_info[0] == 3: return input(prompt) return raw_input(prompt) def get_token(): """Prompt to have user submit a GRASS token.""" print(""" Need to first get the GRASS session token: 1. Log into workstation (any account will do). 2. Go to https://workstation.theorchard.com/grasssession 3. Copy the token value, without quotes """) return ask('Input token value: ') def get_api_data(upc, token): """Grab data from ows-film-transparency API. Args: upc (str): target film's UPC. token (str): GRASS token. Returns: dict: JSON decoded data object from the API. """ url = API_URL_FORMAT.format(upc=upc) print('Getting data for UPC:', upc) request = Request(url=url, headers={'session': token}) with urlopen(request) as response: return json.load(response) def save_data(upc, data): """Save data to a timestamped folder. Args: upc (str): target film's UPC. data (dict): native python data object to save as JSON. """ datestamp = datetime.now().strftime('%Y-%m-%d') os.makedirs(datestamp, exist_ok=True) filename = '{upc}.json'.format(upc=upc) target = os.path.join(datestamp, filename) print('Saving data to', target) with open(target, 'wt+', encoding='utf-8') as fh: json.dump(data, fh, indent=4) if __name__ == '__main__': token = get_token() for upcs in FILM_WHITELIST.values(): for upc in upcs: output = get_api_data(upc, token) save_data(upc, output)