private void GetTracks() { if (SPOTIFY_INST == null) { SPOTIFY_INST = MySpotify.GetInstanceAuth(); stopwatch.Start(); } else { if (stopwatch.ElapsedMilliseconds > (55 * 60 * 1000)) { SPOTIFY_INST.AccessToken = MySpotify.RefreshToken().AccessToken; stopwatch.Reset(); } } spotify = SPOTIFY_INST; playlist = spotify.GetPlaylistTracks(MYSPOTIFY_NAME, dgvPlaylists.CurrentRow.Cells[1].Value.ToString()); dgvTracksOnList.Rows.Clear(); for (int c = 0; c < playlist.Items.Count; c++) { fullTrack = playlist.Items[c].Track; dgvTracksOnList.Rows.Add(fullTrack.Name, fullTrack.Artists[0].Name, fullTrack.Id, fullTrack.Uri, fullTrack.DurationMs ); } }
private void CheckToken() { if (SPOTIFY_INST == null) { SPOTIFY_INST = MySpotify.GetInstanceAuth(); TOKEN_TIMER.Restart(); } else { if (TOKEN_TIMER.ElapsedMilliseconds > (tokenElapseTime * 60 * 1000)) { //SPOTIFY_INST.AccessToken = MySpotify.RefreshToken().AccessToken; MySpotify.RefreshToken(); TOKEN_TIMER.Restart(); } } spotify = SPOTIFY_INST; }
private void UKUpdate() { CheckToken(); int start = 0, end = 0; if (txtUKStart.Text == "") { start = 0; } else if (!int.TryParse(txtUKStart.Text, out start)) { MessageBox.Show("Start is a number only field"); return; } if (txtUKEnd.Text == "") { end = 0; } else if (!int.TryParse(txtUKEnd.Text, out end)) { MessageBox.Show("End is a number only field"); return; } if (start > 0 && end > 0) { //Clear existing list //Get playlist tracks var playlist = spotify.GetPlaylist(MYSPOTIFY_NAME, CHARTS_PLAYLIST); Paging <PlaylistTrack> deleteTracks = spotify.GetPlaylistTracks(MYSPOTIFY_NAME, CHARTS_PLAYLIST); List <DeleteTrackUri> deleteList = new List <DeleteTrackUri>(); foreach (PlaylistTrack t in deleteTracks.Items) { deleteList.Add(new DeleteTrackUri(t.Track.Uri)); } ErrorResponse doDelete = spotify.RemovePlaylistTracks(MYSPOTIFY_NAME, CHARTS_PLAYLIST, deleteList); //Add new tracks OfficialChartsEntities db = new OfficialChartsEntities(); var tracksToRecord = from t in db.Record_Spotify //where t.TrackTitle.Contains("have you ever been lonely") orderby t.ID select t; List <Record_Spotify> lTracks = tracksToRecord.ToList(); int perCycle = 100; int cycles = (end - start + 1) / perCycle; for (int cycle = 0; cycle < cycles; cycle++) { string uris = "{\"uris\":["; for (int t = 0; t < perCycle; t++) { int trackNo = start + cycle * perCycle; if (uris != "{\"uris\":[") { uris += ","; } string id = MySpotify.IdFromUrl(lTracks[t].SpotifyURL); uris += ("\"spotify:track:" + id + "\""); } uris += "]}"; string url = "https://api.spotify.com/v1/playlists/2ps7zHd9of81uPSCNZxWEv/tracks"; HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url); myReq.Headers["Authorization"] = "Bearer " + spotify.AccessToken; myReq.ContentType = "application/json"; myReq.Method = "POST"; using (var streamWriter = new StreamWriter(myReq.GetRequestStream())) { streamWriter.Write(uris); streamWriter.Flush(); streamWriter.Close(); } WebResponse wr = myReq.GetResponse(); wr.Dispose(); } //ErrorResponse addTracks = spotify.AddPlaylistTracks(mySpotifyName, "2ps7zHd9of81uPSCNZxWEv", tracksToAdd); //if (addTracks.HasError()) //{ // string debug = addTracks.Error.Message; //} GetPlayLists(); } }