import argparse import datetime import json import os import time from spotify_api import SpotifyAPI spotify_api_licensors = ['theorchard', 'sme'] spotify_api_credentials = {} for licensor in spotify_api_licensors: spotify_api_credentials[licensor] = { 'client_id': os.environ.get( 'SPOTIFY_API_CLIENT_ID_{}'.format(licensor.upper())), 'client_secret': os.environ.get( 'SPOTIFY_API_CLIENT_SECRET_{}'.format(licensor.upper())), 'licensor': os.environ.get( 'SPOTIFY_API_LICENSOR_{}'.format(licensor.upper())), 'version': os.environ.get( 'SPOTIFY_API_VERSION_{}'.format(licensor.upper()), 'v2') } reports = ['streams', 'sub_30_sec_streams'] def main(date, once=False): currect_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M') for licensor in spotify_api_licensors: spotify_api = SpotifyAPI( spotify_api_credentials[licensor]['client_id'], spotify_api_credentials[licensor]['client_secret'], spotify_api_credentials[licensor]['licensor'], spotify_api_credentials[licensor]['version']) for report_name in reports: info = {} expected_count_files = spotify_api.get_countries(report_name, date) info[report_name] = { 'time': currect_time, 'len': len(expected_count_files), 'expected_count_files': expected_count_files} if not once: with open('{}_{}.json'.format(licensor, date), 'a') as file: file.write(json.dumps(info) + '\n') print( currect_time, licensor, report_name, len(expected_count_files)) if __name__ == '__main__': #date = '2019-04-07' parser = argparse.ArgumentParser(description='Date Range Flow Exec') parser.add_argument( 'date', help='Date string. f.e. 2019-04-07', default='2019-04-07', ) parser.add_argument( '--once', action='store_true', help='Run only one time') args = parser.parse_args() while True: main(args.date, args.once) if args.once: break time.sleep(60*5)