public void CheckAction(Account user, DateTime lastCheck) { var api = _spotifyService.GetSpotifyWebApi(_spotifyService.GetSpotifyToken(user)); string currentUserId = (_spotifyWrapper.GetSpotifyProfile(_spotifyService.GetSpotifyToken(user)) as SpotifyProfileModel).Id; Paging <SimplePlaylist> playlists = _spotifyService.GetUserPlaylists(api, user); if (playlists == null) { return; } _newTracks = new List <PlaylistTrack>(); for (int i = 0; i < playlists.Items.Count; i++) { Paging <PlaylistTrack> tracks = _spotifyService.GetPlaylistTracks(api, playlists.Items[i].Id, user); for (int j = 0; j < tracks.Items.Count; j++) { if (tracks.Items[j].AddedAt >= lastCheck && tracks.Items[j].AddedBy.Id != currentUserId) { if (_lastTriggerDate == null) { _lastTriggerDate = tracks.Items[j].AddedAt; } else if (_lastTriggerDate < tracks.Items[j].AddedAt) { _lastTriggerDate = tracks.Items[j].AddedAt; } _newTracks.Add(tracks.Items[j]); } } } }
public IViewModel ConnectToAccount(string code) { Console.WriteLine($"SpotifyService(ConnectToAccount): The user code is {code}"); var result = _spotifyWrapper.GenerateSpotifyToken(code, "http://127.0.0.1:8081/spotify/login"); if (!result.Success) { Console.WriteLine("SpotifyService(GetSpotifyToken): Failed to get token"); return(new ErrorViewModel() { Error = (result as RequestFailedModel).Error }); } result = _spotifyWrapper.GetSpotifyProfile(result as SpotifyTokenModel); if (!result.Success) { Console.WriteLine("SpotifyService(GetSpotifyToken): Failed to get profile"); return(new ErrorViewModel() { Error = (result as RequestFailedModel).Error }); } var profile = result as SpotifyProfileModel; byte[] encodedPassword = new UTF8Encoding().GetBytes($"{profile.Id}"); byte[] hash = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(encodedPassword); string encoded = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); var account = _context.Accounts.FirstOrDefault(a => a.UserName.Equals(profile.Id) && a.Password.Equals(encoded)); if (account == null) { account = new Account() { UserName = profile.Id, Password = encoded }; _context.Accounts.Add(account); _context.SaveChanges(); Console.WriteLine($"SpotifyService(GetSpotifyToken): Crated Account({account.Id})"); } Console.WriteLine($"SpotifyService(GetSpotifyToken): Account({account.Id})"); return(_accountService.Login(account)); }