Last active
February 13, 2020 20:02
-
-
Save tbell83/40814cec872066389f23bc44a0a33208 to your computer and use it in GitHub Desktop.
import fairplay rds to dynamodb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/local/bin/python3 | |
| import sys | |
| import datetime | |
| import boto3 | |
| import pymysql.cursors | |
| PROPERTY = sys.argv[1] | |
| ENVIRON = sys.argv[2] | |
| DB_DOMAIN = 'drama9.com' if ENVIRON != 'prod' else f'wbdn{PROPERTY}.net' | |
| SQL_HOST = f'{PROPERTY}-{ENVIRON}-reader.rds.{DB_DOMAIN}' | |
| DYNAMO_TABLE = f'{PROPERTY}-{ENVIRON}-fairplayksm-video' | |
| TABLES = { | |
| DYNAMO_TABLE: [], | |
| f'{DYNAMO_TABLE}log': [], | |
| } | |
| BOTO_SESSION = boto3.Session(region_name='us-east-1') | |
| SSM_CLIENT = BOTO_SESSION.client('ssm') | |
| DYNAMO_CLIENT = BOTO_SESSION.client('dynamodb') | |
| SQL_PWD = SSM_CLIENT.get_parameter( | |
| Name=f'/{PROPERTY}-{ENVIRON}/www/db_password', | |
| WithDecryption=True, | |
| )['Parameter']['Value'] | |
| CONNECTION = pymysql.connect( | |
| host=SQL_HOST, | |
| db='fairplay', | |
| user='dramauser', | |
| password=SQL_PWD, | |
| ) | |
| with CONNECTION.cursor() as cursor: | |
| table_property_name = 'boomerang' if PROPERTY == 'bo' else PROPERTY | |
| sql = f'select content_id, contentkey, contentiv from video where propertyname="{table_property_name}";' | |
| cursor.execute(sql) | |
| results = cursor.fetchall() | |
| for row in results: | |
| content_id, contentkey, contentiv = row | |
| TABLES[DYNAMO_TABLE].append( | |
| { | |
| 'content_id': content_id, | |
| 'contentkey': contentkey, | |
| 'contentiv': contentiv, | |
| } | |
| ) | |
| with CONNECTION.cursor() as cursor: | |
| table_property_name = 'boomerang' if PROPERTY == 'bo' else PROPERTY | |
| sql = f'select id, content_id, logdate, contentkey, contentiv from videolog where propertyname="{table_property_name}"' | |
| cursor.execute(sql) | |
| results = cursor.fetchall() | |
| for row in results: | |
| id, content_id, logdate, contentkey, contentiv = row | |
| TABLES[f'{DYNAMO_TABLE}log'].append( | |
| { | |
| 'id': id, | |
| 'content_id': content_id, | |
| 'logdate': logdate, | |
| 'contentkey': contentkey, | |
| 'contentiv': contentiv, | |
| } | |
| ) | |
| for table_name, table in TABLES.items(): | |
| for row in table: | |
| item = {} | |
| for k, v in row.items(): | |
| if type(v) == datetime.datetime: | |
| v = v.strftime('%Y-%m-%dT%H:%M:%SZ') | |
| item[k] = {'S': v} | |
| DYNAMO_CLIENT.put_item( | |
| TableName=table_name, | |
| Item=item, | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment