import argparse import csv import os import requests def parse_args(): parser = argparse.ArgumentParser() parser.add_argument( '--filepath', help='The relative path from the script to the CSV file', required=True ) parser.add_argument( '--activity_type', help='The activity type (e.g. spike_detector or trending_tracks)', required=True, ) parser.add_argument( '--account_type', help='The account type (vendor or subaccount)', required=True ) parser.add_argument('--user_type', help='The user type (alw or oa)', required=True) parser.add_argument( '--action_type', help='The action type (subscribe or unsubscribe)', required=True ) parser.add_argument('--env', help='The environment (qa or prod)', default='prod') return parser.parse_args() def read_subscriptions_from_csv(filepath): path_from_script = os.path.join(os.path.dirname(__file__), filepath) subscriptions = [] with open(path_from_script) as csvfile: reader = csv.DictReader(csvfile) for row in reader: user_id = row.get('user_id') account_id = row.get('account_id') user_email = row.get('user_email') country_group = row.get('country_group') if user_id and account_id: subscriptions.append({'user_id': user_id, 'account_id': account_id}) if user_email and country_group: subscriptions.append( { 'user_email': user_email.replace(' ', ''), 'country_group': country_group.replace(' ', ''), } ) return subscriptions def process_subscriptions(subscriptions, activity_type, account_type, user_type, action_type, env): with open('subscription-logs.txt', 'w') as log_file: log_file.write('{0} - {1}\n'.format(action_type, activity_type)) url = 'https://{0}-ows-notifications.theorchard.io/{1}'.format(env, action_type) for item in subscriptions: data = {} headers = {} if activity_type == 'trending_tracks': data['user_feed_name'] = 'user_email_notifications' data['feed_name'] = 'orchard_trending_tracks_{0}'.format(item['country_group']) data['feed_id'] = 'orchard' data['user_feed_id'] = item['user_email'] headers['Orchard-User-Id'] = 'oa:179' log_file.write( 'User email: {0} - Feed name: {1}\n'.format( data['user_feed_id'], data['feed_name'] ) ) else: data['user_feed_name'] = 'user_email_notifications' data['feed_name'] = 'label_{0}'.format(activity_type) data['feed_id'] = '{0}_{1}'.format(account_type, item['account_id']) headers['Orchard-User-Id'] = '{0}:{1}'.format(user_type, item['user_id']) headers['Grass-Account-Type'] = account_type headers['Grass-Account-Id'] = item['account_id'] log_file.write( 'User id: {0} - Feed id: {1} - Feed name: {2}\n'.format( headers['Orchard-User-Id'], data['feed_id'], data['feed_name'] ) ) result = requests.post(url, json=data, headers=headers) if result.status_code != 200: log_file.write('An error occured\n') raise Exception(result.json()) try: print('Started') args = parse_args() print('Parsed args') subscriptions = read_subscriptions_from_csv(args.filepath) print('Read subscriptions from file') process_subscriptions( subscriptions, args.activity_type, args.account_type, args.user_type, args.action_type, args.env, ) print('{0}d {1} users.'.format(args.action_type.capitalize(), len(subscriptions))) print('Done') except Exception as e: print('Error during execution', e)