Class: Tidal::ClientV1

Inherits:
Object
  • Object
show all
Defined in:
lib/tidal/client_v1.rb

Constant Summary collapse

BASE_URL =
'https://openapi.tidal.com/'.freeze
TOKEN_URL =
'https://auth.tidal.com/v1/oauth2/token'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ ClientV1

Initialize the Tidal client

Parameters:

  • client_id (String)

    The client ID for the Tidal API

  • client_secret (String)

    The client secret for the Tidal API



15
16
17
18
19
20
# File 'lib/tidal/client_v1.rb', line 15

def initialize(client_id, client_secret)
  @client_id = client_id
  @client_secret = client_secret
  @client = OAuth2::Client.new(@client_id, @client_secret, site: BASE_URL, token_url: TOKEN_URL)
  @access_token = @client.client_credentials.get_token.token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



9
10
11
# File 'lib/tidal/client_v1.rb', line 9

def access_token
  @access_token
end

Instance Method Details

#get_album_items(album_id, country_code, offset = nil, limit = nil) ⇒ Array

Get album items

Parameters:

  • album_id (String)

    The album ID

  • country_code (String)

    The country code

  • offset (Integer, nil) (defaults to: nil)

    The offset for pagination

  • limit (Integer, nil) (defaults to: nil)

    The limit for pagination

Returns:

  • (Array)

    The list of album items



57
58
59
60
61
62
63
64
65
66
# File 'lib/tidal/client_v1.rb', line 57

def get_album_items(album_id, country_code, offset = nil, limit = nil)
  uri = URI("#{BASE_URL}/albums/#{album_id}/items")
  params = { countryCode: country_code }
  params[:offset] = offset if offset
  params[:limit] = limit if limit
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_albums_by_artist(artist_id, country_code, offset = nil, limit = nil) ⇒ Array

Get albums by artist

Parameters:

  • artist_id (String)

    The artist ID

  • country_code (String)

    The country code

  • offset (Integer, nil) (defaults to: nil)

    The offset for pagination

  • limit (Integer, nil) (defaults to: nil)

    The limit for pagination

Returns:

  • (Array)

    The list of albums by the artist



107
108
109
110
111
112
113
114
115
116
# File 'lib/tidal/client_v1.rb', line 107

def get_albums_by_artist(artist_id, country_code, offset = nil, limit = nil)
  uri = URI("#{BASE_URL}/artists/#{artist_id}/albums")
  params = { countryCode: country_code }
  params[:offset] = offset if offset
  params[:limit] = limit if limit
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_albums_by_barcode_id(barcode_id, country_code) ⇒ Array

Get albums by barcode ID

Parameters:

  • barcode_id (String)

    The barcode ID

  • country_code (String)

    The country code

Returns:

  • (Array)

    The list of albums



27
28
29
30
31
32
33
34
# File 'lib/tidal/client_v1.rb', line 27

def get_albums_by_barcode_id(barcode_id, country_code)
  uri = URI("#{BASE_URL}/albums/byBarcodeId")
  params = { barcodeId: barcode_id, countryCode: country_code }
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_multiple_albums(ids, country_code) ⇒ Array

Get multiple albums

Parameters:

  • ids (Array)

    The array of album IDs

  • country_code (String)

    The country code

Returns:

  • (Array)

    The list of albums



41
42
43
44
45
46
47
48
# File 'lib/tidal/client_v1.rb', line 41

def get_multiple_albums(ids, country_code)
  uri = URI("#{BASE_URL}/albums/byIds")
  params = { ids: ids.join(','), countryCode: country_code }
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_multiple_artists(ids, country_code) ⇒ Array

Get multiple artists

Parameters:

  • ids (Array)

    The array of artist IDs

  • country_code (String)

    The country code

Returns:

  • (Array)

    The list of artists



123
124
125
126
127
128
129
130
# File 'lib/tidal/client_v1.rb', line 123

def get_multiple_artists(ids, country_code)
  uri = URI("#{BASE_URL}/artists")
  params = { ids: ids.join(','), countryCode: country_code }
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_multiple_tracks(ids, country_code) ⇒ Array

Get multiple tracks

Parameters:

  • ids (Array)

    The array of track IDs

  • country_code (String)

    The country code

Returns:

  • (Array)

    The list of tracks



189
190
191
192
193
194
195
196
# File 'lib/tidal/client_v1.rb', line 189

def get_multiple_tracks(ids, country_code)
  uri = URI("#{BASE_URL}/tracks")
  params = { ids: ids.join(','), countryCode: country_code }
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_multiple_videos(ids, country_code) ⇒ Array

Get multiple videos

Parameters:

  • ids (Array)

    The array of video IDs

  • country_code (String)

    The country code

Returns:

  • (Array)

    The list of videos



253
254
255
256
257
258
259
260
# File 'lib/tidal/client_v1.rb', line 253

def get_multiple_videos(ids, country_code)
  uri = URI("#{BASE_URL}/videos")
  params = { ids: ids.join(','), countryCode: country_code }
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_search(query, country_code, type = nil, popularity = nil, offset = nil, limit = nil) ⇒ Hash

Search for content

Parameters:

  • query (String)

    The search query

  • country_code (String)

    The country code

  • type (String, nil) (defaults to: nil)

    The type of content to search for

  • popularity (String, nil) (defaults to: nil)

    The popularity filter

  • offset (Integer, nil) (defaults to: nil)

    The offset for pagination

  • limit (Integer, nil) (defaults to: nil)

    The limit for pagination

Returns:

  • (Hash)

    The search results



285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/tidal/client_v1.rb', line 285

def get_search(query, country_code, type = nil, popularity = nil, offset = nil, limit = nil)
  uri = URI("#{BASE_URL}/search")
  params = { query: query, countryCode: country_code }
  params[:type] = type if type
  params[:popularity] = popularity if popularity
  params[:offset] = offset if offset
  params[:limit] = limit if limit
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_similar_albums(album_id, country_code, offset = nil, limit = nil) ⇒ Array

Get similar albums

Parameters:

  • album_id (String)

    The album ID

  • country_code (String)

    The country code

  • offset (Integer, nil) (defaults to: nil)

    The offset for pagination

  • limit (Integer, nil) (defaults to: nil)

    The limit for pagination

Returns:

  • (Array)

    The list of similar albums



75
76
77
78
79
80
81
82
83
84
# File 'lib/tidal/client_v1.rb', line 75

def get_similar_albums(album_id, country_code, offset = nil, limit = nil)
  uri = URI("#{BASE_URL}/albums/#{album_id}/similar")
  params = { countryCode: country_code }
  params[:offset] = offset if offset
  params[:limit] = limit if limit
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_similar_artists(artist_id, country_code, offset = nil, limit = nil) ⇒ Array

Get similar artists

Parameters:

  • artist_id (String)

    The artist ID

  • country_code (String)

    The country code

  • offset (Integer, nil) (defaults to: nil)

    The offset for pagination

  • limit (Integer, nil) (defaults to: nil)

    The limit for pagination

Returns:

  • (Array)

    The list of similar artists



139
140
141
142
143
144
145
146
147
148
# File 'lib/tidal/client_v1.rb', line 139

def get_similar_artists(artist_id, country_code, offset = nil, limit = nil)
  uri = URI("#{BASE_URL}/artists/#{artist_id}/similar")
  params = { countryCode: country_code }
  params[:offset] = offset if offset
  params[:limit] = limit if limit
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_similar_tracks(track_id, country_code, offset = nil, limit = nil) ⇒ Array

Get similar tracks

Parameters:

  • track_id (String)

    The track ID

  • country_code (String)

    The country code

  • offset (Integer, nil) (defaults to: nil)

    The offset for pagination

  • limit (Integer, nil) (defaults to: nil)

    The limit for pagination

Returns:

  • (Array)

    The list of similar tracks



237
238
239
240
241
242
243
244
245
246
# File 'lib/tidal/client_v1.rb', line 237

def get_similar_tracks(track_id, country_code, offset = nil, limit = nil)
  uri = URI("#{BASE_URL}/tracks/#{track_id}/similar")
  params = { countryCode: country_code }
  params[:offset] = offset if offset
  params[:limit] = limit if limit
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_single_album(album_id, country_code) ⇒ Hash

Get a single album

Parameters:

  • album_id (String)

    The album ID

  • country_code (String)

    The country code

Returns:

  • (Hash)

    The album data



91
92
93
94
95
96
97
98
# File 'lib/tidal/client_v1.rb', line 91

def get_single_album(album_id, country_code)
  uri = URI("#{BASE_URL}/albums/#{album_id}")
  params = { countryCode: country_code }
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_single_artist(artist_id, country_code) ⇒ Hash

Get a single artist

Parameters:

  • artist_id (String)

    The artist ID

  • country_code (String)

    The country code

Returns:

  • (Hash)

    The artist data



155
156
157
158
159
160
161
162
# File 'lib/tidal/client_v1.rb', line 155

def get_single_artist(artist_id, country_code)
  uri = URI("#{BASE_URL}/artists/#{artist_id}")
  params = { countryCode: country_code }
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_single_track(artist_id, country_code) ⇒ Hash

Get a single track

Parameters:

  • artist_id (String)

    The artist ID

  • country_code (String)

    The country code

Returns:

  • (Hash)

    The track data



221
222
223
224
225
226
227
228
# File 'lib/tidal/client_v1.rb', line 221

def get_single_track(artist_id, country_code)
  uri = URI("#{BASE_URL}/tracks/#{artist_id}")
  params = { countryCode: country_code }
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_single_video(video_id, country_code) ⇒ Hash

Get a single video

Parameters:

  • video_id (String)

    The video ID

  • country_code (String)

    The country code

Returns:

  • (Hash)

    The video data



267
268
269
270
271
272
273
274
# File 'lib/tidal/client_v1.rb', line 267

def get_single_video(video_id, country_code)
  uri = URI("#{BASE_URL}/videos/#{video_id}")
  params = { countryCode: country_code }
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_tracks_by_artist(artist_id, country_code, collapse_by = nil, offset = nil, limit = nil) ⇒ Array

Get tracks by artist

Parameters:

  • artist_id (String)

    The artist ID

  • country_code (String)

    The country code

  • collapse_by (String, nil) (defaults to: nil)

    The collapse criteria

  • offset (Integer, nil) (defaults to: nil)

    The offset for pagination

  • limit (Integer, nil) (defaults to: nil)

    The limit for pagination

Returns:

  • (Array)

    The list of tracks by the artist



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/tidal/client_v1.rb', line 172

def get_tracks_by_artist(artist_id, country_code, collapse_by = nil, offset = nil, limit = nil)
  uri = URI("#{BASE_URL}/artists/#{artist_id}/tracks")
  params = { countryCode: country_code }
  params[:collapseBy] = collapse_by if collapse_by
  params[:offset] = offset if offset
  params[:limit] = limit if limit
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end

#get_tracks_by_isrc(isrc, country_code, offset = nil, limit = nil) ⇒ Array

Get tracks by ISRC

Parameters:

  • isrc (String)

    The ISRC code

  • country_code (String)

    The country code

  • offset (Integer, nil) (defaults to: nil)

    The offset for pagination

  • limit (Integer, nil) (defaults to: nil)

    The limit for pagination

Returns:

  • (Array)

    The list of tracks by ISRC



205
206
207
208
209
210
211
212
213
214
# File 'lib/tidal/client_v1.rb', line 205

def get_tracks_by_isrc(isrc, country_code, offset = nil, limit = nil)
  uri = URI("#{BASE_URL}/tracks/byIsrc")
  params = { isrc: isrc, countryCode: country_code }
  params[:offset] = offset if offset
  params[:limit] = limit if limit
  uri.query = URI.encode_www_form(params)

  response = @client.request(:get, uri.to_s, headers: default_headers)
  JSON.parse(response.body)
end