forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (139 loc) · 5.52 KB
/
Program.cs
File metadata and controls
145 lines (139 loc) · 5.52 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
141
142
143
144
145
using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using SpotifyAPI.SpotifyWebAPI;
using SpotifyAPI.SpotifyWebAPI.Models;
namespace SpotifyWebAPIExample
{
class Program
{
static ImplicitGrantAuth auth;
static void Main(string[] args)
{
Console.WriteLine("### SpotifyWebAPI .NET Test App");
Console.WriteLine("Starting auth process...");
//Create the auth object
auth = new ImplicitGrantAuth()
{
//Your client Id
ClientId = "26d287105e31491889f3cd293d85bfea",
//Set this to localhost if you want to use the built-in HTTP Server
RedirectUri = "http://localhost",
//How many permissions we need?
Scope = Scope.USER_READ_PRIVATE | Scope.USER_READ_EMAIL | Scope.PLAYLIST_READ_PRIVATE | Scope.USER_LIBRARAY_READ | Scope.USER_LIBRARY_MODIFY | Scope.USER_READ_PRIVATE
};
//Start the internal http server
auth.StartHttpServer();
//When we got our response
auth.OnResponseReceivedEvent += auth_OnResponseReceivedEvent;
//Start
auth.DoAuth();
}
static void auth_OnResponseReceivedEvent(Token token, string state, string error)
{
//stop the http server
auth.StopHttpServer();
if(error != null)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: " + error);
return;
}
DisplayMenu(new SpotifyWebAPIClass()
{
AccessToken = token.AccessToken,
TokenType = token.TokenType,
UseAuth = true
});
}
public static void DisplayMenu(SpotifyWebAPIClass spotify)
{
Console.WriteLine("Choose one of the following Tests:");
Console.WriteLine("1 - Display Profile information");
Console.WriteLine("2 - Display all of your playlists");
Console.WriteLine("3 - List all of your playlist-tracks");
Console.Write("Number: ");
String number = Console.ReadLine();
switch (number)
{
default:
DisplayMenu(spotify);
break;
case "1":
DisplayProfile(spotify);
break;
case "2":
DisplayPlaylists(spotify);
break;
case "3":
DisplayPlaylistTracks(spotify);
break;
}
}
public static void DisplayProfile(SpotifyWebAPIClass spotify)
{
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
PrivateProfile profile = spotify.GetPrivateProfile();
Console.WriteLine("Your Display name: " + profile.DisplayName);
Console.WriteLine("Your Country: " + profile.Country);
Console.WriteLine("Your ID: " + profile.Id);
Console.WriteLine("Account product: " + profile.Product);
Console.WriteLine("Your images:");
foreach (Image image in profile.Images)
Console.WriteLine("- " + image.Url);
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
DisplayMenu(spotify);
}
private static void DisplayPlaylists(SpotifyWebAPIClass spotify)
{
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
Paging<SimplePlaylist> playlists = spotify.GetUserPlaylists(spotify.GetPublicProfile().Id);
Console.WriteLine("Printing playlists...");
Console.WriteLine("");
foreach (SimplePlaylist playlist in playlists.Items)
Console.WriteLine(playlist.Name + " (" + playlist.Id + ")");
while(playlists.Next != null)
{
playlists = spotify.DownloadData<Paging<SimplePlaylist>>(playlists.Next);
foreach (SimplePlaylist playlist in playlists.Items)
Console.WriteLine(playlist.Name + " (" + playlist.Id + ")");
}
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
DisplayMenu(spotify);
}
private static void DisplayPlaylistTracks(SpotifyWebAPIClass spotify)
{
Console.WriteLine("");
Console.WriteLine("");
Console.Write("Playlist ID (must be one of yours): ");
String id = Console.ReadLine();
Paging<PlaylistTrack> col = spotify.GetPlaylistTracks(spotify.GetPrivateProfile().Id, id);
if(col.HasError())
{
Console.WriteLine("ERROR: " + col.ErrorResponse.Message);
DisplayMenu(spotify);
return;
}
foreach(PlaylistTrack track in col.Items)
Console.WriteLine(track.Track.Name + " (" + track.Track.Id + ")");
while (col.Next != null)
{
col = spotify.DownloadData<Paging<PlaylistTrack>>(col.Next);
foreach (PlaylistTrack track in col.Items)
Console.WriteLine(track.Track.Name + " (" + track.Track.Id + ")");
}
DisplayMenu(spotify);
}
}
}