mirror of
https://github.com/Sarsoo/Spotify.NET.git
synced 2024-12-26 07:56:26 +00:00
fd8572bf93
Forgot to include this in the last commit
136 lines
4.6 KiB
C#
136 lines
4.6 KiB
C#
using SpotifyAPI.Web;
|
|
using SpotifyAPI.Web.Auth;
|
|
using SpotifyAPI.Web.Enums;
|
|
using SpotifyAPI.Web.Models;
|
|
using System;
|
|
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;
|
|
|
|
public WebControl()
|
|
{
|
|
InitializeComponent();
|
|
|
|
_savedTracks = new List<FullTrack>();
|
|
_auth = new ImplicitGrantAuth
|
|
{
|
|
RedirectUri = "http://localhost:8000",
|
|
ClientId = "26d287105e31491889f3cd293d85bfea",
|
|
Scope = Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead | Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate,
|
|
State = "XSS"
|
|
};
|
|
_auth.OnResponseReceivedEvent += _auth_OnResponseReceivedEvent;
|
|
}
|
|
|
|
private void _auth_OnResponseReceivedEvent(Token token, string state)
|
|
{
|
|
_auth.StopHttpServer();
|
|
|
|
if (state != "XSS")
|
|
{
|
|
MessageBox.Show(@"Wrong state received.", @"SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return;
|
|
}
|
|
if (token.Error != null)
|
|
{
|
|
MessageBox.Show($"Error: {token.Error}", @"SpotifyWeb API Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
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,
|
|
SubItems = { string.Join(",", track.Artists.Select(source => source.Name)), track.Album.Name }
|
|
}));
|
|
|
|
_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)
|
|
{
|
|
_auth.StartHttpServer(8000);
|
|
_auth.DoAuth();
|
|
}
|
|
}
|
|
} |