import logging import pandas import pytest from jsonbender import F as ApplyFunction 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, get_field_with_diff_custom, merge_arrays, transform_json API = api_init.APIOverride() log = cl.Logger(logging.DEBUG) apollo_to_delphi_keys = { # playlist object "playlist_id": GetField("playlistUri") >> ApplyFunction(lambda val: val.replace(":playlist:", "_")), "name": GetField("name"), "owner": GetField("userId") >> ApplyFunction(lambda val: 0 if val is None else ("spotify_" + val)), "owner_name": GetField("userDisplayName") >> ApplyFunction(lambda val: 0 if val is None else val), "owner_category": GetField("buzzCategoryId") >> ApplyFunction(lambda val: 0 if val is None else val), "followers": GetField("followers"), "is_personalized": GetField("is_personalized"), } delphi_to_delphi_keys = { # playlist object "playlist_id": GetField("playlist_id"), "name": GetField("playlist", "name"), "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"])), "followers": GetField("playlist", "followers"), "is_personalized": GetField("playlist", "is_personalised"), } current_playlist_apollo_portal = "/sonytracks/analyze?playlisturi=spotify%3Aplaylist%3A{0}" is_playlist_personalized = "/apollo-api/spotify/personalized-playlists/?playlist_ids={0}" current_playlist_delphi = "/v3/public/track-positions/playlists?dsp=spotify&include=playlists&playlist_id=spotify_{0}" @parameterized.expand( [ "37i9dQZF1DXcBWIGoYBM5M", "37i9dQZF1DX4o1oenSJRJd", "37i9dQZF1DX3rxVfibe1L0", "5H765bjiGWNovpZbfPeM3o", ] ) @pytest.mark.skip() def test_playlist_meta_data(playlist_id): apollo_response = API.get_apollo_response(current_playlist_apollo_portal.format(playlist_id)) personalized_list = API.get_apollo_response(is_playlist_personalized.format(playlist_id)) if len(personalized_list) > 0: if personalized_list[0] == playlist_id: apollo_response["is_personalized"] = True else: apollo_response["is_personalized"] = False apollo_transformed_response = transform_json(apollo_to_delphi_keys, [apollo_response]) delphi_response = API.get_delphi_response(current_playlist_delphi.format(playlist_id)) 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), "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), "followers": get_field_with_diff_custom("followers", aggregated_matrix, 2), "is_personalized": get_field_with_diff("is_personalized", aggregated_matrix), }, index=index_for_all_fields, ).style.apply(format_color, subset=(subset_to_mark, slice(None))).to_excel(f"decomission/spotify_playlist_metadata/{playlist_id}_spotify_playlist_metadata.xlsx")