using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using NLog; using Sony.Filtr.Buzz; using Sony.Filtr.Contracts.Definitions; using Sony.Filtr.Contracts.Entities; using Sony.Filtr.DeezerAPI; using Sony.Filtr.SpotifyWebAPI; namespace Sony.Filtr.Worker.Onetime { public class IdentifyWrongBuzzUsers { private readonly BuzzAccountManager _buzzAccountManager; private readonly SpotifyWebApi _spotifyWebApi; private readonly DeezerApi _deezerApi; private Logger _logger; public IdentifyWrongBuzzUsers(BuzzAccountManager buzzAccountManager, SpotifyWebApi spotifyWebApi, DeezerApi deezerApi) { _buzzAccountManager = buzzAccountManager; _spotifyWebApi = spotifyWebApi; _deezerApi = deezerApi; _logger = NLog.LogManager.GetLogger("IdentifyWrongBuzzUsers"); } public async Task ExecuteAsync() { List failed = new List(); var allBuzzUsers = await _buzzAccountManager.GetBuzzUsersAsync(); foreach(var spotifyUser in allBuzzUsers.Where(b => b.ServiceType == ServiceType.Spotify).ToList()) { try { var parsedLink = SpotifyLink.ParseUserLink(spotifyUser.Username); var user = await _spotifyWebApi.GetUserAsync(parsedLink.ExtractSpotifyUserName()); if (user == null) failed.Add($"{spotifyUser.Username} got 404"); } catch(Exception ex) { failed.Add($"{spotifyUser.Username} got error"); } } foreach (var deezerUser in allBuzzUsers.Where(b => b.ServiceType == ServiceType.Deezer).ToList()) { try { var user = await _deezerApi.GetUserAsync(int.Parse(deezerUser.Username)); if (user == null) failed.Add($"{deezerUser.Username} got 404"); } catch (Exception ex) { failed.Add($"{deezerUser.Username} got error"); } } var file = new FileInfo("IdentifyWrongBuzzUsers.txt"); using (var write = file.CreateText()) { foreach(var fail in failed) { await write.WriteLineAsync(fail); } } } } }