import argparse import csv import os import pymysql from sys import exit as sysexit from configparser import ConfigParser config = ConfigParser() if not config.read('youtube_ingestion.ini'): sysexit("map_assets_to_tracks.ini could not be found.") source_table = config.get('config', 'source_table') host = config.get('config', 'host') user = config.get('config', 'user') password = config.get('config', 'password') database = config.get('config', 'database') infile = config.get('config', 'infile') start_row = config.getint('config', 'start_row') conn = pymysql.connect(host=host, user=user, password=password, db=database, charset='utf8', local_infile=True, cursorclass=pymysql.cursors.DictCursor) check_table_query = """SHOW TABLES LIKE %s;""" create_table_query = """CREATE TABLE `{}` ( `id` BIGINT(20) NOT NULL AUTO_INCREMENT, `custom_id` VARCHAR(1000) DEFAULT NULL, `upc` VARCHAR(255) DEFAULT NULL, `isrc` VARCHAR(255) DEFAULT NULL, `derived_tuid` VARCHAR(50) DEFAULT NULL, `reason` VARCHAR(255) DEFAULT NULL, `reported_ownership` VARCHAR(1000) DEFAULT NULL, `reported_conflicts` VARCHAR(1000) DEFAULT NULL, `derived_ownership` VARCHAR(1000) DEFAULT NULL, `derived_conflicts` VARCHAR(1000) DEFAULT NULL, PRIMARY KEY (`id`) ) DEFAULT CHARSET=utf8mb4;""" load_file_query = """ LOAD DATA LOCAL INFILE %s INTO TABLE {} CHARACTER SET utf8mb4 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' IGNORE %s LINES ( @col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9, @col10, @col11, @col12, @col13, @col14, @col15, @col16, @col17, @col18, @col19, @col20, @col21, @col22, @col23, @col24 ) SET custom_id = @col6, isrc = @col7, upc = @col9, reported_ownership = @col19, reported_conflicts = @col20; """ cursor = conn.cursor() # Check for table existence cursor.execute(check_table_query, source_table) check_table = cursor.fetchone() if not check_table: cursor.execute(create_table_query.format(source_table)) print('Table `{}` created.'.format(source_table)) print( 'Data from {} is being loaded into table `{}`.'.format( infile, source_table)) cursor.execute(load_file_query.format(source_table), (infile, start_row))