From ccc5f75b40c4a6147924b8bb016fa300c9d21736 Mon Sep 17 00:00:00 2001 From: Girish S Date: Tue, 24 May 2011 15:53:55 +0530 Subject: [PATCH] ratings (like/dislike) support --- lib/youtube_it/client.rb | 8 ++++++++ lib/youtube_it/request/video_upload.rb | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/youtube_it/client.rb b/lib/youtube_it/client.rb index 81a9049..1db2b25 100644 --- a/lib/youtube_it/client.rb +++ b/lib/youtube_it/client.rb @@ -170,6 +170,14 @@ def delete_playlist(playlist_id) client.delete_playlist(playlist_id) end + def like_video(video_id) + client.rate_video(video_id, 'like') + end + + def dislike_video(video_id) + client.rate_video(video_id, 'dislike') + end + def enable_http_debugging client.enable_http_debugging end diff --git a/lib/youtube_it/request/video_upload.rb b/lib/youtube_it/request/video_upload.rb index d4ab0b3..fed7ffa 100644 --- a/lib/youtube_it/request/video_upload.rb +++ b/lib/youtube_it/request/video_upload.rb @@ -417,6 +417,28 @@ def delete_playlist(playlist_id) return true end + def rate_video(video_id, rating) + response = nil + rating_body = video_xml_for(:rating => rating) + rating_header = { + "Content-Type" => "application/atom+xml", + "Content-Length" => "#{rating_body.length}", + } + rating_url = "/feeds/api/videos/#{video_id}/ratings" + + if @access_token.nil? + rating_header.merge!(authorization_headers) + http_connection do |session| + response = session.post(rating_url, rating_body, rating_header) + end + else + rating_header.merge!(authorization_headers_for_oauth) + response = @access_token.post("http://%s%s" % [base_url, rating_url], rating_body, rating_header) + end + raise_on_faulty_response(response) + {:code => response.code, :body => response.body} + end + def favorites favorite_url = "/feeds/api/users/default/favorites" http_connection do |session| @@ -572,6 +594,7 @@ def video_xml_for(data) b.entry(:xmlns => "http://www.w3.org/2005/Atom", 'xmlns:yt' => "http://gdata.youtube.com/schemas/2007") do | m | m.content(data[:comment]) if data[:comment] m.id(data[:favorite] || data[:playlist]) if data[:favorite] || data[:playlist] + m.tag!("yt:rating", :value => data[:rating]) if data[:rating] end.to_s end