Module: Googol::Playlists

Included in:
YoutubeAccount
Defined in:
lib/googol/youtube_account/playlists.rb

Overview

Separate module to group YoutubeAccount methods related to playlists.

Instance Method Summary collapse

Instance Method Details

#create_playlist!(attrs = {}) ⇒ String

Creates a playlist for a Youtube account

Parameters:

  • attributes (Hash)

    The attributes of the new playlist

Returns:

  • (String)

    The ID of the playlist

See Also:



14
15
16
17
18
19
20
# File 'lib/googol/youtube_account/playlists.rb', line 14

def create_playlist!(attrs = {})
  snippet = {title: attrs[:title], description: attrs[:description]}
  playlist = youtube_request! json: true, method: :post,
    path: '/playlists?part=snippet,status',
    body: {snippet: snippet, status: {privacyStatus: :public}}
  playlist[:id]
end

#delete_playlists!(filters = {}) ⇒ Object

Delete all the playlists of the Youtube account matching the filters.

Examples:

account.delete_playlists! title: ‘Title’


account.delete_playlists! description: /desc/


Parameters:

  • filters (Hash) (defaults to: {})

    The filter to query the list of playlists with.

  • title (Hash)

    a customizable set of options

  • description (Hash)

    a customizable set of options



121
122
123
# File 'lib/googol/youtube_account/playlists.rb', line 121

def delete_playlists!(filters = {})
  playlists(filters).map{|playlist_id| delete_playlist! playlist_id}
end

#find_or_create_playlist_by(filters = {}) ⇒ String or nil

Note:

Google API does not have a “search” endpoint, therefore we have

Return the first playlist of the Youtube account matching the attributes or creates one if none is found

to scan the list of playlist page by page, limiting at 10 pages to prevent this function from running forever (50 playlists per page).

Examples:

account.find_playlist_by title: ‘Title’ # => ‘e45fXDsdsdsd’


account.find_playlist_by description: ‘xxxx’ # => nil


Parameters:

  • filters (Hash) (defaults to: {})

    The filter to query the list of playlists with.

  • title (Hash)

    a customizable set of options

  • description (Hash)

    a customizable set of options

Returns:

  • (String or nil)

    The ID of the playlist (or nil if not found)



67
68
69
# File 'lib/googol/youtube_account/playlists.rb', line 67

def find_or_create_playlist_by(filters = {})
  find_playlist_by(filters) || create_playlist!(filters)
end

#find_playlist_by(filters = {}) ⇒ String or nil

Note:

Google API does not have a “search” endpoint, therefore we have

Return the first playlist of the Youtube account matching the attributes.

to scan the list of playlist page by page, limiting at 10 pages to prevent this function from running forever (50 playlists per page).

Examples:

account.find_playlist_by title: ‘Title’ # => ‘e45fXDsdsdsd’


account.find_playlist_by description: ‘xxxx’ # => nil


Parameters:

  • filters (Hash) (defaults to: {})

    The filter to query the list of playlists with.

  • title (Hash)

    a customizable set of options

  • description (Hash)

    a customizable set of options

Returns:

  • (String or nil)

    The ID of the playlist (or nil if not found)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/googol/youtube_account/playlists.rb', line 36

def find_playlist_by(filters = {})
  page = filters.delete(:page) || 1

  path = "/playlists?part=id,snippet&mine=true"
  path << "&maxResults=#{filters.delete(:max) || 50 }"
  path << "&pageToken=#{filters.delete :token}" if filters[:token]

  response = youtube_request! path: path

  if playlist = response[:items].find{|p| playlist_matches? p, filters}
    playlist[:id]
  elsif page < 10 && token = response[:nextPageToken]
    find_playlist_by filters.merge page: page + 1, token: token
  end
end

#update_playlist!(playlist_id, attrs = {}) ⇒ String

Returns The ID of the playlist.

Returns:

  • (String)

    The ID of the playlist

See Also:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/googol/youtube_account/playlists.rb', line 87

def update_playlist!(playlist_id, attrs = {})
  body = {id: playlist_id}
  parts = []

  if attrs.key? :title
    body[:snippet] = {title: attrs[:title]}
    parts << 'snippet'
  else
    raise RequestError, "Cannot update a playlist without a title"
  end

  if attrs.key? :description
    body[:snippet][:description] = attrs[:description]
  end

  if attrs.key? :public
    body[:status] = {privacyStatus: attrs[:public] ? 'public' : 'private'}
    parts << 'status'
  end

  playlist = youtube_request! json: true, method: :put,
    path: "/playlists?part=#{parts.join ','}", body: body
  playlist[:id]
end