"""Create show family and backfill show family id for single feeds. Run this in dev with command PYTHONPATH=. env/bin/python3.8 dev/backfill_show_family_id_for_single_feeds.py -- -env dev """ import argparse import logging 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 def get_podcasts_without_show_family_id(): """Return podcasts without show_family_id. Returns: dict: containing the podcasts. """ with mysql.pod_db_session(read_only=True) as session: rows = session.query(podcast_model.Podcast).filter( podcast_model.Podcast.is_deleted.isnot(True), podcast_model.Podcast.show_family_id.is_(None) ).all() return [row.to_dict() for row in rows] def backfill_show_family_id_for_single_feeds(user_id): """Create show family, backfill show family id for single feeds. Args: user_id (str): user id of the user based on env. """ podcast_id = None error_count = 0 errored_podcast_ids = [] print('Fetching podcasts without show_family_id') podcasts = get_podcasts_without_show_family_id() with mysql.pod_db_session() as session: for podcast in podcasts: try: podcast_id = podcast['id'] print(f'\nRunning for podcast having id: {podcast_id}') created_show_family = show_family_model.create_show_family( { 'network_id': podcast['network_id'], 'title': podcast['title'], 'created_by': user_id, 'updated_by': user_id }, session ) show_family_id = created_show_family['id'] print( 'Created show_family having id: {} and title: "{}" '.format( show_family_id, created_show_family['title']) ) podcast_model.update_podcast( podcast_id, { 'show_family_id': show_family_id }, 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 having id: {podcast_id}\n' f'Exception trace: {e}\n' ) 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 for single feeds.""" global request_headers argparser = argparse.ArgumentParser(prog='Backfill show_family_id for single feeds') argparser.add_argument('-env', required=True, help='env', default='qa') args = argparser.parse_args() 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_for_single_feeds for {env} environment.\n') backfill_show_family_id_for_single_feeds(user_id) if __name__ == '__main__': main()