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
70 lines (57 loc) · 2.3 KB
/
Program.cs
File metadata and controls
70 lines (57 loc) · 2.3 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
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
using System.Threading.Tasks;
namespace SpotifyAPI.Web.Examples.CLI;
internal static class Program
{
private static string _clientId = "";
private static string _secretId = "";
public static void Main()
{
_clientId = string.IsNullOrEmpty(_clientId)
? Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID")
: _clientId;
_secretId = string.IsNullOrEmpty(_secretId)
? Environment.GetEnvironmentVariable("SPOTIFY_SECRET_ID")
: _secretId;
Console.WriteLine("####### Spotify API Example #######");
Console.WriteLine("This example uses AuthorizationCodeAuth.");
Console.WriteLine(
"Tip: If you want to supply your ClientID and SecretId beforehand, use env variables (SPOTIFY_CLIENT_ID and SPOTIFY_SECRET_ID)");
var auth =
new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:80", "http://localhost:80",
Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
auth.AuthReceived += AuthOnAuthReceived;
auth.Start();
auth.OpenBrowser();
Console.ReadLine();
auth.Stop(0);
}
private static async void AuthOnAuthReceived(object sender, Token token)
{
var auth = (AuthorizationCodeAuth)sender;
auth.Stop();
var api = new SpotifyWebAPI
{
Token = token
};
await PrintUsefulData(api);
}
private static async Task PrintAllPlaylistTracks(SpotifyWebAPI api, Paging<SimplePlaylist> playlists)
{
if (playlists.Items == null) return;
playlists.Items.ForEach(playlist => Console.WriteLine($"- {playlist.Name}"));
if (playlists.HasNextPage())
await PrintAllPlaylistTracks(api, await api.GetNextPageAsync(playlists));
}
private static async Task PrintUsefulData(SpotifyWebAPI api)
{
var profile = await api.GetPrivateProfileAsync();
var name = string.IsNullOrEmpty(profile.DisplayName) ? profile.Id : profile.DisplayName;
Console.WriteLine($"Hello there, {name}!");
Console.WriteLine("Your playlists:");
await PrintAllPlaylistTracks(api, api.GetUserPlaylists(profile.Id));
}
}