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;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using Image = System.Drawing.Image;
|
|
|
|
|
|
|
|
|
|
namespace SpotifyAPI.Example
|
|
|
|
|
{
|
|
|
|
|
public partial class WebControl : UserControl
|
|
|
|
|
{
|
|
|
|
|
private SpotifyWebAPI _spotify;
|
|
|
|
|
private ImplicitGrantAuth _auth;
|
|
|
|
|
|
|
|
|
|
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>();
|
|
|
|
|
_auth = new ImplicitGrantAuth
|
|
|
|
|
{
|
2015-09-08 14:35:07 +01:00
|
|
|
|
RedirectUri = "http://localhost:8000",
|
2015-07-07 17:11:11 +01:00
|
|
|
|
ClientId = "26d287105e31491889f3cd293d85bfea",
|
2016-03-17 21:58:10 +00:00
|
|
|
|
Scope = Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead | Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate,
|
2015-07-07 17:11:11 +01:00
|
|
|
|
State = "XSS"
|
|
|
|
|
};
|
|
|
|
|
_auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-23 20:25:13 +01:00
|
|
|
|
private void _auth_OnResponseReceivedEvent(Token token, string state)
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
_auth.StopHttpServer();
|
|
|
|
|
|
|
|
|
|
if (state != "XSS")
|
|
|
|
|
{
|
2015-10-28 16:05:09 +00:00
|
|
|
|
MessageBox.Show(@"Wrong state received.", @"SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2015-07-07 17:11:11 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (token.Error != null)
|
|
|
|
|
{
|
2015-10-28 16:05:09 +00:00
|
|
|
|
MessageBox.Show($"Error: {token.Error}", @"SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
2015-07-07 17:11:11 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_spotify = new SpotifyWebAPI
|
|
|
|
|
{
|
|
|
|
|
UseAuth = true,
|
|
|
|
|
AccessToken = token.AccessToken,
|
|
|
|
|
TokenType = token.TokenType
|
|
|
|
|
};
|
|
|
|
|
InitialSetup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2015-09-08 14:35:07 +01:00
|
|
|
|
_auth.StartHttpServer(8000);
|
2015-07-07 17:11:11 +01:00
|
|
|
|
_auth.DoAuth();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-23 20:25:13 +01:00
|
|
|
|
}
|