""" Takes the given csv bulk data and transforms it to json for processing by the existing DB PR https://github.com/theorchard/database/pull/9252 """ import csv import json import sys # added this to avoid an error when processing a large amount of data field_size_limit = sys.maxsize while True: try: csv.field_size_limit(field_size_limit) break except OverflowError: field_size_limit = int(field_size_limit / 10) def read_file(filename): with open(filename) as f: reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE) next(reader) for line in reader: yield line def file_details_from_acr_filename(filename): parts = filename.split('_') assert len(parts) == 6 return { 'upc': parts[0], 'cd': parts[1], 'track_id': parts[2], 'tuid': parts[3], 'isrc': parts[4], 'identifier_timestamp': int(parts[5]) } def file_details_to_dd_filename(details): return f"{details['upc']}_{details['cd']}_{details['track_id']}" def main(): input_filename = sys.argv[1] output_filename = sys.argv[2] tuid_to_latest_delivery = {} # Find the latest delivery time for each tuid for input in read_file(input_filename): file_list_str = input[1] file_list = file_list_str.strip(']""[').split('","') for asset in file_list: details = file_details_from_acr_filename(asset) current_timestamp = tuid_to_latest_delivery.get(details['tuid']) if current_timestamp is not None: if current_timestamp >= details['identifier_timestamp']: continue assert current_timestamp != details['identifier_timestamp'] tuid_to_latest_delivery[details['tuid']] = details['identifier_timestamp'] # Write the file out with only the latest deliveries, and with DD-style filenames with open(output_filename, 'w') as output_file: for input in read_file(input_filename): file_list_str = input[1] file_list = file_list_str.strip(']""[').split('","') output = { 'acr_id': input[0], 'assets': [], } for asset in file_list: details = file_details_from_acr_filename(asset) if tuid_to_latest_delivery[details['tuid']] != details['identifier_timestamp']: continue output['assets'].append({ 'filename': file_details_to_dd_filename(details), 'tuid': details['tuid'], #'md5': asset['md5'], }) output_file.write(json.dumps(output) + '\n') if __name__ == "__main__": main()