geniusr package problem with get_lyrics function

157 Views Asked by At

i know thats been asked before (Get lyrics from song with Geniusr) but it didnt help me.

every other function works except the get_lyrics_* functions.... always with the same error:

Error in section_artist[is.na(section_artist)] <- artist : replacement has length zero

i have installed and opened html_nodes and rvest and tried running the fixing patch from the old stackoverflow and github questions.

it still doesnt work. would should i do!!!!

my code:

library(tidyverse)
library(geniusr)                        
library(tidytext)
library(readr)
library(rvest)


#genius api token
genius_token(force = TRUE)
(...)


get_lyrics <- function (session) {
  lyrics <-  session %>% html_nodes(xpath = '//div[contains(@class, "Lyrics__Container")]')
  song <-  session %>% html_nodes(xpath = '//span[contains(@class, "SongVariantdesktop__")]') %>% html_text(trim = TRUE)
  artist <-  session %>% html_nodes(xpath = '//a[contains(@class, "SongVariantdesktop__Artist")]') %>% html_text(trim = TRUE)
  xml_find_all(lyrics, ".//br") %>% xml_add_sibling("p", "\n")
  xml_find_all(lyrics, ".//br") %>% xml_remove()
  lyrics <- html_text(lyrics, trim = TRUE)
  lyrics <- unlist(strsplit(lyrics, split = "\n"))
  lyrics <- grep(pattern = "[[:alnum:]]", lyrics, value = TRUE)
  if (is_empty(lyrics)) {
    return(tibble(line = NA, section_name = NA, section_artist = NA, 
                  song_name = song, artist_name = artist))
  }
  section_tags <- nchar(gsub(pattern = "\\[.*\\]", "", lyrics)) == 0
  sections <- geniusr:::repeat_before(lyrics, section_tags)
  sections <- gsub("\\[|\\]", "", sections)
  sections <- strsplit(sections, split = ": ", fixed = TRUE)
  section_name <- sapply(sections, "[", 1)
  section_artist <- sapply(sections, "[", 2)
  section_artist[is.na(section_artist)] <- artist
  tibble(line = lyrics[!section_tags], section_name = section_name[!section_tags], 
         section_artist = section_artist[!section_tags], song_name = song, 
         artist_name = artist)
}
assignInNamespace("get_lyrics", get_lyrics, "geniusr")


and than i tried an example and this came out:

> get_lyrics_search(Rihanna, Stay)
Error: object 'Rihanna' not found
> get_lyrics_search("Rihanna", "Stay")
Error in `tibble()`:
! Tibble columns must have compatible sizes.
• Size 34: Existing data.
• Size 0: Column `artist_name`.
ℹ Only values of size one are recycled.
Run `rlang::last_trace()` to see where the error occurred.
Called from: signal_abort(cnd, .file)
Browse[1]> c
> rlang::last_trace()
<error/tibble_error_incompatible_size>
Error in `tibble()`:
! Tibble columns must have compatible sizes.
• Size 34: Existing data.
• Size 0: Column `artist_name`.
ℹ Only values of size one are recycled.
---
Backtrace:
    ▆
 1. └─geniusr::get_lyrics_search("Rihanna", "Stay")
 2.   └─global get_lyrics(session)
 3.     └─tibble::tibble(...)
Run rlang::last_trace(drop = FALSE) to see 5 hidden frames.
> rlang::last_trace(drop = FALSE)
<error/tibble_error_incompatible_size>
Error in `tibble()`:
! Tibble columns must have compatible sizes.
• Size 34: Existing data.
• Size 0: Column `artist_name`.
ℹ Only values of size one are recycled.
---
Backtrace:
    ▆
 1. └─geniusr::get_lyrics_search("Rihanna", "Stay")
 2.   └─global get_lyrics(session)
 3.     └─tibble::tibble(...)
 4.       └─tibble:::tibble_quos(xs, .rows, .name_repair)
 5.         └─tibble:::vectbl_recycle_rows(...)
 6.           └─tibble:::abort_incompatible_size(...)
 7.             └─tibble:::tibble_abort(...)
 8.               └─rlang::abort(x, class, ..., call = call, parent = parent, use_cli_format = TRUE)
0

There are 0 best solutions below