import os import json import boto3 import psycopg2 from psycopg2.extras import RealDictCursor import datetime import logging import requests import pathlib log = logging.getLogger() log.setLevel(logging.INFO) REGION = os.environ["AWS_REGION"] if "AWS_REGION" in os.environ else "eu-west-1" S3_BUCKET_DEVEL = "frontend-api-devel-filestore" def get_secret(secret_name): # Create a Secrets Manager client session = boto3.session.Session() client = session.client( service_name='secretsmanager', region_name=REGION ) get_secret_value_response = json.loads(client.get_secret_value(SecretId=secret_name)['SecretString']) return get_secret_value_response DB_CONN = {'fansifter': None, 'sandbox': None} def get_rds_connection_fansifter(): global DB_CONN if DB_CONN['fansifter'] is None: try: secret = get_secret('fansifter-rds') DB_CONN['fansifter'] = psycopg2.connect(host=secret['host'], port=secret['port'], database=secret['dbname'], user=secret['username'], password=secret['password'], cursor_factory=RealDictCursor) except Exception as e: log.exception(e) return DB_CONN['fansifter'] def get_rds_connection_sandbox(): global DB_CONN if DB_CONN['sandbox'] is None: try: secret = get_secret('sandbox_pass') DB_CONN['sandbox'] = psycopg2.connect(host='localhost', # secret['host'], port='5431', # secret['port'], database=secret['dbname'], user=secret['username'], password=secret['password'], cursor_factory=RealDictCursor) except Exception as e: log.exception(e) return DB_CONN['sandbox'] rds_connectors = {'fansifter': get_rds_connection_fansifter, 'sandbox': get_rds_connection_sandbox} def rds_query(connection_name, query, vars=None): with rds_connectors[connection_name]() as conn: with conn.cursor() as cur: cur.execute(query, vars=vars) return cur.fetchall() bucket_name = S3_BUCKET_DEVEL file_path = '/home/toomas/projects/test_data/' #file_prefix = file_path + 'af65a0ef98e7420800afdea2beb81059bde2bc59d797cc0851412ac41/enrichment/' + str(datetime.datetime.now()).replace(' ', 'T')[:-7] + '/' file_prefix = file_path + 'af65a0ef98e7420800afdea2beb81059bde2bc59d797cc0851412ac41/enrichment/2020-07-20T19:51:47/' pathlib.Path(file_prefix).mkdir(parents=True, exist_ok=True) piplKey = "7s7c5afv488mtqrv8jlklvd5" connection = 'fansifter' other_buckets = set() all_profiles = rds_query(connection, "select mc.fan_id, f.root_email from af65a0ef98e7420800afdea2beb81059bde2bc59d797cc0851412ac41.merch_clusters mc " "inner join af65a0ef98e7420800afdea2beb81059bde2bc59d797cc0851412ac41.fan_table f on f.fan_id=mc.fan_id") print(f"Found {len(all_profiles)} profiles") for fan_id, fan_email in [sch.values() for sch in all_profiles]: url = f"https://api.pipl.com/search/?email={fan_email}&key={piplKey}&top_match=true" out_name = f"{file_prefix}{fan_id}.json" print(url) if not pathlib.Path(out_name).exists(): res = requests.get(url) with open(out_name, "xt") as ff: ff.write(res.content.decode()) print(res.content)