public bool Execute(Account user, object result, string args) { var api = _spotifyService.GetSpotifyWebApi(_spotifyService.GetSpotifyToken(user)); var tracks = result as List <SimpleTrack>; var playlists = _spotifyService.GetUserPlaylists(api, user); if (playlists == null || tracks == null) { return(false); } for (int i = 0; i < playlists.Items.Count; i++) { if (playlists.Items[i].Name == "AreaPlaylist") { Console.WriteLine("AreaPlaylist already exists, adding new track to it"); _spotifyService.AddTracksToPlaylist(api, tracks, playlists.Items[i].Id, user); return(true); } } FullPlaylist playlist = _spotifyService.CreatePlaylist(api, "AreaPlaylist", user); if (playlist != null) { Console.WriteLine("AreayPlaylist created and tracks added"); _spotifyService.AddTracksToPlaylist(api, tracks, playlist.Id, user); return(true); } Console.WriteLine("AreaPlaylist not found even after creating it"); return(false); }
/// <summary> /// Save the new playlist to the users spotify account /// </summary> /// <returns></returns> public async Task SavePlaylist() { var playlist = await SpotifyService.CreatePlaylist(AuthenticationService.User, AuthenticationService.AuthenticationToken, "Favorite Artists + Top Songs " + DateTime.Now.ToString(), "Created with Randify!", isPublic : true); try { var tracks = new List <Track>(); for (int i = 0; i < Tracks.Count; i++) { if (!string.IsNullOrWhiteSpace(Tracks[i].Id)) { tracks.Add(Tracks[i]); } if (i % 100 == 0) { await SpotifyService.AddTracksToPlaylist(AuthenticationService.User, AuthenticationService.AuthenticationToken, playlist, tracks); tracks.Clear(); } } await SpotifyService.AddTracksToPlaylist(AuthenticationService.User, AuthenticationService.AuthenticationToken, playlist, tracks); } catch (Exception ex) { PageException = ex; } ShowPlaylistSaved = true; StateHasChanged(); }
public async Task <ActionResult <Room> > AddSpotifyAuthToRoom(int roomId, string spotifyAuth) { try { var room = await _context.Rooms .SingleOrDefaultAsync(s => s.Id == roomId); if (room == null) { return(NotFound()); } if (room.SpotifyPlaylist == null) { var service = new SpotifyService(spotifyAuth); var ownerId = await service.GetUserIdAsync(); var playlist = await service.CreatePlaylist(room.Name, ownerId); room.SpotifyPlaylist = (SpotifyPlaylist)playlist; room.IsSpotifyEnabled = true; await _context.SaveChangesAsync(); } return(room); } catch (Exception ex) { throw new SystemWeb.HttpResponseException(HttpStatusCode.InternalServerError); } }
public async Task <IActionResult> SpotifyAuthorized(string code, string state) { if (state.Contains("-sync")) { var option = new CookieOptions(); option.Expires = DateTime.Now.AddDays(1); Request.Cookies.TryGetValue("jwtToken", out string jwtToken); jwtToken = await _tokenService .AddClaimToToken( jwtToken, "spotifyAuth", (await new UserAccountsService(new HttpClient(), _config).RequestAccessRefreshToken(code)).AccessToken); Response.Cookies.Append("jwtToken", jwtToken, option); var roomId = state.TrimEnd("-sync".ToCharArray()); return(RedirectToAction("Index", "Room", routeValues: new { Id = roomId })); } var refreshToken = (await new UserAccountsService(new HttpClient(), _config).RequestAccessRefreshToken(code)).AccessToken; var room = await _roomDataStore.GetItemAsync(state); room.SpotifyAuthCode = refreshToken; var request = HttpContext.Request; var roomUrl = $"{request.Scheme}://{request.Host}/Room/Index/{room.Id}"; var spotify = new SpotifyService(refreshToken); var spotifyUserId = await spotify.GetUserIdAsync(); var updateRoomTask = _roomDataStore.UpdateItemAsync(room); var createSpotifyRoomTask = spotify.CreatePlaylist(room.Name, spotifyUserId, roomUrl); await Task.WhenAll(updateRoomTask, createSpotifyRoomTask); var playlist = (SpotifyPlaylist)createSpotifyRoomTask.Result; playlist.Room = room; await _spotifyPlaylistsStore.AddItemAsync(playlist); return(RedirectToAction("Index", "Room", routeValues: new { Id = state })); }