import logging import pandas import pytest from jsonbender import F as ApplyFunction from jsonbender import If, 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 = { # album "albumId": GetField("albumId") >> ApplyFunction(lambda val: "" if val is None else str(val)), "albumName": GetField("albumName") >> ApplyFunction(lambda val: "" if val is None else val), "albumReleaseDate": GetField("albumReleaseDate") >> ApplyFunction(lambda val: "" if val is None else val.split("T")[0]), # artist "artistId": GetField("artistId") >> ApplyFunction(lambda val: "" if val is None else str(val)), "artistName": GetField("artistName") >> ApplyFunction(lambda val: "" if val is None else val), # track "trackId": GetField("id") >> ApplyFunction(lambda val: "" if val is None else str(val)), "trackIsrc": GetField("isrc") >> ApplyFunction(lambda val: "" if val is None else val), "trackName": GetField("name") >> ApplyFunction(lambda val: "" if val is None else val), "position": GetField("position") >> ApplyFunction(lambda val: "" if val is None else val), "releaseDate": If(GetField("releaseDate") == K(None), GetField("albumReleaseDate"), GetField("releaseDate")) >> ApplyFunction(lambda val: "" if val is None else val.split("T")[0]), } delphi_to_delphi_keys = { # album "albumId": GetField("apple_track", "album", "album_id") >> ApplyFunction(lambda val: "" if val is None else str(val)), "albumName": GetField("apple_track", "album", "name") >> ApplyFunction(lambda val: "" if val is None else val), "albumReleaseDate": GetField("apple_track", "album", "release_date") >> ApplyFunction(lambda val: "" if val is None else val), # artist "artistId": GetField("apple_track", "artists") >> ApplyFunction(lambda val: "" if len(val) == 0 else val[0]["artist_id"]), "artistName": GetField("apple_track", "artists") >> ApplyFunction(lambda val: "" if len(val) == 0 else " & ".join(v["name"] for v in val)), # track "trackId": GetField("apple_track", "track_id"), "trackIsrc": GetField("apple_track", "isrc") >> ApplyFunction(lambda val: "" if val is None else val), "trackName": GetField("apple_track", "name") >> ApplyFunction(lambda val: "" if val is None else val), "position": GetField("current") >> ApplyFunction(lambda val: "" if val is None else val + 1), "releaseDate": GetField("apple_track", "release_date") >> ApplyFunction(lambda val: "" if val is None else val), } limit = 1000 playlist_apollo_portal = "/applemusic/playlists/{0}/tracklisthistory/{1}/{2}" # &country_code=us country code does not work for Delphi playlist_delphi = "/v3/public/track-positions/playlists?dsp=apple&include=track_info&playlist_id=apple_{0}&start_date={1}&end_date={2}&include=track_info&expand_isrc_by_track_positions=true&country_code={3}" @parameterized.expand( [ ("pl.f4d106fed2bd41149aaacabb233eb5eb", "us"), # ('pl.005c5a0f52744716b0021df43fe61fc6', 'us'), # ('pl.0e61531414db4ee8a9b34fada69845c8', 'us'), # ('pl.005c5a0f52744716b0021df43fe61fc6', 'us'), # ('pl.0e61531414db4ee8a9b34fada69845c8', 'us'), # ('pl.9a964a33c1484aec8fdb0cac3e7771ed', 'us'), # ('pl.f4d106fed2bd41149aaacabb233eb5eb', 'ca'), # ("pl.4fe671d3bd994738ace5ae974daebc10", 'au'), # ("pl.559ea7b879434003bb7621b1d60d3403", 'gb'), # ("pl.567c541f63414e798be5cf214e155557", 'us'), # ("pl.56e0d896d28b4291ba7e02af84605fc7", 'us'), ] ) @pytest.mark.skip() def test_playlists_prev_tracklist(playlist_id, market): date = "2021-10-01" apollo_response = API.get_apollo_response(playlist_apollo_portal.format(playlist_id, market, date)) delphi_response = API.get_delphi_response(playlist_delphi.format(playlist_id, date, date, market))["items"] # delphi_response_e = [t["track"] for t in delphi_response['items'] if t["track"] != 'null'] # delphi_response_with_tracks = [i for i in delphi_response_e if i is not None] apollo_transformed_response = transform_json(apollo_to_delphi_keys, apollo_response) delphi_transformed_response = transform_json(delphi_to_delphi_keys, delphi_response) aggregated_matrix = merge_arrays(apollo_transformed_response, delphi_transformed_response, "trackIsrc") 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), # album "albumId": get_field_with_diff("albumId", aggregated_matrix), "albumName": get_field_with_diff("albumName", aggregated_matrix), "albumReleaseDate": get_field_with_diff("albumReleaseDate", aggregated_matrix), # artist "artistId": get_field_with_diff("artistId", aggregated_matrix), "artistName": get_field_with_diff("artistName", aggregated_matrix), # track "trackId": get_field_with_diff("trackId", aggregated_matrix), "trackIsrc": get_field_with_diff("trackIsrc", aggregated_matrix), "trackName": get_field_with_diff("trackName", aggregated_matrix), "position": get_field_with_diff("position", aggregated_matrix), "releaseDate": get_field_with_diff("releaseDate", aggregated_matrix), }, index=index_for_all_fields, ).style.apply(format_color, subset=(subset_to_mark, slice(None))).to_excel(f"decomission/apple_playlist_prev_tracklist/{date}_{market}_{playlist_id}_prev_tracklist_apollo_{len(apollo_transformed_response)}_delphi_{len(delphi_transformed_response)}.xlsx")