"""Create show family and backfill show family id for each feed and group multiple feeds. Also, handle backfilling feed type for private-rss feed. Run this in dev with command PYTHONPATH=. env/bin/python3.8 dev/backfill_show_family_id_and_group_multiple_feeds.py -- -env dev """ import argparse import csv import logging import os import sys from podcast.connectors import mysql from podcast.models import podcast as podcast_model from podcast.models import show_family as show_family_model from podcast.utils import api_utils log = logging.getLogger('main') log.setLevel(logging.DEBUG) fmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') sh = logging.StreamHandler(sys.stdout) sh.setFormatter(fmt) log.addHandler(sh) users = { 'dev': '366', 'qa': '1298', 'prod': '88' } request_headers = None PRIVATE_RSS = 'private-rss' def backfill_show_family_id_and_group_multiple_feeds(filename, user_id): """Create show family, backfill show family id for each feed, and group multiple feeds. Args: filename (str): The filename to access the csv file. user_id (str): user id of the user based on env. """ if not filename: raise Exception('File with name: backfillShowFamilyIdAndGroupFeeds.csv not found.') log.info(f'Reading file: {filename}.\n') with open(filename) as csv_file: prev_show_family_title = None show_family_id = None error_count = 0 errored_podcast_ids = [] reader = csv.DictReader(csv_file) for row in reader: try: show_family_title = row['Show Family Title'] podcast_id = int(row['Show ID']) podcast_name = row['Feed Name'] feed_type = row['Feed Type'] print( f'\nRunning for podcast: "{podcast_name}" having id: {podcast_id} ' f'and feed type: "{feed_type}" for show_family: "{show_family_title}."' ) with mysql.pod_db_session() as session: if prev_show_family_title != show_family_title: podcast = podcast_model.get_podcast_by_id(podcast_id) created_show_family = show_family_model.create_show_family( { 'network_id': podcast['network_id'], 'title': show_family_title, 'created_by': user_id, 'updated_by': user_id }, session ) show_family_id = created_show_family['id'] print(f'Created show_family with title: "{show_family_title}" having id: {show_family_id}') podcast_update_data = { 'show_family_id': show_family_id } if feed_type == PRIVATE_RSS: podcast_update_data['feed_type'] = PRIVATE_RSS podcast_model.update_podcast( podcast_id, podcast_update_data, session ) print(f'Updated podcast: {podcast_id} with show_family_id: {show_family_id}') except Exception as e: errored_podcast_ids.append(podcast_id) error_count += 1 log.info( f'Got an exception for podcast: "{podcast_name}" having id: {podcast_id}\n' f'Exception trace: {e}\n' ) finally: prev_show_family_title = show_family_title print(f'\nErrored count: {error_count} and errored_podcast_ids: {errored_podcast_ids} \n') def main(): """Extract shell arguments, create show family, start backfilling show_family_id, and grouping feeds.""" global request_headers argparser = argparse.ArgumentParser(prog='Backfill show_family_id and group feeds') argparser.add_argument('-env', required=True, help='env', default='qa') args = argparser.parse_args() filename = os.environ.get('FILENAME') env = args.env user_id = users[env] def get_user_id(): """Mock get user id return value.""" return user_id api_utils.get_user_id = get_user_id request_headers = { 'Grass-Account-Type': 'vendor', 'Grass-Account-Id': '1', 'Correlation-Id': '12', 'Orchard-Identity-UUID': 'podcast-admin', 'Content-Type': 'application/json' } log.info(f'Running backfill_show_family_id_and_group_multiple_feeds for {env} environment.\n') backfill_show_family_id_and_group_multiple_feeds(filename, user_id) if __name__ == '__main__': main()