import copy 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_id": (GetField("country", "countryCode") + K("_apple_") + GetField("id")), "name": GetField("name"), "owner": GetField("owner") >> ApplyFunction(lambda val: 0 if val is None else ("apple_" + val["accountId"])), "owner_name": GetField("owner") >> ApplyFunction(lambda val: 0 if val is None else val["accountName"]), "owner_category": GetField("owner") >> ApplyFunction(lambda val: 0 if val is None or val["categoryId"] is None else val["categoryId"]), "country_code": GetField("country", "countryCode") >> ApplyFunction(lambda val: "_gl" if val == "worldwide" else val), # historical values "added": GetField("country", "earliestDate"), "removed_date": GetField("country", "latestDate"), "days_in_playlist": GetField("country", "daysInPlaylist"), } delphi_to_delphi_keys = { # playlist object "playlist_id": (GetField("playlist", "country_code") + K("_") + 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"])), "is_personalized": GetField("playlist", "is_personalised"), "country_code": GetField("playlist", "country_code") >> ApplyFunction(lambda val: "_gl" if val == "worldwide" else val), # historical values "added": GetField("earliest_position_date_time") >> ApplyFunction(lambda val: "" if val is None else val.split("T")[0]), "removed_date": GetField("removed_date_time") >> ApplyFunction(lambda val: "" if val is None else val.split("T")[0]), "days_in_playlist": GetField("num_days_on") >> ApplyFunction(lambda val: int(val) + 1), } limit = 10000 prev_playlist_apollo_portal = "/applemusic/playlists/tracklisthistory/track?isrc={0}&limit={1}&excludeCurrentPlaylists=true" prev_playlist_delphi = "/v3/public/track-positions/previous/playlists?isrc={0}&dsp=apple&include=playlists&limit={1}" @parameterized.expand( [ # as it was ("USSM12200612"), # flowers # ('USSM12209777'), # watermelon sugar # ('USSM11912587') ] ) @pytest.mark.skip() def test_previous_playlists(isrc): apollo_response = API.get_apollo_response(prev_playlist_apollo_portal.format(isrc, limit)) delphi_response = API.get_delphi_response(prev_playlist_delphi.format(isrc, limit)) apollo_total_playlists_count = apollo_response["pagination"]["total"] print(f"apollo playlists count: {apollo_total_playlists_count}") delphi_playlists = [] for playlist in delphi_response["items"]: delphi_playlists.append(playlist["playlist_id"]) delphi_total_playlists_count = len(set(delphi_playlists)) print(f"delphi playlists count: {delphi_total_playlists_count}") delphi_total = delphi_response["count"] # each country for playlist are being inside list on playlist level. # converting it to array with each separate country expand_apple_array = [] for val in apollo_response["items"]: simple_playlist_item = val countries = simple_playlist_item["countries"] del simple_playlist_item["countries"] for country in countries: country_copy = copy.copy(simple_playlist_item) country_copy["country"] = country expand_apple_array.extend([country_copy]) apollo_total = len(expand_apple_array) apollo_transformed_response = transform_json(apollo_to_delphi_keys, expand_apple_array) 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), "country_code": get_field_with_diff("country_code", aggregated_matrix), # historical values "added": get_field_with_diff("added", aggregated_matrix), "removed_date": get_field_with_diff("removed_date", aggregated_matrix), "days_in_playlist": get_field_with_diff("days_in_playlist", aggregated_matrix), }, index=index_for_all_fields, ).style.apply(format_color, subset=(subset_to_mark, slice(None))).to_excel(f"decomission/apple_track_prev_playlists/{isrc}_apple_prev_playlist_total_apollo_{apollo_total}_delphi_{delphi_total}.xlsx")