using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using NLog; using Sony.Filtr.Buzz; using Sony.Filtr.Contracts.Definitions; using Sony.Filtr.Contracts.Entities; using Sony.Filtr.Core.EditorialPlaylists; using Sony.Filtr.DeezerAPI; using Sony.Filtr.DeezerAPI.Model; using Sony.Filtr.Playlists; using Sony.Filtr.Playlists.Deezer; using Sony.Filtr.Utility; using Sony.Filtr.Utility.Extensions; namespace Sony.Filtr.Tasks.Tasks.Deezer { public class DeezerImportPlaylistsTask : IScheduledTask { private readonly EditorialPlaylistManager _editorialPlaylistManager; private readonly EditorialPlaylistImportManager _editorialPlaylistImportManager; private readonly DeezerApi _deezerApi; private readonly DeezerPlaylistManager _deezerPlaylistManager; private readonly EditorialPlaylistManagerNew _editorialPlaylistManagerNew; private readonly BuzzAccountManager _buzzAccountManager; private Logger _logger; public DeezerImportPlaylistsTask(EditorialPlaylistManager editorialPlaylistManager, EditorialPlaylistImportManager editorialPlaylistImportManager, DeezerApi deezerApi, DeezerPlaylistManager deezerPlaylistManager, EditorialPlaylistManagerNew editorialPlaylistManagerNew, BuzzAccountManager buzzAccountManager) { _editorialPlaylistManager = editorialPlaylistManager; _editorialPlaylistImportManager = editorialPlaylistImportManager; _deezerApi = deezerApi; _deezerPlaylistManager = deezerPlaylistManager; _editorialPlaylistManagerNew = editorialPlaylistManagerNew; _buzzAccountManager = buzzAccountManager; _logger = NLog.LogManager.GetLogger("DeezerImportPlaylistsTask"); } public async Task ExecuteAsync(Guid scheduledTaskLogId) { var editorialPlaylistImportUsers = _editorialPlaylistImportManager.GetDeezerImportUsers(); var deezerBuzzUsers = await _buzzAccountManager.GetBuzzUsersAsync(musicServiceId: (int)MusicService.Deezer); var deezerBuzzUserIds = deezerBuzzUsers.Select(d => Maybe.ToLongOrDefault(d.Username)).FilterNull(); var allDeezerUserIds = editorialPlaylistImportUsers.Select(u => u.UserId).Union(deezerBuzzUserIds).Distinct().ToList(); _logger.Info("Begin fetching all playlists for {0} users", allDeezerUserIds.Count); var existingPlaylists = _deezerPlaylistManager.GetPlaylists(); var existingPlaylistDic = existingPlaylists.ToDictionary(k => k.DeezerId, v => v); await allDeezerUserIds.ForEachAsync(5, async userId => { try { var playlists = await _deezerApi.GetAllUsersPlaylistsAsync(userId); _logger.Debug("Fetching all playlists for {0}.", userId); if (playlists != null) { var ownPublicPlaylists = playlists.Where(p => !p.is_loved_track && p.creator.id == userId).ToList(); if (ownPublicPlaylists.Any()) { _logger.Debug("Found {0} playlists for user {1}.", ownPublicPlaylists.Count, userId); var mappedPlaylists = ownPublicPlaylists.Select(MapDeezerPlaylist).Where(p => ShouldAddOrUpdate(p, existingPlaylistDic)); await mappedPlaylists.ForEachAsync(5, async playlist => { try { _logger.Debug("Saving {0}.", playlist.DeezerId); _deezerPlaylistManager.AddOrUpdateDeezerPlaylist(playlist); } catch (Exception ex) { _logger.Error(ex, "Could not save playlist!"); } }); } } } catch(Exception ex) { _logger.Error(ex, $"Could not import playlists for user {userId}!"); } }); _logger.Info("Done fetching all playlists for {0} deezer users.", allDeezerUserIds.Count); return null; } private bool ShouldAddOrUpdate(DeezerPlaylist deezerPlaylist, Dictionary existingPlaylistDic) { DeezerPlaylist existingPlaylist; if (existingPlaylistDic.TryGetValue(deezerPlaylist.DeezerId, out existingPlaylist)) { var different = deezerPlaylist.DeezerId != existingPlaylist.DeezerId || deezerPlaylist.Title != existingPlaylist.Title || deezerPlaylist.Description != existingPlaylist.Description || deezerPlaylist.Rating != existingPlaylist.Rating || deezerPlaylist.Link != existingPlaylist.Link || deezerPlaylist.Picture != existingPlaylist.Picture || deezerPlaylist.CreatorId != existingPlaylist.CreatorId || deezerPlaylist.CreatorName != existingPlaylist.CreatorName || deezerPlaylist.Duration != existingPlaylist.Duration || deezerPlaylist.TrackCount != existingPlaylist.TrackCount; return different; } return true; //New playlist } private Contracts.Entities.DeezerPlaylist MapDeezerPlaylist(UserDeezerPlaylist playlist) { return new Contracts.Entities.DeezerPlaylist() { DeezerId = playlist.id, CreatorId = playlist.creator.id, CreatorName = playlist.creator.name, //Description = playlist.description, Duration = playlist.duration, Link = playlist.link, Picture = playlist.picture, Rating = playlist.rating, Title = playlist.title, //TrackCount = playlist.tr }; } } }