module GoogleMusicApi::Playlist
Holds all the playlist related methods
Public Instance Methods
Adds tracks to a playlist @param [string] playlist_id @param [array] track_ids
# File lib/google_music_api/playlist.rb, line 138 def add_tracks_to_playlist(playlist_id, track_ids) url = 'plentriesbatch' options = {} prev_id, cur_id, next_id = nil, SecureRandom.uuid, SecureRandom.uuid mutations = [] track_ids.each_with_index do |value, index| m_details = { clientId: cur_id, creationTimestamp: '-1', deleted: false, lastModifiedTimestamp: '0', playlistId: playlist_id, source: 1, trackId: value, } m_details[:source] = 2 if value[0] == 'T' m_details[:precedingEntryId] = prev_id if index > 0 m_details[:followingEntryId] = next_id if index < value.length - 1 mutations << {create: m_details} prev_id, cur_id, next_id = cur_id, next_id, SecureRandom.uuid end options[:body] = {mutations: mutations}.to_json make_post_request(url, options) end
Creates a playlist
@param [String] name @param [String] description, default = '' @param [boolean] public, default = false @return [Hash] with the API response, status is in the 'response_code' key
# File lib/google_music_api/playlist.rb, line 45 def create_playlist(name, description = '', public = false) create_playlists [{name: name, description: description, public: public}][0] end
Batch creates playlists
@param [Array] playlist_descriptions, consists of one or more hashes with the keys :name, :description and :public @return [Array] of hashes with the result of each creation operation
# File lib/google_music_api/playlist.rb, line 52 def create_playlists(playlist_descriptions = []) url = 'playlistbatch' creates = playlist_descriptions.map do |pd| { create: { creationTimestamp: '-1', deleted: false, lastModifiedTimestamp: '0', name: pd[:name], description: pd.fetch(:description, ''), type: 'USER_GENERATED', shareState: (pd.fetch(:public, false) ? 'PUBLIC' : 'PRIVATE') } } end options = { body: {mutations: creates}.to_json } make_post_request(url, options).fetch('mutate_response') end
Deletes a playlist
@param [string] id @return [Hash] with the API response, status is in the 'response_code' key
# File lib/google_music_api/playlist.rb, line 112 def delete_playlist(id) delete_playlists([id])[0] end
Batch deletes one or more playlists @return [Array] of hashes with the result of each creation operation @param [Array] ids
# File lib/google_music_api/playlist.rb, line 119 def delete_playlists(ids = []) url = 'playlistbatch' deletes = ids.map do |pd| { delete: pd } end options = { body: {mutations: deletes}.to_json } make_post_request(url, options).fetch('mutate_response') end
Gets all the playlists that user has @return [Array] of Hashes that describe a playlist
# File lib/google_music_api/playlist.rb, line 7 def get_all_playlists url = 'playlistfeed' make_post_request(url).fetch('data', {'items' => []})['items'] end
Gets all the tracks in the user's own playlists. Doesn't include subscribed playlists. If you want to get the tracks of a single playlist, use
{get_shared_playlists_entries}
@return [Array] of hashes describing playlist entries. Each Playlist
entry will have the playlist's id and the track description
# File lib/google_music_api/playlist.rb, line 16 def get_own_playlists_entries url = 'plentryfeed' make_post_request(url).fetch('data', {'items' => []})['items'] end
Batch removes track from playlists. You can send playlist entries of multiple playlists
@param [Array] playlist_entry_ids These are playlist entries, not track ids
# File lib/google_music_api/playlist.rb, line 177 def remove_tracks_from_playlist(playlist_entry_ids) url = 'plentriesbatch' mutations = playlist_entry_ids.map { |id| {delete: id}} options = { body: { mutations: mutations }.to_json } make_post_request url, options end
# File lib/google_music_api/playlist.rb, line 190 def reorder_playlist_entry throw NotImplementedError.new end
Updates a single playlist @param [string] id, the actual id, not the share token @param [string] new_name @param [string] new_description @param [boolean] new_public @return [Hash] with the API response, status is in the 'response_code' key
# File lib/google_music_api/playlist.rb, line 81 def update_playlist(id, new_name = nil, new_description = nil, new_public = nil) update_playlists [{id: id, name: new_name, description: new_description, public: new_public}][0] end
Batch updates one or more playlists @param [Array] playlist_descriptions, consists of one or more hashes with the keys id:, :name, :description and :public @return [Array] of hashes with the result of each creation operation
# File lib/google_music_api/playlist.rb, line 88 def update_playlists(playlist_descriptions) url = 'playlistbatch' updates = playlist_descriptions.map do |pd| { update: { id: pd[:id], name: pd[:name], description: pd[:description], shareState: (pd[:public] ? 'PUBLIC' : 'PRIVATE') } } end options = { body: {mutations: updates}.to_json } make_post_request(url, options).fetch('mutate_response') end
Protected Instance Methods
# File lib/google_music_api/playlist.rb, line 196 def add_track_type(track_id) if track_id[0] == 'T' {'id': track_id, 'type': 1} else {'id': track_id, 'type': 0} end end