"""Backfill episode guids from megaphone. Run this in dev with command PYTHONPATH=. env/bin/python3.6 dev/backfill_episodes_guids.py -- -env dev -api_key=megaphone_token """ import argparse import time from podcast import config from podcast.connectors import mysql from podcast.models import episode as episode_model from podcast.models.api_episode import ApiEpisode from podcast.models.podcast import Podcast from podcast.utils import api_utils users = { 'dev': '366', 'qa': '1298', 'prod': '88' } request_headers = None def get_all_podcasts(): """Return podcast id, network id, and podcast megaphone id of all the podcasts. Returns: list: containing the tuple of podcast id, network id, and megaphone id. """ with mysql.pod_db_session(read_only=True) as session: podcasts = session.query(Podcast) \ .with_entities(Podcast.id, Podcast.network_id, Podcast.megaphone_id) \ .filter(Podcast.is_deleted.isnot(True)).all() return podcasts def get_episodes_by_podcast_id(podcast_id): """Return all the episodes by podcast id. Args: podcast_id (int): the podcast identifier Returns: list: containing the dict of episodes. """ with mysql.pod_db_session(read_only=True) as session: episodes = session.query(episode_model.Episode).filter( episode_model.Episode.podcast_id == podcast_id, episode_model.Episode.is_deleted.isnot(True) ).all() return [episode.to_dict() for episode in episodes] def backfill_episodes_guids(): """Backfill all the non-matching episode guids from megaphone. Get all podcasts from db Iterate each db podcast Get all episodes for each podcast from db and megaphone Iterate each db episode Check if megaphone episode matches with db episode If match, check if megaphone guid exist and matches db episode uuid Update episode uuid with megaphone guid for the episode in db Log any error/exception, one of the known exception is podcast not found in megaphone which gives 403 error """ mp_api_call_count = 0 all_orch_podcasts = get_all_podcasts() for orch_podcast in all_orch_podcasts: podcast_id, network_id, podcast_mp_id = orch_podcast try: if mp_api_call_count > 10: # avoid status 429 too many calls to megaphone api mp_api_call_count = 0 time.sleep(10) orch_episodes = get_episodes_by_podcast_id(podcast_id) mp_api_episode = ApiEpisode(network_id) mp_api_call_count += 1 mp_episodes = mp_api_episode.get_all(podcast_mp_id, page=1, per_page=500)['items'] for orch_episode in orch_episodes: mp_episode = next(( mp_episode for mp_episode in mp_episodes if mp_episode['id'] == orch_episode['megaphone_id'] ), None) if mp_episode: if mp_episode['guid'] and mp_episode['guid'] != orch_episode['uuid']: updated_episode = episode_model.update_episode(orch_episode['id'], {'uuid': mp_episode['guid']}) success_message = 'Updated guid of episode: {} from: {} to: {}\n'.format( orch_episode['id'], orch_episode['uuid'], updated_episode['uuid']) print(success_message) except Exception as error: error_message = 'Failed for podcast: {} with error: {}\n'.format(podcast_id, error) print(error_message) def main(): """Extract shell arguments and start backfilling episode guids.""" global request_headers argparser = argparse.ArgumentParser(prog='Backfill episodes guids') argparser.add_argument('-api_key', required=True, help='Megaphone API Key') argparser.add_argument('-env', required=True, help='env', default='qa') args = argparser.parse_args() config.MEGAPHONE_API_TOKEN = args.api_key env = args.env 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' } backfill_episodes_guids() if __name__ == '__main__': main()