Class: YtDataApi::YtDataApiClient
- Inherits:
-
Object
- Object
- YtDataApi::YtDataApiClient
- Defined in:
- lib/yt_data_api.rb
Instance Method Summary collapse
- #add_video_to_playlist(video_id, playlist_id) ⇒ Object
- #add_videos_to_playlist(video_ids, playlist_id) ⇒ Object
- #create_playlist(playlist_name) ⇒ Object
- #delete_playlist(playlist_id) ⇒ Object
- #empty_playlist(playlist_id) ⇒ Object
- #get_client_playlist_entries(playlist_id) ⇒ Object
- #get_playlist_id(playlist_name) ⇒ Object
- #get_video_ids(search_query, count = 1) ⇒ Object
-
#initialize(user_id, user_pswd, dev_key) ⇒ YtDataApiClient
constructor
A new instance of YtDataApiClient.
Constructor Details
#initialize(user_id, user_pswd, dev_key) ⇒ YtDataApiClient
Returns a new instance of YtDataApiClient.
9 10 11 12 13 14 15 |
# File 'lib/yt_data_api.rb', line 9 def initialize(user_id, user_pswd, dev_key) post_response = client_login(user_id, user_pswd, dev_key) if(post_response.code != "200") raise "Error while authenticating #{user_id} - #{post_response.code}: #{post_response.message}" end end |
Instance Method Details
#add_video_to_playlist(video_id, playlist_id) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/yt_data_api.rb', line 82 def add_video_to_playlist(video_id, playlist_id) headers = {"Content-Type" => "application/atom+xml", "Authorization" => "GoogleLogin auth=#{@client_auth_key}", "X-GData-Key" => @dev_auth_key, "GData-Version" => "2"} new_row = "<?xml version='1.0' encoding='UTF-8'?>" + "<entry xmlns='http://www.w3.org/2005/Atom' " + "xmlns:yt='http://gdata.youtube.com/schemas/2007'>" + "<id>#{video_id}</id></entry>" headers["Content-Length"] = new_row.bytesize.to_s uri = URI.parse("http://gdata.youtube.com/feeds/api/playlists/#{playlist_id}") http = Net::HTTP.new(uri.host, uri.port) post_response = http.post(uri.path, new_row, headers) end |
#add_videos_to_playlist(video_ids, playlist_id) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/yt_data_api.rb', line 101 def add_videos_to_playlist(video_ids, playlist_id) failed_to_add = [] video_ids.each_with_index do |video_id, index| post_response = add_video_to_playlist(video_id, playlist_id) if(post_response.code == "403") sleep(30) post_repsonse = add_video_to_playlist(video_id, playlist_id) end if(post_response.code != "201") failed_to_add << "#{index + 1},#{video_id},#{post_response.code}" end end if(failed_to_add.empty?) "201" else failed_to_add end end |
#create_playlist(playlist_name) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/yt_data_api.rb', line 17 def create_playlist(playlist_name) headers = {"Content-Type" => "application/atom+xml", "Authorization" => "GoogleLogin auth=#{@client_auth_key}", "X-GData-Key" => @dev_auth_key, "GData-Version" => "2"} new_row = "<?xml version='1.0' encoding='UTF-8'?>" + "<entry xmlns='http://www.w3.org/2005/Atom' " + "xmlns:yt='http://gdata.youtube.com/schemas/2007'>" + "<title type='text'>#{playlist_name}</title><summary></summary></entry>" headers["Content-Length"] = new_row.bytesize.to_s uri = URI.parse("http://gdata.youtube.com/feeds/api/users/default/playlists") http = Net::HTTP.new(uri.host, uri.port) post_response = http.post(uri.path, new_row, headers) post_response_content = parse_atom_xml(post_response) post_response_entry = post_response_content.root.find_first('yt:playlistId') playlist_id = post_response_entry.content return post_response, playlist_id end |
#delete_playlist(playlist_id) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/yt_data_api.rb', line 173 def delete_playlist(playlist_id) headers = {"Content-Type" => "application/atom+xml", "Authorization" => "GoogleLogin auth=#{@client_auth_key}", "X-GData-Key" => @dev_auth_key, "GData-Version" => "2"} uri = URI.parse("http://gdata.youtube.com/feeds/api/users/#{@client_user_id}/playlists/#{playlist_id}") http = Net::HTTP.new(uri.host, uri.port) delete_response = http.delete(uri.path, headers) if(delete_response.code != "200") raise "#{playlist_id} not deleted for #{@client_user_id} - #{delete_response.code}" end delete_response.code end |
#empty_playlist(playlist_id) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/yt_data_api.rb', line 151 def empty_playlist(playlist_id) headers = {"Content-Type" => "application/atom+xml", "Authorization" => "GoogleLogin auth=#{@client_auth_key}", "X-GData-Key" => @dev_auth_key, "GData-Version" => "2"} playlist_entries = self.get_client_playlist_entries(playlist_id) playlist_entries.each do |playlist_entry| uri = URI.parse("http://gdata.youtube.com/feeds/api/playlists/#{playlist_id}/#{playlist_entry}") http = Net::HTTP.new(uri.host, uri.port) delete_response = http.delete(uri.path, headers) if(delete_response.code != "200") raise "#{playlist_entry} not deleted from #{@playlist_id} - #{delete_response.code}" end end self.get_client_playlist_entries(playlist_id).empty? end |
#get_client_playlist_entries(playlist_id) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/yt_data_api.rb', line 124 def get_client_playlist_entries(playlist_id) headers = {"Content-Type" => "application/x-www-form-urlencoded", "Authorization" => "GoogleLogin auth=#{@client_auth_key}", "X-GData-Key" => @dev_auth_key} uri = URI.parse("http://gdata.youtube.com/feeds/api/playlists/#{playlist_id}?v=2") playlist_entries_feed = Net::HTTP.start(uri.host, uri.port){|http| http.get(uri.path, headers) } playlist_entries_content = parse_atom_xml(playlist_entries_feed) playlist_entries = playlist_entries_content.root.find('atom:entry') playlist_entries_ids = [] playlist_entries.each do |entry| entry_id = entry.find_first('atom:id') playlist_entry = entry_id.content[/http:\/\/.*\/(.+)/, 1] playlist_entries_ids << playlist_entry end playlist_entries_ids end |
#get_playlist_id(playlist_name) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/yt_data_api.rb', line 41 def get_playlist_id(playlist_name) headers = {"Content-Type" => "application/x-www-form-urlencoded", "Authorization" => "GoogleLogin auth=#{@client_auth_key}", "X-GData-Key" => @dev_auth_key} uri = URI.parse("http://gdata.youtube.com/feeds/api/users/default/playlists?v=2") playlists_feed = Net::HTTP.start(uri.host, uri.port){|http| http.get(uri.path, headers) } playlists_content = parse_atom_xml(playlists_feed) playlists_entries = playlists_content.root.find('atom:entry') playlists_entries.each do |entry| if(entry.find_first('atom:title').content.eql?(playlist_name)) return entry.find_first('yt:playlistId').content end end return nil end |
#get_video_ids(search_query, count = 1) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/yt_data_api.rb', line 64 def get_video_ids(search_query, count = 1) query_uri = URI.parse("http://gdata.youtube.com/feeds/api/videos?q=#{search_query}&max-results=#{count}") rss_query_result = parse_rss(query_uri) video_ids = [] rss_query_result.items.each do |item| video_id = item.id.to_s[/<id>http:\/\/(.*\/)(.*)<\/id>/, 2] return video_id if count == 1 video_ids << video_id end if(video_ids.empty?) nil else video_ids end end |