import requests import zlib import logging import json_log_formatter import os import subprocess import snowflake.connector formatter = json_log_formatter.JSONFormatter() json_handler = logging.FileHandler(filename='my-log.json') json_handler.setFormatter(formatter) logger = logging.getLogger('my_json') logger.addHandler(json_handler) logger.setLevel(logging.INFO) def request_token(oauth_url, client_id, client_secret): """Request oauth token from Spotify. Returns: str: oauth token. """ post_fields = { 'grant_type': 'client_credentials', 'client_id': client_id, 'client_secret': client_secret} res = requests.post(oauth_url, data=post_fields) if res.status_code != 200: raise requests.RequestException(response=res) return res.json()['access_token'] logger.info('Get Spotify Access Token') token = request_token('https://ws.spotify.com/oauth/token', 'sony', 'SECUREDPWD') payload = {'oauth_token': token} r = requests.get('https://ws.spotify.com/analytics/api/' \ 'sonybmgmusicentertainmentv2/streams' \ '/2019/03/02' \ '/US', params=payload, stream=True) if r.status_code != 200: raise requests.RequestException(response=r) outFilePath = '/neo4j-data/decomp/testfile.csv' chunk_size = 1024*1024 logger.info('Starting US File Download') d = zlib.decompressobj(zlib.MAX_WBITS|32) with open(outFilePath, 'wb') as fd: for chunk in r.iter_content(chunk_size=chunk_size): outstr = d.decompress(chunk) fd.write(outstr) logger.info('Finished Decompressed File Download') logger.info('Total file size: %s', os.path.getsize(outFilePath)) subprocess.call(['split', '-l', '1000000', '--filter=/usr/bin/pigz - --fast > $FILE.gz', '/data/decomp/testfile.csv', '/data/decomp/file_parts/part_decomp_']) logger.info('Done splitting files') subprocess.call(['rm', '-f', '/data/decomp/testfile.csv']) logger.info('Removed downloaded file') subprocess.call(['/home/centos/.local/bin/aws', 's3', 'cp', '--recursive', '/data/decomp/file_parts/', 's3://dev-bucket/test/']) logger.info('Uploaded all files to S3') subprocess.call(['rm', '-f', '/data/decomp/file_parts/*']) logger.info('Removed part files') # Load data ctx = snowflake.connector.connect( user='myron_admin', password='SUPERAWESOMEPWD', account='orchard' ) cs = ctx.cursor() try: sql = 'COPY INTO DB.SCHEMA.SPOTIFY_LOAD_TEST \ FROM s3://dev-bucket/test/ \ CREDENTIALS=( \ AWS_KEY_ID=\'KEYKEYKEY\' \ AWS_SECRET_KEY=\'SECRETSECRETSECRET\') \ ON_ERROR=CONTINUE \ FILE_FORMAT=( \ TYPE=\'CSV\' \ COMPRESSION=GZIP \ FIELD_DELIMITER = NONE \ ESCAPE = \'0x11\' \ FIELD_OPTIONALLY_ENCLOSED_BY=\'"\' \ ESCAPE_UNENCLOSED_FIELD=\'0x11\' \ )' logger.info("SELECTING WAREHOUSE") ctx.cursor().execute("USE warehouse AD_HOC_MGMT_REPORTS") logger.info("LOADING DATA") cs.execute(sql) logger.info("DATA LOAD COMPLETE") finally: cs.close() ctx.close() exit()