"""Functionality for preparing the data and writig the CSV reports.""" import csv from ioda import const CSV_FIELDS = [ const.FILENAME, const.CUSTOM_ID, const.ASSET_ID, const.ISRC, const.ADD_ASSET_LABELS, const.UPC, const.GRID, const.SONG_TITLE, const.ARTIST, const.ALBUM, const.GENRE, const.LABEL, const.ORIGINAL_RELEASE_DATE, const.OWNERSHIP, const.MATCH_POLICY, const.AUDIOSWAP ] YOUTUBE_CSV_FIELDS = [ const.ASSET_ID, const.UPC, const.ISRC, const.CUSTOM_ID ] def create_csv_row(row): """Map row from dataset into dict for writing a CSV row. Args: row (dict): row from dataset Returns: dict: dictionary to be written to CSV """ csv_row = { const.FILENAME: const.EMPTY, const.CUSTOM_ID: get_custom_id(row), const.ASSET_ID: str(row[const.ASSET_ID]).strip(), const.ISRC: row.get(const.ORCHARD_ISRC, row[const.ISRC]), const.ADD_ASSET_LABELS: const.EMPTY, const.UPC: row.get(const.ORCHARD_UPC, row[const.UPC]), const.GRID: row[const.GRID], const.SONG_TITLE: row[const.SONG_TITLE], const.ARTIST: row[const.ARTIST], const.ALBUM: row[const.ALBUM], const.GENRE: const.EMPTY, const.LABEL: row[const.LABEL], const.ORIGINAL_RELEASE_DATE: const.EMPTY, const.OWNERSHIP: const.EMPTY, const.MATCH_POLICY: const.EMPTY, const.AUDIOSWAP: const.EMPTY } return csv_row def create_csv_row_from_asset(asset): """Map row from YouTube asset metadata into dict for writing a CSV row. Args: asset (dict): asset metadata from YouTube API Returns: dict: dictionary to be written to CSV """ csv_row = { const.ASSET_ID: asset[const.ID], const.UPC: asset[const.METADATA][const.UPC], const.ISRC: asset[const.METADATA][const.ISRC], const.CUSTOM_ID: asset[const.METADATA][const.YT_CUSTOM_ID], } return csv_row def save_csv( data, file_name, fields=CSV_FIELDS, row_mapper=create_csv_row, write_header=True): """Save CSV file. Args: data (list): data to write file_name (str): name of the file fields (list): list of fields in CSV row_mapper (func): function that should generate a dict for CSV row from source data write_header (bool): should CSV contain header row """ with open(file_name, 'w', encoding='utf8') as csv_file: writer = csv.DictWriter(csv_file, fields, lineterminator='\n') if write_header: writer.writeheader() for row in data: csv_row = row_mapper(row) writer.writerow(csv_row) def get_custom_id(row): """Get valid custom_id. If existing custom_id contains '_', then use it, otherwise construct the custom_custom using columns from joined IODA mapping tables. Args: row (dict): Row from dataset Returns: str: custom_id """ custom_id = row[const.CUSTOM_ID] if const.SEPARATOR not in custom_id: custom_id = '{}_{}_{}'.format( row[const.ORCHARD_UPC], row[const.ORCHARD_ISRC], row[const.ORCHARD_TUID]) return custom_id