using System; using System.Collections.Generic; using System.Linq; using Nancy; using Newtonsoft.Json; using Sony.Filtr.AdminAPI.ViewModels.Spotify.Analytics; using Sony.Filtr.Contracts.Abstractions; using Sony.Filtr.Contracts.Definitions; using Sony.Filtr.SpotifyAnalytics; using Sony.Filtr.SpotifyAnalytics.Data; using Sony.Filtr.Utility; using Sony.Filtr.Utility.Logging; using Sony.Filtr.Utility.Serialization; namespace Sony.Filtr.AdminAPI.Modules.SpotifySpecific { public class SpotifyAnalyticsTrackModule : BaseModule { private readonly SpotifyAnalyticsManager _spotifyAnalyticsManager; private readonly MappingUtility _mappingUtility; public SpotifyAnalyticsTrackModule( IApplicationInstanceManager applicationInstanceManager, SpotifyAnalyticsManager spotifyAnalyticsManager, MappingUtility mappingUtility, ILogger logger) : base(applicationInstanceManager, logger) { _spotifyAnalyticsManager = spotifyAnalyticsManager; _mappingUtility = mappingUtility; Get["GetSpotifyAnalyticsTrackStreams", "/spotifyAnalytics/track/streams", true] = async (parameters, cx) => { Response result = await LogActionWrapper.Execute( async () => await GetSpotifyAnalyticsTrackStreams(mappingUtility, Request), Logger, "/spotifyAnalytics/track/streams -> GetSpotifyAnalyticsTrackStreams", ExtractRequestData(Request)); return result; }; Get["GetSpotifyAnalyticsTrackDemographics", "/spotifyAnalytics/track/demographics", true] = async (parameters, cx) => { Response result = await LogActionWrapper.Execute( async () => await GetSpotifyAnalyticsTrackDemographics(Request), Logger, "/spotifyAnalytics/track/demographics -> GetSpotifyAnalyticsTrackDemographics", ExtractRequestData(Request)); return result; }; Get["GetSpotifyAnalyticsTrackListeners", "/spotifyAnalytics/track/listeners", true] = async (parameters, cx) => { DateTime startDate = Maybe.ToDateTimeOrFallback((string)Request.Query.startDate, DateTime.Today.AddDays(-30)); DateTime endDate = Maybe.ToDateTimeOrFallback((string)Request.Query.endDate, DateTime.Today); string countryCode = (string)Request.Query.countryCode; var isrcParameter = (string)Request.Query.isrc; var isrcs = isrcParameter?.Split(',').ToList(); if (isrcs == null || !isrcs.Any()) return new Response { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "isrc is required." }; var trackListeners = await _spotifyAnalyticsManager.GetTrackListenersAsync(isrcs, startDate, endDate, countryCode); var trackItems = AutoMapper.Mapper.Map>(trackListeners); var viewModel = new SpotifyTrackListenerViewModel() { Items = trackItems }; return Response.AsJson(viewModel); }; } private async System.Threading.Tasks.Task GetSpotifyAnalyticsTrackStreams(MappingUtility mappingUtility, Request request) { DateTime startDate = Maybe.ToDateTimeOrFallback((string)request.Query.startDate, DateTime.Today.AddDays(-30)); DateTime endDate = Maybe.ToDateTimeOrFallback((string)request.Query.endDate, DateTime.Today); string countryCode = (string)request.Query.countryCode; var isrcParameter = (string)request.Query.isrc; var isrcs = isrcParameter?.Split(',').ToList(); if (isrcs == null || !isrcs.Any()) return new Response { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "isrc is required." }; string licensor = (string)request.Query.licensor; SpotifyAnalyticsAccount? licensorAccount = null; if (!string.IsNullOrWhiteSpace(licensor)) licensorAccount = mappingUtility.SpotifyAnalyticsAccountFromString(licensor); var trackStreams = await _spotifyAnalyticsManager.GetTrackStreamsAsync(isrcs, startDate, endDate, countryCode, licensorAccount); var viewModel = new SpotifyAnalyticsTrackStreamViewModel { Items = trackStreams.Select(BuildAnalyticsTrackStreamViewModel).ToList() }; return Response.AsJson(viewModel); } private async System.Threading.Tasks.Task GetSpotifyAnalyticsTrackDemographics(Request request) { DateTime startDate = Maybe.ToDateTimeOrFallback((string)request.Query.startDate, DateTime.Today.AddDays(-30)); DateTime endDate = Maybe.ToDateTimeOrFallback((string)request.Query.endDate, DateTime.Today); string countryCode = (string)request.Query.countryCode; string isrc = (string)request.Query.isrc; if (string.IsNullOrWhiteSpace(isrc)) return new Response { StatusCode = HttpStatusCode.BadRequest, ReasonPhrase = "isrc is required." }; IEnumerable trackDemographics = await _spotifyAnalyticsManager.GetTrackDemographicsAsync(isrc, startDate, endDate, countryCode); var viewModel = new SpotifyTrackDemographicViewModel() { Items = trackDemographics }; return Response.AsJson(viewModel); } private SpotifyAnalyticsTrackStreamItemViewModel BuildAnalyticsTrackStreamViewModel(SpotifyTrackStream trackStream) { return new SpotifyAnalyticsTrackStreamItemViewModel { CountryCode = trackStream.country_code, DeviceOsAndroid = trackStream.device_os_android, DeviceOsBlackberry = trackStream.device_os_blackberry, DeviceOsBrowser = trackStream.device_os_browser, DeviceOsIos = trackStream.device_os_ios, DeviceOsLinux = trackStream.device_os_linux, DeviceOsMac = trackStream.device_os_mac, DeviceOsOther = trackStream.device_type_other, DeviceOsWindows = trackStream.device_os_windows, DeviceTypeBuiltinCarApplication = trackStream.device_type_builtin_car_application, DeviceTypeCellPhone = trackStream.device_type_cell_phone, DeviceTypeConnectedAudioDevice = trackStream.device_type_connected_audio_device, DeviceTypeGamingConsole = trackStream.device_type_gaming_console, DeviceTypeOther = trackStream.device_type_other, DeviceTypePersonalComputer = trackStream.device_type_personal_computer, DeviceTypeSmartTvDevice = trackStream.device_type_smart_tv_device, DeviceTypeTablet = trackStream.device_type_tablet, Isrc = trackStream.isrc, RepeatPlay = trackStream.repeat_play, ShufflePlay = trackStream.shuffle_play, SourceAlbum = trackStream.source_album, SourceArtist = trackStream.source_artist, SourceChart = trackStream.source_chart, SourceCollection = trackStream.source_collection, SourceOther = trackStream.source_other, SourceOthersPlaylist = trackStream.source_others_playlist, SourceRadio = trackStream.source_radio, SourceSearch = trackStream.source_search, Streams = trackStream.streams }; } } public class SpotifyTrackStreamViewModel { public IEnumerable Items { get; set; } } public class SpotifyTrackStreamItemViewModel { [JsonConverter(typeof(DateNoTimeConverter))] public DateTime Date { get; set; } public string CountryCode { get; set; } public int Saves { get; set; } public int Skips { get; set; } public int Streams { get; set; } public string Isrc { get; set; } } public class SpotifyTrackDemographicViewModel { public IEnumerable Items { get; set; } } public class SpotifyTrackDemographicItemViewModel { [JsonConverter(typeof(DateNoTimeConverter))] public DateTime Date { get; set; } public string CountryCode { get; set; } public string Isrc { get; set; } public int GenderFemaleAge0_17 { get; set; } public int GenderMaleAge0_17 { get; set; } public int GenderNeutralAge0_17 { get; set; } public int GenderUnknownAge0_17 { get; set; } public int GenderFemaleAge18_22 { get; set; } public int GenderMaleAge18_22 { get; set; } public int GenderNeutralAge18_22 { get; set; } public int GenderUnknownAge18_22 { get; set; } public int GenderFemaleAge23_27 { get; set; } public int GenderMaleAge23_27 { get; set; } public int GenderNeutralAge23_27 { get; set; } public int GenderUnknownAge23_27 { get; set; } public int GenderFemaleAge28_34 { get; set; } public int GenderMaleAge28_34 { get; set; } public int GenderNeutralAge28_34 { get; set; } public int GenderUnknownAge28_34 { get; set; } public int GenderFemaleAge35_44 { get; set; } public int GenderMaleAge35_44 { get; set; } public int GenderNeutralAge35_44 { get; set; } public int GenderUnknownAge35_44 { get; set; } public int GenderFemaleAge45_59 { get; set; } public int GenderMaleAge45_59 { get; set; } public int GenderNeutralAge45_59 { get; set; } public int GenderUnknownAge45_59 { get; set; } public int GenderFemaleAge60_150 { get; set; } public int GenderMaleAge60_150 { get; set; } public int GenderNeutralAge60_150 { get; set; } public int GenderUnknownAge60_150 { get; set; } public int GenderFemaleAgeUnknown { get; set; } public int GenderMaleAgeUnknown { get; set; } public int GenderNeutralAgeUnknown { get; set; } public int GenderUnknownAgeUnknown { get; set; } } public class SpotifyTrackListenerViewModel { public List Items { get; set; } } public class SpotifyTrackListenerItemViewModel { public DateTime ReportDate { get; set; } public string ReportCountryCode { get; set; } public DateTime Date { get; set; } public string CountryCode { get; set; } public string Isrc { get; set; } public int Listeners { get; set; } } }