import copy import datetime 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 extend_playlists_with_streams, format_color, generate_provider, get_field_with_diff, get_field_with_diff_custom_date, get_field_with_diff_custom_date_or_zero, get_field_with_diff_delphi_not_empty, 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"]), "track_count": GetField("country", "playlistTrackCount"), "country_code": GetField("country", "countryCode"), # streams "playlist_streams": GetField("country", "streams7Days"), "streams": GetField("country", "isrc_streams") >> ApplyFunction(lambda val: 0 if val is None else val), "7_streams": GetField("country", "isrc_7_streams") >> ApplyFunction(lambda val: 0 if val is None else val), # localstreams_7days local_streams # historical values "current": GetField("country", "trackPosition") >> ApplyFunction(lambda val: val - 1), "trend": GetField("country", "positionChange") >> ApplyFunction(lambda val: 0 if val is None else val), "trend_date": GetField("country", "positionChangeDate") >> ApplyFunction(lambda val: "" if val is None else str(val).split("T")[0]), "added": GetField("country", "added") >> ApplyFunction(lambda val: "" if val is None else str(val).split("T")[0]), "earliest_added": GetField("country", "earliestAdded") >> ApplyFunction(lambda val: "" if val is None else str(val).split("T")[0]), "album_name": "", "upc": "", } delphi_to_delphi_keys_local = { # playlist object "playlist_id": (GetField("playlist", "country_code") + K("_") + GetField("playlist_id")) >> ApplyFunction(lambda val: "_gl" if val == "worldwide" else val), "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 or val["owner_category"]["owner_category_id"] is None else int(val["owner_category"]["owner_category_id"])), "track_count": GetField("playlist", "num_tracks"), "country_code": GetField("playlist", "country_code"), # streams "playlist_streams": GetField("streaming_info", "us") >> ApplyFunction(lambda val: 0 if val is None or val["7"] is None or val["7"]["playlist"] is None else val["7"]["playlist"]), # better to give as 0 "streams": GetField("streaming_info", "us") >> ApplyFunction(lambda val: 0 if val is None or val["1"] is None or val["1"]["isrc_in_playlist"] is None else val["1"]["isrc_in_playlist"]), # need to add conditions for using for any market "7_streams": GetField("streaming_info", "us") >> ApplyFunction(lambda val: 0 if val is None or val["7"] is None or val["7"]["isrc_in_playlist"] is None else val["7"]["isrc_in_playlist"]), # need to add conditions for using for any market # localstreams_7days local_streams # historical values "current": GetField("current"), "trend": GetField("trend_change_14_days") >> ApplyFunction(lambda val: 0 if val is None else val), "trend_date": GetField("last_date_change_14_days") >> ApplyFunction(lambda val: "" if val is None else str(val).split("T")[0]), "added": GetField("last_added_date_time") >> ApplyFunction(lambda val: "" if val is None else str(val).split("T")[0]), "earliest_added": GetField("earliest_position_date_time") >> ApplyFunction(lambda val: "" if val is None else str(val).split("T")[0]), "album_name": GetField("apple_track", "album") >> ApplyFunction(lambda val: "" if val is None or val["name"] is None else val["name"]), "upc": GetField("apple_track", "album") >> ApplyFunction(lambda val: "" if val is None or val["upc"] is None else val["upc"]), } delphi_to_delphi_keys_ww = delphi_to_delphi_keys_local.copy() delphi_to_delphi_keys_ww.update({"streams": GetField("streaming_info", "local") >> ApplyFunction(lambda val: 0 if val is None or val["1"] is None or val["1"]["isrc_in_playlist"] is None else val["1"]["isrc_in_playlist"])}) delphi_to_delphi_keys_ww.update({"7_streams": GetField("streaming_info", "local") >> ApplyFunction(lambda val: 0 if val is None or val["7"] is None or val["7"]["isrc_in_playlist"] is None else val["7"]["isrc_in_playlist"])}) delphi_to_delphi_keys_ww.update({"playlist_streams": GetField("streaming_info", "local") >> ApplyFunction(lambda val: 0 if val is None or val["7"] is None or val["7"]["playlist"] is None else val["7"]["playlist"])}) limit = 10000 current_playlist_apollo_portal_local = "/applemusic/playlistswithtrack?isrc={0}&limit={1}&country={2}" current_playlist_apollo_track_streams_local = "/dsp-api/consumer_analytics/v1/playlists-summary?start_date={0}&end_date={1}&vendor=apple&isrc={2}&market={3}" current_playlist_delphi_local = "/v3/public/track-positions/playlists?dsp=apple&isrc={0}&include=playlists&include=streams_for_period" "&apple_country_code={1}&streams_country_code={2}&include=track_info" current_playlist_apollo_portal_ww = "/applemusic/playlistswithtrack?isrc={0}&limit={1}" current_playlist_apollo_track_streams_ww = "/dsp-api/consumer_analytics/v1/playlists-summary?start_date={0}&end_date={1}&vendor=apple&isrc={2}" current_playlist_delphi_ww = "/v3/public/track-positions/playlists?dsp=apple&isrc={0}&include=playlists,streams_for_period,track_info" "&streams_country_code=local&country_code=us,gb,cn,au,ca,ru,de,fr,mx,br,in,za,kr,it,tw,tr,ch,es,th,hk,ae,nz,dk,id,il,nl,se,no" @parameterized.expand( [ # as it was # ("USSM12200612", "us"), ("USSM12200612", "ww"), # flowers # ('USSM12209777', 'us'), ("USSM12209777", "ww"), # watermelon sugar # ('USSM11912587', 'us'), # ('USAT22211992', 'ww'), ("GBAYE2200771", "ww"), ] ) @pytest.mark.skip() def test_current_playlists(isrc, market): apple_latest_date = API.get_latest_date()["apple"] apple_latest_date = datetime.datetime.strptime(apple_latest_date, "%Y-%m-%d").date() apple_date_7_days = apple_latest_date - datetime.timedelta(days=6) print(apple_date_7_days) if market != "ww": apollo_response = API.get_apollo_response(current_playlist_apollo_portal_local.format(isrc, limit, market)) apollo_apple_track_streams_1_day_period = API.get_apollo_response(current_playlist_apollo_track_streams_local.format(apple_latest_date, apple_latest_date, isrc, market)) apollo_apple_track_streams_7_days_period = API.get_apollo_response(current_playlist_apollo_track_streams_local.format(apple_date_7_days, apple_latest_date, isrc, market)) delphi_response = API.get_delphi_response(current_playlist_delphi_local.format(isrc, market, market)) else: apollo_response = API.get_apollo_response(current_playlist_apollo_portal_ww.format(isrc, limit)) apollo_apple_track_streams_1_day_period = API.get_apollo_response(current_playlist_apollo_track_streams_ww.format(apple_latest_date, apple_latest_date, isrc)) apollo_apple_track_streams_7_days_period = API.get_apollo_response(current_playlist_apollo_track_streams_ww.format(apple_date_7_days, apple_latest_date, isrc)) delphi_response = API.get_delphi_response(current_playlist_delphi_ww.format(isrc)) apollo_total = apollo_response["pagination"]["total"] delphi_total = delphi_response["count"] # add 1 day and 7 days streams to general response apollo_response_1_day_streams = extend_playlists_with_streams(apollo_response, apollo_apple_track_streams_1_day_period, "isrc_streams") apollo_response_general = extend_playlists_with_streams(apollo_response_1_day_streams, apollo_apple_track_streams_7_days_period, "isrc_7_streams") # 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_general["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_transformed_response = transform_json(apollo_to_delphi_keys, expand_apple_array) if market != "ww": delphi_transformed_response = transform_json(delphi_to_delphi_keys_local, delphi_response["items"]) else: delphi_transformed_response = transform_json(delphi_to_delphi_keys_ww, 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), "track_count": get_field_with_diff("track_count", aggregated_matrix), "country_code": get_field_with_diff("country_code", aggregated_matrix), # streams "playlist_streams": get_field_with_diff("playlist_streams", aggregated_matrix), "streams": get_field_with_diff("streams", aggregated_matrix), "7_streams": get_field_with_diff("7_streams", aggregated_matrix), # historical values "current": get_field_with_diff("current", aggregated_matrix), "trend": get_field_with_diff("trend", aggregated_matrix), "trend_date": get_field_with_diff_custom_date_or_zero("trend_date", aggregated_matrix), "added": get_field_with_diff_custom_date("added", aggregated_matrix), "earliest_added": get_field_with_diff_custom_date("earliest_added", aggregated_matrix), # maybe calc based on total days "album_name": get_field_with_diff_delphi_not_empty("album_name", aggregated_matrix), "upc": get_field_with_diff_delphi_not_empty("upc", aggregated_matrix), }, index=index_for_all_fields, ).style.apply(format_color, subset=(subset_to_mark, slice(None))).to_excel(f"decomission/apple_track_current_playlists/{isrc}_{market}_apple_current_playlist_total_apollo_{apollo_total}_delphi_{delphi_total}.xlsx")