Class: RSpotify::Recommendations

Inherits:
Base
  • Object
show all
Defined in:
lib/rspotify/recommendations.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#external_urls, #href, #id, #type, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#complete!, #embed, find, #method_missing, #respond_to?, search

Constructor Details

#initialize(options = {}) ⇒ Recommendations

Returns a new instance of Recommendations.



100
101
102
103
104
105
# File 'lib/rspotify/recommendations.rb', line 100

def initialize(options = {})
  @seeds = options['seeds'].map { |i| RecommendationSeed.new i }
  @tracks = options['tracks'].map { |i| Track.new i }

  super(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RSpotify::Base

Instance Attribute Details

#seedsArray<RecommendationSeed>

Returns:



5
6
7
# File 'lib/rspotify/recommendations.rb', line 5

def seeds
  @seeds
end

#tracksArray<Track>

An array of track object (simplified) ordered according to the parameters supplied.

Returns:

  • (Array<Track>)

    the current value of tracks



5
6
7
# File 'lib/rspotify/recommendations.rb', line 5

def tracks
  @tracks
end

Class Method Details

.available_genre_seedsArray<String>

Retrieve a list of available genres seed parameter values for recommendations.

Examples:

genres = RSpotify::Recommendations.available_genre_seeds

Returns:

  • (Array<String>)


12
13
14
15
16
17
# File 'lib/rspotify/recommendations.rb', line 12

def self.available_genre_seeds
  response = RSpotify.get('recommendations/available-genre-seeds')
  return response if RSpotify.raw_response

  response['genres']
end

.generate(limit: 20, seed_artists: [], seed_genres: [], seed_tracks: [], market: nil, **options) ⇒ Array<Recommendations>

Note:

Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.

Create a playlist-style listening experience based on seed artists, tracks and genres

Examples:

recommendations = RSpotify::Recommendations.generate(limit: 20, seed_tracks: ['0c6xIDDpzE81m2q797ordA'])
recommendations = RSpotify::Recommendations.generate(seed_tracks: ['0c6xIDDpzE81m2q797ordA'], seed_artists: ['4NHQUGzhtTLFvgF5SZesLK'], market: 'ES')
recommendations = RSpotify::Recommendations.generate(seed_tracks: ['0c6xIDDpzE81m2q797ordA'], seed_genres: ['alt_rock'], seed_artists: ['4NHQUGzhtTLFvgF5SZesLK'], target_energy: 1.0)

Parameters:

  • limit (Integer) (defaults to: 20)

    The target size of the list of recommended tracks. For seeds with unusually small pools or when highly restrictive filtering is applied, it may be impossible to generate the requested number of recommended tracks. Debugging information for such cases is available in the response. Default: 20. Minimum: 1. Maximum: 100.

  • seed_artists (Array<String>) (defaults to: [])

    A list of Spotify IDs for seed artists.

  • seed_genres (Array<String>) (defaults to: [])

    A list of any genres in the set of available genre seeds.

  • seed_tracks (Array<String>) (defaults to: [])

    A list of Spotify IDs for seed tracks.

  • market (String) (defaults to: nil)

    Optional. An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. Because min_*, max_* and target_* are applied to pools before relinking, the generated results may not precisely match the filters applied. Original, non-relinked tracks are available via the linked_from attribute of the relinked track response.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :min_acousticness (Float)

    Hard floor on the confidence measure from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high confidence the track is acoustic.

  • :max_acousticness (Float)

    Hard ceiling on the confidence measure from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high confidence the track is acoustic.

  • :target_acousticness (Float)

    Target value on the confidence measure from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high confidence the track is acoustic.

  • :min_danceability (Float)

    Hard floor on how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable.

  • :max_danceability (Float)

    Hard ceiling on how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable.

  • :target_danceability (Float)

    Target value on how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity. A value of 0.0 is least danceable and 1.0 is most danceable.

  • :min_duration_ms (Integer)

    Hard floor on the duration of the track in milliseconds.

  • :max_duration_ms (Integer)

    Hard ceiling on the duration of the track in milliseconds.

  • :target_duration_ms (Integer)

    Target value on the duration of the track in milliseconds.

  • :min_energy (Float)

    Hard floor on energy which is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy.

  • :max_energy (Float)

    Hard ceiling on energy which is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy.

  • :target_energy (Float)

    Target value on energy which is a measure from 0.0 to 1.0 and represents a perceptual measure of intensity and activity. Typically, energetic tracks feel fast, loud, and noisy. For example, death metal has high energy, while a Bach prelude scores low on the scale. Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy.

  • :min_instrumentalness (Float)

    Hard floor on prediction of whether a track contains no vocals. “Ooh” and “aah” sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly “vocal”. The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0.

  • :max_instrumentalness (Float)

    Hard ceiling on prediction of whether a track contains no vocals. “Ooh” and “aah” sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly “vocal”. The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0.

  • :target_instrumentalness (Float)

    Target value on prediction of whether a track contains no vocals. “Ooh” and “aah” sounds are treated as instrumental in this context. Rap or spoken word tracks are clearly “vocal”. The closer the instrumentalness value is to 1.0, the greater likelihood the track contains no vocal content. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0.

  • :min_key (Integer)

    Hard floor on the key the track is in. Integers map to pitches using standard Pitch Class notation. E.g. 0 = C, 1 = C♯/D♭, 2 = D, and so on.

  • :max_key (Integer)

    Hard ceiling on the key the track is in. Integers map to pitches using standard Pitch Class notation. E.g. 0 = C, 1 = C♯/D♭, 2 = D, and so on.

  • :target_key (Integer)

    Target value on the key the track is in. Integers map to pitches using standard Pitch Class notation. E.g. 0 = C, 1 = C♯/D♭, 2 = D, and so on.

  • :min_liveness (Float)

    Hard floor on liveness, which detects the presence of an audience in the recording. Higher liveness values represent an increased probability that the track was performed live. A value above 0.8 provides strong likelihood that the track is live.

  • :max_liveness (Float)

    Hard ceiling on liveness, which detects the presence of an audience in the recording. Higher liveness values represent an increased probability that the track was performed live. A value above 0.8 provides strong likelihood that the track is live.

  • :target_liveness (Float)

    Target value on liveness, which detects the presence of an audience in the recording. Higher liveness values represent an increased probability that the track was performed live. A value above 0.8 provides strong likelihood that the track is live.

  • :min_loudness (Float)

    Hard floor on the overall loudness of a track in decibels (dB). Loudness values are averaged across the entire track and are useful for comparing relative loudness of tracks. Loudness is the quality of a sound that is the primary psychological correlate of physical strength (amplitude). Values typical range between -60 and 0 db.

  • :max_loudness (Float)

    Hard ceiling on the overall loudness of a track in decibels (dB). Loudness values are averaged across the entire track and are useful for comparing relative loudness of tracks. Loudness is the quality of a sound that is the primary psychological correlate of physical strength (amplitude). Values typical range between -60 and 0 db.

  • :target_loudness (Float)

    Target value on the overall loudness of a track in decibels (dB). Loudness values are averaged across the entire track and are useful for comparing relative loudness of tracks. Loudness is the quality of a sound that is the primary psychological correlate of physical strength (amplitude). Values typical range between -60 and 0 db.

  • :min_mode (Integer)

    Hard floor on the modality (major or minor) of a track, the type of scale from which its melodic content is derived. Major is represented by 1 and minor is 0.

  • :max_mode (Integer)

    Hard ceiling on the modality (major or minor) of a track, the type of scale from which its melodic content is derived. Major is represented by 1 and minor is 0.

  • :target_mode (Integer)

    Target value on the modality (major or minor) of a track, the type of scale from which its melodic content is derived. Major is represented by 1 and minor is 0.

  • :min_popularity (Integer)

    Hard floor on the popularity of the track. The value will be between 0 and 100, with 100 being the most popular. The popularity is calculated by algorithm and is based, in the most part, on the total number of plays the track has had and how recent those plays are.

  • :max_popularity (Integer)

    Hard ceiling on the popularity of the track. The value will be between 0 and 100, with 100 being the most popular. The popularity is calculated by algorithm and is based, in the most part, on the total number of plays the track has had and how recent those plays are.

  • :target_popularity (Integer)

    Target value on the popularity of the track. The value will be between 0 and 100, with 100 being the most popular. The popularity is calculated by algorithm and is based, in the most part, on the total number of plays the track has had and how recent those plays are.

  • :min_speechiness (Float)

    Hard floor on speechiness which detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks.

  • :max_speechiness (Float)

    Hard ceiling on speechiness which detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks.

  • :target_speechiness (Float)

    Target value on speechiness which detects the presence of spoken words in a track. The more exclusively speech-like the recording (e.g. talk show, audio book, poetry), the closer to 1.0 the attribute value. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech, either in sections or layered, including such cases as rap music. Values below 0.33 most likely represent music and other non-speech-like tracks.

  • :min_tempo (Float)

    Hard floor on the overall estimated tempo of a track in beats per minute (BPM). In musical terminology, tempo is the speed or pace of a given piece and derives directly from the average beat duration.

  • :max_tempo (Float)

    Hard ceiling on the overall estimated tempo of a track in beats per minute (BPM). In musical terminology, tempo is the speed or pace of a given piece and derives directly from the average beat duration.

  • :target_tempo (Float)

    Target value on the overall estimated tempo of a track in beats per minute (BPM). In musical terminology, tempo is the speed or pace of a given piece and derives directly from the average beat duration.

  • :min_time_signature (Integer)

    Hard floor on the estimated overall time signature of a track. The time signature (meter) is a notational convention to specify how many beats are in each bar (or measure).

  • :max_time_signature (Integer)

    Hard ceiling on the estimated overall time signature of a track. The time signature (meter) is a notational convention to specify how many beats are in each bar (or measure).

  • :target_time_signature (Integer)

    Target value on the estimated overall time signature of a track. The time signature (meter) is a notational convention to specify how many beats are in each bar (or measure).

  • :min_valence (Float)

    Hard floor on the measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry).

  • :max_valence (Float)

    Hard ceiling on the measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry).

  • :target_valence (Float)

    Target value on the measure from 0.0 to 1.0 describing the musical positiveness conveyed by a track. Tracks with high valence sound more positive (e.g. happy, cheerful, euphoric), while tracks with low valence sound more negative (e.g. sad, depressed, angry).

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rspotify/recommendations.rb', line 76

def self.generate(limit: 20, seed_artists: [], seed_genres: [], seed_tracks: [], market: nil, **options)
  url = "recommendations?limit=#{limit}"

  url << "&seed_artists=#{seed_artists.join(',')}" if seed_artists.any?
  url << "&seed_genres=#{seed_genres.join(',')}" if seed_genres.any?
  url << "&seed_tracks=#{seed_tracks.join(',')}" if seed_tracks.any?

  options.each do |option, value|
    url << "&#{option}=#{value}"
  end

  response = if market.is_a? Hash
    url << '&market=from_token'
    User.oauth_get(market[:from].id, url)
  else
    url << "&market=#{market}" if market
    RSpotify.get(url)
  end

  return response if RSpotify.raw_response

  Recommendations.new response
end