2015-10-23 20:25:13 +01:00
|
|
|
|
using SpotifyAPI.Web;
|
|
|
|
|
using SpotifyAPI.Web.Auth;
|
|
|
|
|
using SpotifyAPI.Web.Enums;
|
|
|
|
|
using SpotifyAPI.Web.Models;
|
|
|
|
|
using System;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2016-07-19 20:16:03 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Image = System.Drawing.Image;
|
|
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Example
|
|
|
|
|
{
|
|
|
|
|
public partial class WebControl : UserControl
|
|
|
|
|
{
|
|
|
|
|
private SpotifyWebAPI _spotify;
|
|
|
|
|
|
|
|
|
|
private PrivateProfile _profile;
|
|
|
|
|
private List<FullTrack> _savedTracks;
|
|
|
|
|
private List<SimplePlaylist> _playlists;
|
2015-10-23 20:25:13 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
public WebControl()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
_savedTracks = new List<FullTrack>();
|
2016-07-19 20:16:03 +01:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void InitialSetup()
|
|
|
|
|
{
|
|
|
|
|
if (InvokeRequired)
|
|
|
|
|
{
|
|
|
|
|
Invoke(new Action(InitialSetup));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
authButton.Enabled = false;
|
|
|
|
|
_profile = _spotify.GetPrivateProfile();
|
|
|
|
|
|
|
|
|
|
_savedTracks = GetSavedTracks();
|
|
|
|
|
savedTracksCountLabel.Text = _savedTracks.Count.ToString();
|
|
|
|
|
_savedTracks.ForEach(track => savedTracksListView.Items.Add(new ListViewItem()
|
|
|
|
|
{
|
|
|
|
|
Text = track.Name,
|
2015-10-23 20:25:13 +01:00
|
|
|
|
SubItems = { string.Join(",", track.Artists.Select(source => source.Name)), track.Album.Name }
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
_playlists = GetPlaylists();
|
|
|
|
|
playlistsCountLabel.Text = _playlists.Count.ToString();
|
|
|
|
|
_playlists.ForEach(playlist => playlistsListBox.Items.Add(playlist.Name));
|
|
|
|
|
|
|
|
|
|
displayNameLabel.Text = _profile.DisplayName;
|
|
|
|
|
countryLabel.Text = _profile.Country;
|
|
|
|
|
emailLabel.Text = _profile.Email;
|
|
|
|
|
accountLabel.Text = _profile.Product;
|
|
|
|
|
|
|
|
|
|
if (_profile.Images != null && _profile.Images.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
using (WebClient wc = new WebClient())
|
|
|
|
|
{
|
|
|
|
|
byte[] imageBytes = await wc.DownloadDataTaskAsync(new Uri(_profile.Images[0].Url));
|
|
|
|
|
using (MemoryStream stream = new MemoryStream(imageBytes))
|
|
|
|
|
avatarPictureBox.Image = Image.FromStream(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<FullTrack> GetSavedTracks()
|
|
|
|
|
{
|
|
|
|
|
Paging<SavedTrack> savedTracks = _spotify.GetSavedTracks();
|
|
|
|
|
List<FullTrack> list = savedTracks.Items.Select(track => track.Track).ToList();
|
|
|
|
|
|
|
|
|
|
while (savedTracks.Next != null)
|
|
|
|
|
{
|
|
|
|
|
savedTracks = _spotify.GetSavedTracks(20, savedTracks.Offset + savedTracks.Limit);
|
|
|
|
|
list.AddRange(savedTracks.Items.Select(track => track.Track));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<SimplePlaylist> GetPlaylists()
|
|
|
|
|
{
|
|
|
|
|
Paging<SimplePlaylist> playlists = _spotify.GetUserPlaylists(_profile.Id);
|
|
|
|
|
List<SimplePlaylist> list = playlists.Items.ToList();
|
|
|
|
|
|
|
|
|
|
while (playlists.Next != null)
|
|
|
|
|
{
|
|
|
|
|
playlists = _spotify.GetUserPlaylists(_profile.Id, 20, playlists.Offset + playlists.Limit);
|
|
|
|
|
list.AddRange(playlists.Items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void authButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2016-07-19 20:16:03 +01:00
|
|
|
|
Task.Run(() => RunAuthentication());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void RunAuthentication()
|
|
|
|
|
{
|
2016-07-29 23:19:57 +01:00
|
|
|
|
WebAPIFactory webApiFactory = new WebAPIFactory(
|
2016-07-19 20:16:03 +01:00
|
|
|
|
"http://localhost",
|
|
|
|
|
8000,
|
|
|
|
|
"26d287105e31491889f3cd293d85bfea",
|
|
|
|
|
Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead |
|
2017-03-02 21:28:55 +00:00
|
|
|
|
Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead | Scope.PlaylistReadCollaborative |
|
2017-05-29 22:17:15 +01:00
|
|
|
|
Scope.UserReadRecentlyPlayed | Scope.UserReadPlaybackState | Scope.UserModifyPlaybackState);
|
2016-07-19 20:16:03 +01:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-07-24 22:58:45 +01:00
|
|
|
|
_spotify = await webApiFactory.GetWebApi();
|
2016-07-19 20:16:03 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_spotify == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
InitialSetup();
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-23 20:25:13 +01:00
|
|
|
|
}
|