Skip to content

AlbertoAlmuinha/DSpoty

Repository files navigation

DSpoty

CRAN status Lifecycle: stable Build Status Total Downloads

R Package to Get Multiple Information from Spotify API.

Installation

You can install the package from the CRAN repository:

install.packages("DSpoty")

or try the development version from GitHub:

devtools::install_github('https://github.com/AlbertoAlmuinha/DSpoty')

Authentication


You need to get your Spotify API Client ID and Secret ID. You can get yours here . The best way to authenticate is to set your ID’s to system environment variables (client_id and secret_id). If you prefer, you can use your ID’s en each function to call the function get_spotify_access_token, but it’s not recomendable.

Sys.setenv(client_id = 'client_id')
Sys.setenv(client_secret = 'client_secret')

access_token <- DSpoty::get_spotify_access_token()

Usage



Get Artist Information


You can get an artist Spotify information with the function get_artists. This function returns data like the name of the artist, links to open in spotify, popularity, number of followers or musical genre.

 

artist <- get_artist('Arctic Monkeys')

artist_name

artist_uri

artist_img

num_followers

spotify_url

popularity

musical_genre

Arctic Monkeys

7Ln80lUS6He07XvHI8qqHH

https://i.scdn.co/image/ed0552e9746ed2bbf04ae4bcb5525700ca31522d

8780891

https://open.spotify.com/artist/7Ln80lUS6He07XvHI8qqHH

86

garage_rock-modern_rock-permanent_wave-rock-sheffield_indie

 

Another option is to search artists by musical genre. You can choose the number of artists returned and the year. The information returned is the same that with get_artists function.

artists_by_genre <- get_artists_by_genre('rock', limit = 20, year = 2019)

 

artist_name

artist_uri

artist_img

num_followers

spotify_url

popularity

musical_genre

Queen

1dfeR4HaWDbWqFHLkxsg1d

https://i.scdn.co/image/b040846ceba13c3e9c125d68389491094e7f2982

22469906

https://open.spotify.com/artist/1dfeR4HaWDbWqFHLkxsg1d

93

glam_rock-rock

The Beatles

3WrFJ7ztbogyGnTHbHJFl2

https://i.scdn.co/image/6b2a709752ef9c7aaf0d270344157f6cd2e0f1a7

14688077

https://open.spotify.com/artist/3WrFJ7ztbogyGnTHbHJFl2

91

british_invasion-merseybeat-psychedelic_rock-rock

Twenty One Pilots

3YQKmKGau1PzlVlkL1iodx

https://i.scdn.co/image/19e8f3bc875b7a4b9cf0041a5ee696c4be5478aa

15682408

https://open.spotify.com/artist/3YQKmKGau1PzlVlkL1iodx

88

modern_rock-rock

Maroon 5

04gDigrS5kc9YWfZHwBETP

https://i.scdn.co/image/608c7b23420c9556a7eabd9097f7e171a91d3871

21450933

https://open.spotify.com/artist/04gDigrS5kc9YWfZHwBETP

94

pop-pop_rock

Imagine Dragons

53XhwfbYqKCa1cC15pYq2q

https://i.scdn.co/image/01b36ca0f45f2f15117022a2754287a6ca1acdcc

24260851

https://open.spotify.com/artist/53XhwfbYqKCa1cC15pYq2q

92

modern_rock

 

Finally, you can search related artists. In this case, you need the artist ID (and not the name) to use the function. You can get this information from the function get_artists or artists_by_genre. You will get the top 20 related artist.

related_artists<-get_related_artists(artist_uri = artist$artist_uri)

 

artist_name

artist_uri

popularity

url_img

The Kooks

1GLtl8uqKmnyCWxHmw9tL4

73

https://i.scdn.co/image/0a3dfed5bce0e6d14703011ec52a42ae719c0d4b

Alex Turner

1ctkBmvz80MGyi72Ix055S

66

https://i.scdn.co/image/ab67616d0000b273672951d44a35cb08092f2c1f

The Last Shadow Puppets

2Z7UcsdweVlRbAk5wH5fsf

65

https://i.scdn.co/image/1e721c9502da1663fe9dd0b9f8a90519546f0a9f

Blossoms

22RISwgVJyZu9lpqAcv1F5

66

https://i.scdn.co/image/b0722b13be43495b793d3911ef90403d62eb5d7b

Kasabian

11wRdbnoYqRddKBrpHt4Ue

69

https://i.scdn.co/image/8ef02a528dfa5496a410aa80e8b0316574ca95b7


Get Albums Information


You can get the whole discography of an artist in few seconds with the function get_artists_albums. You must select one of the next options to the parameter album_type (default option is album):

  • album
  • single
  • appears_on
  • compilation

 

albums <- get_artist_albums('Arctic Monkeys')

albums <- get_artist_albums('Arctic Monkeys', album_type = 'appears_on', limit = 20)

 

album_uri

album_name

rel_album_artista

album_type

album_img

album_url

release_date

release_date_precision

total_tracks

available_markets

popularity

1nXwgYOolvutEaVWfGbP4w

Isles Of Wonder: Music For The Opening Ceremony Of The London 2012 Olympic Games

appears_on

compilation

https://i.scdn.co/image/852299f998fe2b5c1e34450600c5bc8e4d432eef

https://open.spotify.com/album/1nXwgYOolvutEaVWfGbP4w

2012-01-01

day

36

CA

14

1GvWtv7m3NX1vZ1Lk4i13M

The BRIT Awards 2012

appears_on

compilation

https://i.scdn.co/image/209b4066a831af140c232d4bf6a3eadab013945d

https://open.spotify.com/album/1GvWtv7m3NX1vZ1Lk4i13M

2012-01-01

day

61

GB

29

5GaeDKlnZggETclP0Y3FfE

Domino Sampler ’11

appears_on

compilation

https://i.scdn.co/image/ab67616d0000b273d20f68791889d57d61a8400e

https://open.spotify.com/album/5GaeDKlnZggETclP0Y3FfE

2011-12-20

day

18

CA

9

4jqIQ4srPIBcTcPdGFxdGF

All The Rage

appears_on

compilation

https://i.scdn.co/image/ab67616d0000b2737c4f2e686a2fa77227fb5b53

https://open.spotify.com/album/4jqIQ4srPIBcTcPdGFxdGF

2008-02-01

day

15

AD

29

3Vx8n3WCI0s0l2mvS0IMg3

Again & Again

appears_on

single

https://i.scdn.co/image/ab67616d0000b273549cf94db1362c3939806e7e

https://open.spotify.com/album/3Vx8n3WCI0s0l2mvS0IMg3

2008

year

4

JP

0


Get Tracks Information


Firstly, you can get all the tracks of an artist. This can be done with the next function:

tracks <- get_artist_tracks('Arctic Monkeys')

 

track_name

track_uri

album_uri

artist_uri

disc_number

track_duration_ms

explicit

spotify_url

track_mp3_30s

track_number

Star Treatment

0FgNSsaSZTvbLXUumSO8LQ

1jeMiSeSnNS0Oys375qegp

7Ln80lUS6He07XvHI8qqHH

1

354640

FALSE

https://open.spotify.com/track/0FgNSsaSZTvbLXUumSO8LQ

https://p.scdn.co/mp3-preview/96cf115e9813becf7d7d39f80fcc6a929666858f?cid=51de8d21176d4ba6bab17336e02c2d77

1

One Point Perspective

2AKwcNHeGAqCpD6etK425X

1jeMiSeSnNS0Oys375qegp

7Ln80lUS6He07XvHI8qqHH

1

208640

FALSE

https://open.spotify.com/track/2AKwcNHeGAqCpD6etK425X

https://p.scdn.co/mp3-preview/9ff7880279f7b054f15c089cbb5f445994c9c926?cid=51de8d21176d4ba6bab17336e02c2d77

2

American Sports

0mQiDbYxHElUp1eNpLZXaY

1jeMiSeSnNS0Oys375qegp

7Ln80lUS6He07XvHI8qqHH

1

158040

FALSE

https://open.spotify.com/track/0mQiDbYxHElUp1eNpLZXaY

https://p.scdn.co/mp3-preview/aa923f7fc63e04db25d28dbe6737614591bc9e1d?cid=51de8d21176d4ba6bab17336e02c2d77

3

Tranquility Base Hotel & Casino

7co8zvT6HJuyT81IDukyID

1jeMiSeSnNS0Oys375qegp

7Ln80lUS6He07XvHI8qqHH

1

212093

TRUE

https://open.spotify.com/track/7co8zvT6HJuyT81IDukyID

https://p.scdn.co/mp3-preview/154fb32a10dabdad3512556e0fa48279604b5a3b?cid=51de8d21176d4ba6bab17336e02c2d77

4

Golden Trunks

3NmndGYOIYocA5OTHMVoaw

1jeMiSeSnNS0Oys375qegp

7Ln80lUS6He07XvHI8qqHH

1

173626

FALSE

https://open.spotify.com/track/3NmndGYOIYocA5OTHMVoaw

https://p.scdn.co/mp3-preview/cada88e14b7a14571232ca35874c5398ebc3725b?cid=51de8d21176d4ba6bab17336e02c2d77

5

 

You can also get all the tracks features information (like danceability, mode, acousticness, speechness, valence, loudness…) for an artist:

 

features <- get_artist_tracks_features('Arctic Monkeys')

 

track_uri

danceability

energy

key

loudness

mode

speechiness

acousticness

instrumentalness

liveness

valence

tempo

time_signature

popularity

0FgNSsaSZTvbLXUumSO8LQ

0.581

0.767

7

-5.026

0

0.0527

0.24300

1.31e-03

0.141

0.6730

120.070

4

60

2AKwcNHeGAqCpD6etK425X

0.714

0.585

8

-4.570

1

0.0419

0.05700

4.44e-03

0.144

0.8420

77.104

4

59

0mQiDbYxHElUp1eNpLZXaY

0.372

0.865

7

-4.288

0

0.0513

0.00169

2.93e-03

0.127

0.5940

176.689

3

53

7co8zvT6HJuyT81IDukyID

0.584

0.835

9

-4.326

0

0.0297

0.04110

1.47e-02

0.199

0.7810

142.200

4

60

3NmndGYOIYocA5OTHMVoaw

0.492

0.564

2

-5.811

1

0.0278

0.12900

1.73e-05

0.131

0.0896

92.544

4

54

 

You can also search for tracks recommendations based on another track. You can choose the number of recommended tracks returned:

recommended_songs <- get_tracks_recommendations_by_name('Arctic Monkeys', 'Do I Wanna Know?', limit = 10)

 

track_name

popularity

artist_name

track_duration_ms

track_mp3_30s

track_url

artist_url

artist_uri

track_uri

Let It Happen

76

Tame Impala

467586

NULL

https://open.spotify.com/track/2X485T9Z5Ly0xyaghN73ed

https://open.spotify.com/artist/5INjqkS1o8h1imAzPqGZBb

5INjqkS1o8h1imAzPqGZBb

2X485T9Z5Ly0xyaghN73ed

Somebody Told Me

70

The Killers

197160

NULL

https://open.spotify.com/track/59XNy4523UcEvQfYM8fdQB

https://open.spotify.com/artist/0C0XlULifJtAgn6ZNCW2eu

0C0XlULifJtAgn6ZNCW2eu

59XNy4523UcEvQfYM8fdQB

Clint Eastwood

69

Gorillaz

340920

https://p.scdn.co/mp3-preview/20e27bbc6c7dd769fc5245d2ef6da78e2ad99549?cid=51de8d21176d4ba6bab17336e02c2d77

https://open.spotify.com/track/7yMiX7n9SBvadzox8T5jzT

https://open.spotify.com/artist/3AA28KZvwAUcZuOKwyblJQ

3AA28KZvwAUcZuOKwyblJQ

7yMiX7n9SBvadzox8T5jzT

I Want It All

62

Arctic Monkeys

185406

https://p.scdn.co/mp3-preview/5b90c0c4229fd4dbf7982bfae413a50c7bdc5ca2?cid=51de8d21176d4ba6bab17336e02c2d77

https://open.spotify.com/track/1j9rezdE3YeC7yktZXC1em

https://open.spotify.com/artist/7Ln80lUS6He07XvHI8qqHH

7Ln80lUS6He07XvHI8qqHH

1j9rezdE3YeC7yktZXC1em

24/7

59

The Neighbourhood

219973

https://p.scdn.co/mp3-preview/29aac828243e1a1b5cad85d4a490188be385c4d6?cid=51de8d21176d4ba6bab17336e02c2d77

https://open.spotify.com/track/4Cdb1dE3pfiWlqnAIcImyQ

https://open.spotify.com/artist/77SW9BnxLY8rJ0RciFqkHh

77SW9BnxLY8rJ0RciFqkHh

4Cdb1dE3pfiWlqnAIcImyQ


Search Spotify Item


You can also search an spotify item (artist, album or track) by name with the function get_spotify_item. We can use it in four different ways:

  • Search an artist (this is basically the same output as get_artist function).

 

search_artist <- get_spotify_item(name = 'Arctic Monkeys', type = 'artist')

 

artist_name

artist_uri

artist_img

num_followers

spotify_url

popularity

musical_genre

Arctic Monkeys

7Ln80lUS6He07XvHI8qqHH

https://i.scdn.co/image/ed0552e9746ed2bbf04ae4bcb5525700ca31522d

8780891

https://api.spotify.com/v1/artists/7Ln80lUS6He07XvHI8qqHH

86

garage_rock-modern_rock-permanent_wave-rock-sheffield_indie

 

  • Search an album with the ‘name’ parameter. In this case, we can get similar results but not exactly the album name specified in the parameter function (We can try to use a higher limit to get the desired album).

 

search_album <- get_spotify_item(name = 'Nuclear', type = 'album')

 

album_uri

album_name

album_type

album_img

album_url

release_date

release_date_precision

total_tracks

available_markets

popularity

3tgTuhdTPvaka7LUU4gzdb

Bang! Pow! Boom! Nuclear

album

https://i.scdn.co/image/ab67616d0000b273b6e01aed96a03d8706c91931

https://open.spotify.com/album/3tgTuhdTPvaka7LUU4gzdb

2010-04-01

day

23

AD

49

search_album_with_limit <- get_spotify_item(name = 'Nuclear', type = 'album', limit = 5)

album_uri

album_name

album_type

album_img

album_url

release_date

release_date_precision

total_tracks

available_markets

popularity

3tgTuhdTPvaka7LUU4gzdb

Bang! Pow! Boom! Nuclear

album

https://i.scdn.co/image/ab67616d0000b273b6e01aed96a03d8706c91931

https://open.spotify.com/album/3tgTuhdTPvaka7LUU4gzdb

2010-04-01

day

23

AD

49

0am24qfT1RsxWY2bxhcrJB

Not Animal

album

https://i.scdn.co/image/ab67616d0000b2733365b7ae7c208abc36ede955

https://open.spotify.com/album/0am24qfT1RsxWY2bxhcrJB

2008-10-07

day

12

AD

49

2azrcCM9HIS232wphRkTTG

Nuclear Bonds

single

https://i.scdn.co/image/ab67616d0000b273d0a1f24ca9fb845889433591

https://open.spotify.com/album/2azrcCM9HIS232wphRkTTG

2015-10-13

day

9

AD

41

1T2uKSyUf7DBWOABtsmYLM

Nuclear

album

https://i.scdn.co/image/ab67616d0000b27313c4ca893a92d3a81e0cb3e8

https://open.spotify.com/album/1T2uKSyUf7DBWOABtsmYLM

2019-03-22

day

12

AD

71

3MDHsd4SZfMtCdM8Clihqp

THE DUST OF RETREAT (EXPLICIT)

album

https://i.scdn.co/image/ab67616d0000b273c455808106bb65b0cff01bde

https://open.spotify.com/album/3MDHsd4SZfMtCdM8Clihqp

2005

year

12

US

39

 

  • Search an album with the ‘artist’ and ‘album’ parameters to avoid the problem.

 

search_album_2 <- get_spotify_item(artist = 'Leiva', album = 'Nuclear', type = 'album')

 

album_uri

album_name

album_type

album_img

album_url

release_date

release_date_precision

total_tracks

available_markets

popularity

1T2uKSyUf7DBWOABtsmYLM

Nuclear

album

https://i.scdn.co/image/ab67616d0000b27313c4ca893a92d3a81e0cb3e8

https://open.spotify.com/album/1T2uKSyUf7DBWOABtsmYLM

2019-03-22

day

12

AD

71

 

  • Search a specific track.

 

search_track <- get_spotify_item(name = 'Do I Wanna Know?' , type = 'track')

 

track_name

track_uri

artist_name

artist_uri

disc_number

track_duration_ms

explicit

spotify_url

track_mp3_30s

track_number

Do I Wanna Know?

5FVd6KXrgO9B3JPmC8OPst

Arctic Monkeys

7Ln80lUS6He07XvHI8qqHH

1

272394

FALSE

https://open.spotify.com/track/5FVd6KXrgO9B3JPmC8OPst

https://p.scdn.co/mp3-preview/73e00a0a59c897b16d0fe30df43f7aeb2997079d?cid=51de8d21176d4ba6bab17336e02c2d77

1


Issues


If you find some type of error, bug or doubt, please, let me know it here .


License


This package (DSpoty) is licensed under the MIT License.

A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.