import logging import pandas import pytest from jsonbender import F as ApplyFunction from jsonbender import OptionalS 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 calc_days_in_playlist, format_color, generate_provider, get_field_with_diff, get_field_with_diff_custom, get_field_with_diff_custom_date, get_field_with_diff_int, merge_arrays, transform_json API = api_init.APIOverride() log = cl.Logger(logging.DEBUG) apollo_to_delphi_keys = { "playlist_id": GetField("playlist", "spotifyLink") >> ApplyFunction(lambda val: val.replace(":playlist:", "_")), "name": GetField("playlist", "name"), "owner": GetField("playlist") >> ApplyFunction(lambda val: 0 if val is None else ("spotify_" + val["accountId"])), "owner_name": GetField("playlist") >> ApplyFunction(lambda val: 0 if val is None else val["accountName"]), "owner_category": GetField("playlist") >> ApplyFunction(lambda val: 0 if val is None else val["buzzCategoryId"]), "is_personalized": GetField("playlist", "is_personalized"), "country_code": OptionalS("playlist", "countryCode", default="") >> ApplyFunction(lambda val: "" if val is None else val.lower().replace("_gl", "global")), # streams "followers": GetField("playlist", "subscribers"), # historical values "added": GetField("earliestTrackDate") >> ApplyFunction(lambda val: "" if val is None else val[:10]), "removed_date": GetField("latestTrackDate") >> ApplyFunction(lambda val: "" if val is None else val[:10]), "days_in_playlist": GetField("days_in_playlist"), # calc based on dates to match Delphi } 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"])), "is_personalized": GetField("playlist", "is_personalised"), "country_code": OptionalS("playlist", "country_code", default=""), # >> ApplyFunction(lambda val: "_gl" if val == "worldwide" else val), # streams "followers": GetField("playlist", "followers"), # historical values "added": GetField("earliest_position_date_time") >> ApplyFunction(lambda val: "" if val is None else val[:10]), "removed_date": GetField("removed_date_time") >> ApplyFunction(lambda val: "" if val is None else val[:10]), "days_in_playlist": GetField("num_days_on"), } apollo_offset = 0 apollo_limit = 2000 delphi_limit = 10000 prev_playlist_apollo_portal = "/gate-api/playlists/spotify/by-track/history/?isrc={0}&limit={1}&excludeCurrentPlaylists=true" prev_playlist_delphi = "/v3/public/track-positions/previous/playlists?isrc={0}&dsp=spotify&include=playlists&limit={1}" @parameterized.expand( [ # "USSM12103949", # "USSM12200612", # 'KRA402200020', # 'GBBKS1200164' # NEW # 'USQX92205877', # 'CLPP22200072', # 'GBUM72204532', # 'USWL12201130', # 'QZNMY2232113' # 'USAT22221156', # 'NL6NQ2200016', # 'ITDJ72200061' # 'DEUM72203211', # 'AUNV02201200', # NEW - 16.12.22 # ('USSM12211733'), # ('ITB002200852'), # ('USUG12209223'), # ('US39N2203950'), # as it was # ("USSM12200612"), # flowers # 'USSM12209777', # 'USJI10800160', # 'GBARL1300107', # 'GBARL1300522', # 'USSM10804556', "USSM12210918" ] ) @pytest.mark.skip() def test_previous_playlists(isrc): playlists_count = API.get_apollo_response(prev_playlist_apollo_portal.format(isrc, apollo_limit))["pagination"]["total"] apollo_response_all = [] for offset in range(0, playlists_count, apollo_limit): apollo_response = API.get_apollo_response(prev_playlist_apollo_portal.format(isrc, apollo_limit))["items"] apollo_response_all += apollo_response delphi_response = API.get_delphi_response(prev_playlist_delphi.format(isrc, delphi_limit)) apollo_response_with_days_in_playlist = calc_days_in_playlist(apollo_response_all) apollo_transformed_response = transform_json(apollo_to_delphi_keys, apollo_response_with_days_in_playlist) delphi_transformed_response = transform_json(delphi_to_delphi_keys, delphi_response["items"]) apollo_total = playlists_count delphi_total = delphi_response["count"] 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), "is_personalized": get_field_with_diff("is_personalized", aggregated_matrix), "country_code": get_field_with_diff("country_code", aggregated_matrix), # streams "followers": get_field_with_diff_custom("followers", aggregated_matrix, 2), # historical values "added": get_field_with_diff_custom_date("added", aggregated_matrix), "removed_date": get_field_with_diff_custom_date("removed_date", aggregated_matrix), "days_in_playlist": get_field_with_diff_int("days_in_playlist", aggregated_matrix), }, index=index_for_all_fields, ).style.apply(format_color, subset=(subset_to_mark, slice(None))).to_excel(f"decomission/spotify_track_prev_playlists/{isrc}_spotify_prev_playlist_total_apollo_{apollo_total}_delphi_{delphi_total}.xlsx")