I made a new R package (I understand 🔥📦🔥 is the correct term) this week. My sophomore post on this site (from way back in March) was a text-based analysis of Kanye West records, which exposed me to the Genius API1 once I wanted to get hold of some lyrical content. In the process, I wrote some functions for playing nicely with this API in R that I knew would probably be handy for others dabbling/wanting to dabble in similar matters.
The Genius API’s search capabilities allows for users to scan all hosted content on the site (i.e. all songs). A couple of quick implementations exist in the package for retrieving artist (
search_artist()) and song (
search_song()) matches for search terms. Let’s start by looking for songs brought back by a simple search for “Versace”.
versace_search <- search_song(search_term = "Versace") %>% glimpse
## Observations: 10 ## Variables: 5 ## $ song_id <int> 176872, 2898687, 182654, 183346, 176951, 20050... ## $ song_name <chr> "Versace (Remix) (Ft. Drake)", "Versace on the... ## $ song_lyrics_url <chr> "https://genius.com/Migos-versace-remix-lyrics... ## $ artist_id <int> 44080, 500, 1319, 554, 44080, 32397, 2236, 125... ## $ artist_name <chr> "Migos", "Bruno Mars", "Meek Mill", "Tyga", "M...
Wanna bring back some fuller information about a particular song (
get_song_meta()), artist (
get_artist_meta()) or album (
get_album_meta())? Well, now you can. Let’s test one out - how about investigating a classic Versace-themed track from our last search, ‘Versace’ by Migos.
versace_meta <- get_song_meta(song_id = versace_search$song_id) %>% glimpse
## Observations: 1 ## Variables: 13 ## $ song_id <int> 176951 ## $ song_name <chr> "Versace" ## $ song_lyrics_url <chr> "https://genius.com/Migos-versace-lyrics" ## $ song_art_image_url <chr> "https://images.genius.com/4fe960be500117d6... ## $ release_date <chr> "2013-07-09" ## $ pageviews <int> 88862 ## $ annotation_count <int> 29 ## $ artist_id <int> 44080 ## $ artist_name <chr> "Migos" ## $ artist_url <chr> "https://genius.com/artists/Migos" ## $ album_id <int> 36011 ## $ album_name <chr> "Young Rich Niggas" ## $ album_url <chr> "https://genius.com/albums/Migos/Young-rich...
With this metadata, we can easily pull up the song’s artwork, for example. Why not.
knitr::include_graphics(path = versace_meta$song_art_image_url)
The package allows you to grab lyrics for tracks real quick, using the
scrape_lyrics_url()) function.3 While we’re at it, let’s call on
tidytext and answer a question for the people - just how much were the Migos chatting about “Versace” in ‘Versace’?
# get words proportion representation versace_prop <- scrape_lyrics_url(versace_meta$song_lyrics_url) %>% unnest_tokens(word, line) %>% count(word) %>% mutate(proportion = n/sum(n)) %>% # was the word versace or the rest mutate(versace_or_the_rest = if_else(word == "versace", "Versace", "Not Versace")) %>% group_by(versace_or_the_rest) %>% summarise(proportion = sum(proportion) * 100) # waffle plot parts <- c(`Not Versace` = versace_prop$proportion[], Versace = versace_prop$proportion[]) waffle(parts, rows = 8, size = 1, colors = c("#3C3A32", "#E3BD08"), use_glyph = "volume-up", glyph_size = 10, title = "How much Versace is in Migos - 'Versace'?") + labs(caption = "@ewen_") + theme_void(base_family = "Work Sans", base_size = 12) + theme(legend.position = "bottom")
The word “Versace” represents 32.5% of the lyrics in Migos - Versace. Phew.
That’s all for now, enjoy the package and keep up with future developments over at the GitHub repo.
As well as API helper functions, the package includes things that are scraping the Genius site (Genius’ API doesn’t include lyrics, for example). Be sure to read the terms of service in case of any concerns.↩