"""Backfill podcast seasons. Run this in dev with command PYTHONPATH=. env/bin/python3.8 dev/backfill_podcast_seasons.py -- -env dev """ import argparse import csv import logging import os import sys from podcast.connectors import mysql 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_all_seasons(season_model): """Return all podcast_seasons. Returns: list: containing the tuple of all podcast seasons. """ with mysql.pod_db_session(read_only=True) as session: return session.query(season_model.PodcastSeason.podcast_id, season_model.PodcastSeason.number).all() def get_podcast_seasons(season_model, podcast_id): """Return a podcast seasons. Returns: list: containing the tuple of a podcast seasons. """ with mysql.pod_db_session(read_only=True) as session: return session.query(season_model.PodcastSeason.number).filter( season_model.PodcastSeason.podcast_id == podcast_id ).all() def create_podcast_seasons(podcast_id, season_number, season_model): data = [] missing_season_nos= [] existing_seasons = get_podcast_seasons(season_model, podcast_id) season_nos = [ season[0] for season in existing_seasons ] if season_number not in season_nos: season_nos.append(season_number) missing_season_nos.append(season_number) for season_no in range(1, max(season_nos)+1): if season_no not in season_nos: missing_season_nos.append(season_no) missing_season_nos.sort() for season_no in missing_season_nos: data.append(dict( number=season_no, name=f'Season {season_no}' )) return season_model.create_seasons(podcast_id, data, False) def backfill_podcast_seasons(filename): """Backfill podcast_seasons for newly created seasons on episode level. Args: filename (str): The filename to access the csv file. """ from podcast.models import podcast_season as season_model existing_seasons = get_all_seasons(season_model) with open(filename) as csv_file: error_count = 0 errored_podcast_seasons = [] reader = csv.DictReader(csv_file) for row in reader: try: podcast_id = int(row['Podcast Id']) season_number = int(row['Season Number']) print(f'Running for podcast_id: {podcast_id} & season number: {season_number}') if not (podcast_id, season_number) in existing_seasons: create_podcast_seasons(podcast_id, season_number, season_model) except Exception as e: errored_podcast_seasons.append((podcast_id, season_number)) error_count += 1 log.info( f'Got an exception for podcast_id: {podcast_id} & season number: {season_number}\n' f'Exception trace: {e}\n' ) print(f'\nErrored errored count: {error_count} and podcast_seasons: {errored_podcast_seasons} \n') def main(): """Extract shell arguments and start backfilling newly added season's on episode model.""" global request_headers argparser = argparse.ArgumentParser(prog='Backfill podcast_season') argparser.add_argument('-env', required=True, help='env', default='qa') args = argparser.parse_args() env = args.env filename = os.environ.get('FILENAME', 'backfill_podcast_seasons.csv') def get_user_id(): """Mock get user id return value.""" return users[env] 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 podcast_seasons backfill for {env} Environment.\n') backfill_podcast_seasons(filename) if __name__ == '__main__': main()