-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.swift
More file actions
165 lines (140 loc) · 4.91 KB
/
main.swift
File metadata and controls
165 lines (140 loc) · 4.91 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import Foundation
import Combine
import SpotifyWebAPI
var cancellables: Set<AnyCancellable> = []
let dispatchGroup = DispatchGroup()
// Retrieve the client id and client secret from the environment variables.
let spotifyCredentials = getSpotifyCredentialsFromEnvironment()
let spotifyAPI = SpotifyAPI(
authorizationManager: ClientCredentialsFlowManager(
clientId: spotifyCredentials.clientId,
clientSecret: spotifyCredentials.clientSecret
)
)
// Authorize the application.
try spotifyAPI.authorizationManager.waitUntilAuthorized()
// MARK: - The Application is Now Authorized -
// MARK: Search for Tracks and Albums
dispatchGroup.enter()
spotifyAPI.search(
query: "The Beatles",
categories: [.track, .album],
market: "US"
)
.sink(
receiveCompletion: { completion in
print("completion:", completion, terminator: "\n\n\n")
dispatchGroup.leave()
},
receiveValue: { results in
print("\nReceived results for search for 'The Beatles'")
let tracks = results.tracks?.items ?? []
print("found \(tracks.count) tracks:")
print("------------------------")
for track in tracks {
print("\(track.name) - \(track.album?.name ?? "nil")")
}
let albums = results.albums?.items ?? []
print("\nfound \(albums.count) albums:")
print("------------------------")
for album in albums {
print("\(album.name)")
}
}
)
.store(in: &cancellables)
dispatchGroup.wait()
// MARK: Retrieve a Playlist
// "This is Jimi Hendrix"
// https://open.spotify.com/playlist/37i9dQZF1DWTNV753no4ic
let playlistURI = "spotify:playlist:37i9dQZF1DWTNV753no4ic"
dispatchGroup.enter()
spotifyAPI.playlist(playlistURI, market: "US")
.sink(
receiveCompletion: { completion in
print("completion:", completion, terminator: "\n\n\n")
dispatchGroup.leave()
},
receiveValue: { playlist in
print("\nReceived Playlist")
print("------------------------")
print("name:", playlist.name)
print("link:", playlist.externalURLs?["spotify"] ?? "nil")
print("description:", playlist.description ?? "nil")
print("total tracks:", playlist.items.total)
for track in playlist.items.items.compactMap(\.item) {
print(track.name)
}
}
)
.store(in: &cancellables)
dispatchGroup.wait()
// MARK: Retrieve all the Episodes in a Show
// "The Joe Rogan Experience"
// https://open.spotify.com/show/4rOoJ6Egrf8K2IrywzwOMk
let showURI = "spotify:show:4rOoJ6Egrf8K2IrywzwOMk"
dispatchGroup.enter()
spotifyAPI.showEpisodes(showURI, market: "US", limit: 10)
// Retrieve additional pages of results. In this case,
// a total of three pages will be retrieved.
.extendPages(spotifyAPI, maxExtraPages: 2)
.sink(
receiveCompletion: { completion in
print("completion:", completion, terminator: "\n\n\n")
dispatchGroup.leave()
},
receiveValue: { episodes in
let currentPage = (episodes.offset / 10) + 1
if currentPage == 1 {
print("Received Show Episodes For The Joe Rogan Experience")
}
print("page \(currentPage) of results:")
print("------------------------")
for episode in episodes.items {
print(episode.name)
}
print()
}
)
.store(in: &cancellables)
dispatchGroup.wait()
// MARK: Retrieve New Album Releases
dispatchGroup.enter()
spotifyAPI.newAlbumReleases(country: "US")
.sink(
receiveCompletion: { completion in
print("completion:", completion, terminator: "\n\n\n")
dispatchGroup.leave()
},
receiveValue: { newAlbumReleases in
print("\nReceive New Album Rleases")
print("------------------------")
print("message:", newAlbumReleases.message ?? "nil")
for album in newAlbumReleases.albums.items {
print("\(album.name) - \(album.artists?.first?.name ?? "nil")")
}
}
)
.store(in: &cancellables)
dispatchGroup.wait()
// MARK: Artist Top Tracks
// "Cream"
// https://open.spotify.com/artist/74oJ4qxwOZvX6oSsu1DGnw
let artistURI = "spotify:artist:74oJ4qxwOZvX6oSsu1DGnw"
dispatchGroup.enter()
spotifyAPI.artistTopTracks(artistURI, country: "US")
.sink(
receiveCompletion: { completion in
print("completion:", completion, terminator: "\n\n\n")
dispatchGroup.leave()
},
receiveValue: { tracks in
print("\nReceived top tracks for Cream:")
print("------------------------")
for track in tracks {
print("\(track.name) - \(track.album?.name ?? "nil")")
}
}
)
.store(in: &cancellables)
dispatchGroup.wait()