-
Notifications
You must be signed in to change notification settings - Fork 67
/
readme.Rmd
184 lines (131 loc) · 6.63 KB
/
readme.Rmd
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# spotifyr
<!-- badges: start -->
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/spotifyr?color=brightgreen)](https://cran.r-project.org/package=spotifyr)
![](http://cranlogs.r-pkg.org/badges/spotifyr?color=brightgreen)
[![CRAN_time_from_release](https://www.r-pkg.org/badges/ago/spotifyr)](https://cran.r-project.org/package=spotifyr)
[![metacran downloads](https://cranlogs.r-pkg.org/badges/spotifyr)](https://cran.r-project.org/package=spotifyr)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.5638489.svg)](https://doi.org/10.5281/zenodo.5638489)
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
<!-- badges: end -->
## Overview
spotifyr is an R wrapper for pulling track audio features and other information from Spotify's Web API in bulk. By automatically batching API requests, it allows you to enter an artist's name and retrieve their entire discography in seconds, along with Spotify's audio features and track/album popularity metrics. You can also pull song and playlist information for a given Spotify User (including yourself!).
## Installation
CRAN version 2.2.3 (recommended)
```r
install.packages('spotifyr')
```
Development version
```r
devtools::install_github('charlie86/spotifyr')
```
## Authentication
First, set up a Dev account with Spotify to access their Web API [here](https://developer.spotify.com/my-applications/#!/applications). This will give you your `Client ID` and `Client Secret`. Once you have those, you can pull your access token into R with `get_spotify_access_token()`.
The easiest way to authenticate is to set your credentials to the System Environment variables `SPOTIFY_CLIENT_ID` and `SPOTIFY_CLIENT_SECRET`. The default arguments to `get_spotify_access_token()` (and all other functions in this package) will refer to those. Alternatively, you can set them manually and make sure to explicitly refer to your access token in each subsequent function call.
```{r, eval = FALSE}
Sys.setenv(SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxx')
Sys.setenv(SPOTIFY_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxx')
access_token <- get_spotify_access_token()
```
#### Authorization Code Flow
For certain functions and applications, you'll need to log in as a Spotify user. To do this, your Spotify Developer application needs to have a callback url. You can set this to whatever you want that will work with your application, but a good default option is `http://localhost:1410/` (see image below). For more information on authorization, visit the offical [Spotify Developer Guide](https://developer.spotify.com/documentation/general/guides/authorization-guide/).
```{r, out.width = "50%", echo = FALSE}
knitr::include_graphics('man/figures/spotifyr_auth_screenshot.png')
```
## Usage
### What Was the Beatles' Favorite Key?
```{r load_packages, message=FALSE, warning=FALSE, results='hide'}
library(spotifyr)
beatles <- get_artist_audio_features('the beatles')
```
```{r, message = FALSE, warning = FALSE}
library(dplyr)
library(purrr)
library(knitr)
beatles %>%
count(key_mode, sort = TRUE) %>%
head(5) %>%
kable()
```
### Get your most recently played tracks
```{r my-recently-played, message=FALSE}
library(lubridate)
get_my_recently_played(limit = 5) %>%
mutate(
artist.name = map_chr(track.artists, function(x) x$name[1]),
played_at = as_datetime(played_at)
) %>%
select(
all_of(c("track.name", "artist.name", "track.album.name", "played_at"))
) %>%
kable()
```
### Find Your All Time Favorite Artists
```{r my-top-artists}
get_my_top_artists_or_tracks(type = 'artists',
time_range = 'long_term',
limit = 5) %>%
select(.data$name, .data$genres) %>%
rowwise %>%
mutate(genres = paste(.data$genres, collapse = ', ')) %>%
ungroup %>%
kable()
```
### Find your favorite tracks at the moment
```{r top-tracks}
get_my_top_artists_or_tracks(type = 'tracks',
time_range = 'short_term',
limit = 5) %>%
mutate(
artist.name = map_chr(artists, function(x) x$name[1])
) %>%
select(name, artist.name, album.name) %>%
kable()
```
### What's the most joyful Joy Division song?
My favorite audio feature has to be "valence," a measure of musical positivity.
```{r joy-division, results='hide'}
joy <- get_artist_audio_features('joy division')
```
```{r show-joy}
joy %>%
arrange(-valence) %>%
select(.data$track_name, .data$valence) %>%
head(5) %>%
kable()
```
Now if only there was some way to plot joy...
### Joyplot of the emotional rollercoasters that are Joy Division's albums
```{r joyplot, message=FALSE}
library(ggplot2)
library(ggridges)
ggplot(
joy,
aes(x = valence, y = album_name)
) +
geom_density_ridges() +
theme_ridges() +
labs(title = "Joyplot of Joy Division's joy distributions",
subtitle = "Based on valence pulled from Spotify's Web API with spotifyr")
```
## Dope Stuff Other People Have Done with spotifyr
The coolest thing about making this package has definitely been seeing all the awesome stuff other people have done with it. Here are a few examples:
[Exploring the Spotify API with R: A tutorial for beginners, by a beginner](https://msmith7161.github.io/what-is-speechiness/), Mia Smith
[Blue Christmas: A data-driven search for the most depressing Christmas song](https://www.caitlinhudon.com/posts/2017/12/22/blue-christmas), Caitlin Hudon
[Sente-se triste quando ouve "Amar pelos dois"? Não é o único (Do you feel sad when you hear "Love for both?" You're not alone)](https://rr.sapo.pt/especial/112355/sente-se-triste-quando-ouve-amar-pelos-dois-nao-e-o-unico), Rui Barros, Rádio Renascença
[Using Data to Find the Angriest Death Grips Song](https://medium.com/@evanopp/angriest-death-grips-data-anger-502168c1c2f0), Evan Oppenheimer
[Hierarchical clustering of David Bowie records](https://twitter.com/WireMonkey/status/1009915034246565891?s=19), Alyssa Goldberg
[tayloR](https://medium.com/@simranvatsa5/taylor-f656e2a09cc3), Simran Vatsa
## Code of Conduct
Please note that the spotifyr project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.