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
140 lines (117 loc) · 4.63 KB
/
WebControl.cs
File metadata and controls
140 lines (117 loc) · 4.63 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
137
138
139
140
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.Threading.Tasks;
using System.Windows.Forms;
using Image = System.Drawing.Image;
namespace SpotifyAPI.Example
{
public partial class WebControl : UserControl
{
private readonly ProxyConfig _proxyConfig;
private SpotifyWebAPI _spotify;
private PrivateProfile _profile;
private List<FullTrack> _savedTracks;
private List<SimplePlaylist> _playlists;
public WebControl()
{
InitializeComponent();
_proxyConfig = new ProxyConfig();
_savedTracks = new List<FullTrack>();
}
private async void InitialSetup()
{
if (InvokeRequired)
{
Invoke(new Action(InitialSetup));
return;
}
authButton.Enabled = false;
_profile = await _spotify.GetPrivateProfileAsync();
_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)
{
Task.Run(() => RunAuthentication());
}
private async void RunAuthentication()
{
WebAPIFactory webApiFactory = new WebAPIFactory(
"http://localhost",
8000,
"26d287105e31491889f3cd293d85bfea",
Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead |
Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead | Scope.PlaylistReadCollaborative |
Scope.UserReadRecentlyPlayed | Scope.UserReadPlaybackState | Scope.UserModifyPlaybackState,
_proxyConfig);
try
{
_spotify = await webApiFactory.GetWebApi();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (_spotify == null)
return;
InitialSetup();
}
private void applyProxyBtn_Click(object sender, EventArgs e)
{
_proxyConfig.Host = proxyHostTextBox.Text;
_proxyConfig.Port = (int)proxyPortUpDown.Value;
_proxyConfig.Username = proxyUsernameTextBox.Text;
_proxyConfig.Password = proxyPasswordTextBox.Text;
}
}
}