"""Backfill apple subscription shows. Run this in dev with command PYTHONPATH=. env/bin/python3.8 dev/backfill_apple_subscription_shows.py -- -env dev """ import argparse import csv import logging import os import sys import requests from podcast.constants import common from podcast.constants.episode_replication_status import REPLICATION_TYPE_BULK from podcast.models.episode import get_episodes from podcast.models import podcast as podcast_model from podcast.models import podcast_season as podcast_season_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 = { 'Orchard-Identity-Id': 'podcast-admin', 'Orchard-Identity-UUID': 'podcast-admin', 'Orchard-Roles': 'admin', 'Content-Type': 'application/json' } def backfill_shows(filename, env): """Backfill apple subscription shows. Args: filename: path of file to be processed env: environment in which the script is being run """ global request_headers if not filename: raise Exception('File with name: applesubscriptionsshows.csv not found.') log.info(f'Reading file: {filename}.\n') with open(filename) as csv_file: show_count = 0 error_count = 0 errored_shows = [] reader = csv.DictReader(csv_file) for row in reader: try: original_podcast_id = int(row['Show ID']) log.info(f'Running backfill for show {original_podcast_id}\n') podcast = podcast_model.get_podcast_by_id(original_podcast_id) original_episode_ids = get_episodes(original_podcast_id, filter_by_state='DONE') published_episode_ids = list(map(lambda episode: episode['id'], original_episode_ids['items'])) original_podcast_seasons = podcast.get('seasons') feed_data = { 'title': row['Feed Name'], 'description': podcast['description'], 'feed_type': row['Feed Type'], 'show_family_id': podcast['show_family_id'], 'network_id': podcast['network_id'], 'host': podcast['host'], 'owner': podcast['owner'], 'copyright': podcast['copyright'], 'email': podcast['email'], 'explicit': podcast['explicit'], 'show_type': podcast['show_type'], 'language': podcast['language'], 'categories': podcast['categories'], 'link': podcast.get('link'), 'channel_id': row['Apple Channel ID'] } created_podcast = podcast_model.create_podcast(feed_data) podcast_id = created_podcast['id'] log.info(f'A new feed has been created with id {podcast_id}\n') new_podcast_seasons = None is_serial_show = podcast['show_type'] == common.SHOW_TYPE_SERIAL log.info(f'Starting podcast artwork replication for feed with id {podcast_id}\n') replicate_artwork_response = requests.post( f'https://{env}-ows-asset-transcoder.theorchard.io/replicate-podcast-artwork-asset', json = { 'original_podcast_id': original_podcast_id, 'podcast_id': podcast_id }, headers= request_headers ) log.info(f'Replicate Artwork Status {replicate_artwork_response} \n') if original_podcast_seasons and is_serial_show: seasons_data = [ {'name': season['name'], 'number': season['number']} for season in original_podcast_seasons ] new_podcast_seasons = podcast_season_model.create_seasons(podcast_id, seasons_data, False) for season in new_podcast_seasons: season['season_id'] = season.pop('id') if published_episode_ids: log.info(f'Starting Replicate episodes and assets\n') replicate_episode_response = requests.post( f'https://{env}-ows-podcast.theorchard.io/replicate-episodes-and-create-assets', json = { 'original_podcast_id': original_podcast_id, 'podcast_id': podcast_id, 'original_episode_ids': published_episode_ids, 'seasons': new_podcast_seasons, 'replication_type': REPLICATION_TYPE_BULK, 'is_copy_apple_episode_id': True if row['Feed Type'] == common.APPLE_SUBSCRIPTION else False }, headers= request_headers ) log.info(f'Episode and assets replication response {replicate_episode_response} \n') except Exception as e: error_count += 1 errored_shows.append(original_podcast_id) log.info( f'Got an exception for show_id: {original_podcast_id}\n' f'Exception trace: {e}\n' ) show_count += 1 print(f'\nErrored errored count: {error_count} and show_ids: {errored_shows} \n') def main(): """Create apple replica of Public RSS shows and all episodes under those show.""" argparser = argparse.ArgumentParser(prog='Backfill episode external id') argparser.add_argument('-env', required=True, help='env', default='qa') args = argparser.parse_args() filename = 'applesubscriptionsshows.csv' or os.environ.get('FILENAME') env = args.env def get_user_id(): """Mock get user id return value.""" return users[env] api_utils.get_user_id = get_user_id log.info(f'Running backfill apple subscription shows for {env} Environment.\n') backfill_shows(filename, env) if __name__ == '__main__': main()