Module: Genius::Songs

Extended by:
Errors
Defined in:
lib/genius/api/songs.rb

Overview

Genius::Songs module provides methods to work with songs (lyrics/descriptions/etc.)

Constant Summary

Constants included from Errors

Errors::ENDPOINT

Class Method Summary collapse

Methods included from Errors

error_handle, validate_token

Class Method Details

.get_lyrics(song_id) ⇒ Hash

Genius::Songs.get_lyrics -> Hash

Genius::Songs.get_lyrics method is used for extracting lyrics in plain text format.

Parameters:

  • song_id (Integer)

    Song id.

Returns:

Raises:

  • (ArgumentError)

    if song_id is blank.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/genius/api/songs.rb', line 65

def get_lyrics(song_id)
  raise ArgumentError, "`song_id` should be not blank!" if song_id.nil?

  response = HTTParty.get("https://genius.com/songs/#{song_id}")
  document = Nokogiri::HTML(response)
  # @todo: something wrong with lyrics attribute value 
  lyrics_path = document.xpath("//*[@class='Lyrics__Container-sc-1ynbvzw-6 YYrds']")
  lyrics_path.at_css("p").content
rescue NoMethodError
  retry
end

.songs(token: nil, song_id: nil, combine: false) ⇒ String, ...

Genius::Songs.songs -> NilClass

This method provides info about song by its id. It is not the same with Genius::Search.search method, because it modify a JSON only for concrete song id, not for whole search database, which is returned in Genius::Search.search.

Examples:

Genius::Songs.songs(song_id: 294649) #=> {"some_kind_of_hash"}

Parameters:

  • token (String) (defaults to: nil)

    Token to access api.genius.com.

  • song_id (Integer) (defaults to: nil)

    Song id.

Returns:

  • (String)

    the error message if lyrics param is true.

  • (Hash)

    if lyrics param is false.

  • (NilClass)

    if TokenError exception raised.

Raises:

  • (PageNotFound)

    if page is not found.

  • (LyricsNotFound)

    if output JSON is nil.

  • (TokenError)

    if token or Genius::Auth.token are invalid.



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
# File 'lib/genius/api/songs.rb', line 27

def songs(token: nil, song_id: nil, combine: false)
  return if token.nil? && !Auth.authorized?.nil?

  Errors.validate_token(token) unless token.nil?

  response = HTTParty.get("#{Api::RESOURCE}/songs/#{song_id}?access_token=#{token_ext(token)}").body
  response = JSON.parse response
  if combine
    begin
      output_html = Nokogiri::HTML(HTTParty.get("https://genius.com/songs/#{song_id}"))
      raise PageNotFound if PageNotFound.page_not_found?(output_html)

      # @todo: maybe need some optimisations
      unformed_json = output_html.css("script")[17]
                                 .text.match(/window\.__PRELOADED_STATE__\s=\sJSON.parse\('(?<json>(.+?))'\);/)
      raise LyricsNotFoundError if unformed_json.nil?

      formatted_json = unformed_json[:json]
      lyrics_json = JSON.parse(formatted_json.unescape)
      response["lyrics"] = lyrics_json
      return response
    rescue LyricsNotFoundError
      retry
    rescue PageNotFound => e
      "Error description: #{e.msg}\nException type: #{e.exception_type}"
    end
  end
  response
end