import logging import pandas import pytest from jsonbender import F as ApplyFunction from jsonbender import K from jsonbender import S as GetField from parameterized import parameterized from ui_automation_framework.utils import logger as cl import app.src.tests.decomission.api_init as api_init from app.src.tests.decomission.compare_helper import format_color, generate_provider, get_field_with_diff, merge_arrays, transform_json API = api_init.APIOverride() log = cl.Logger(logging.DEBUG) apollo_to_delphi_keys = { # playlist object "playlist_id": (GetField("tracklistCountryCode") + K("_apple_") + GetField("id")), "name": GetField("name"), "curator_id": GetField("curatorId") >> ApplyFunction(lambda val: 0 if val is None else ("apple_" + str(val))), "curator_type": GetField("owner") >> ApplyFunction(lambda val: 0 if val is None else val["categoryId"]), "owner": GetField("owner") >> ApplyFunction(lambda val: 0 if val is None else ("apple_" + val["accountId"])), "owner_name": GetField("owner", "accountName"), "owner_category": GetField("owner") >> ApplyFunction(lambda val: 0 if val is None else val["categoryId"]), "category_name": GetField("owner", "categoryName"), # tracklist "tracklist_country_code": GetField("tracklistCountryCode"), } delphi_to_delphi_keys = { # playlist object "playlist_id": (GetField("playlist", "country_code") + K("_") + GetField("playlist_id")), "name": GetField("playlist", "name"), "curator_id": GetField("playlist", "owner") >> ApplyFunction(lambda val: 0 if val is None else val["username"]), "curator_type": GetField("playlist", "owner") >> ApplyFunction(lambda val: 0 if val is None or val["owner_category"] is None else int(val["owner_category"]["owner_category_id"])), "owner": GetField("playlist", "owner") >> ApplyFunction(lambda val: 0 if val is None else val["username"]), "owner_name": GetField("playlist", "owner") >> ApplyFunction(lambda val: 0 if val is None else val["display_name"]), "owner_category": GetField("playlist", "owner") >> ApplyFunction(lambda val: 0 if val is None or val["owner_category"] is None else int(val["owner_category"]["owner_category_id"])), "category_name": GetField("playlist", "owner") >> ApplyFunction(lambda val: 0 if val is None or val["owner_category"] is None else val["owner_category"]["name"]), # tracklist "tracklist_country_code": GetField("playlist", "country_code"), } current_playlist_apollo_portal = "/applemusic/playlists/{0}?country={1}" current_playlist_delphi = "/v3/public/track-positions/playlists?dsp=apple&include=playlists&playlist_id=apple_{0}&country_code={1}" @parameterized.expand( [ ("pl.2754a2d6e0084e2b8106fcd35fef6492", "us"), # ('pl.132b9a231cbf464086b4f838b2726f94', 'us'), # ('37i9dQZF1DX03VbhoH7arm', 'global'), # ('USSM12200612', ''), # ('KRA402200020', ''), # ('GBBKS1200164', ''), # ('pl.00fc149058624fb5b3cb83433591902c', 'us'), ("pl.9a745aff88074cb9a13156b1dacc16c5", "us"), ] ) @pytest.mark.skip() def test_playlist_meta_data(playlist_id, market): apollo_response = API.get_apollo_response(current_playlist_apollo_portal.format(playlist_id, market)) delphi_response = API.get_delphi_response(current_playlist_delphi.format(playlist_id, market)) apollo_transformed_response = transform_json(apollo_to_delphi_keys, [apollo_response]) delphi_transformed_response = transform_json(delphi_to_delphi_keys, delphi_response["items"]) aggregated_matrix = merge_arrays(apollo_transformed_response, delphi_transformed_response, "playlist_id") index_for_all_fields = list(range(1, len(generate_provider(aggregated_matrix)) + 1)) subset_to_mark = range(3, len(aggregated_matrix) * 5, 5) pandas.DataFrame( { "provider": generate_provider(aggregated_matrix), "playlist_id": get_field_with_diff("playlist_id", aggregated_matrix), "name": get_field_with_diff("name", aggregated_matrix), "curator_id": get_field_with_diff("curator_id", aggregated_matrix), "curator_type": get_field_with_diff("curator_type", aggregated_matrix), "owner": get_field_with_diff("owner", aggregated_matrix), "owner_name": get_field_with_diff("owner_name", aggregated_matrix), "owner_category": get_field_with_diff("owner_category", aggregated_matrix), "category_name": get_field_with_diff("category_name", aggregated_matrix), # tracklist "tracklist_country_code": get_field_with_diff("tracklist_country_code", aggregated_matrix), }, index=index_for_all_fields, ).style.apply(format_color, subset=(subset_to_mark, slice(None))).to_excel(f"decomission/apple_playlist_metadata/{playlist_id}_apple_playlist_metadata.xlsx")