forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebControl.cs
More file actions
136 lines (116 loc) · 4.57 KB
/
WebControl.cs
File metadata and controls
136 lines (116 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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.UserLibrarayRead | 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();
}
}
}