Skip to content

Commit

Permalink
adding support for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
chebyte committed Jun 29, 2011
1 parent 2fb7c13 commit 0ac756c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/youtube_it.rb
Expand Up @@ -56,6 +56,7 @@ def self.create_default_logger
model/content
model/playlist
model/rating
model/subscription
model/thumbnail
model/user
model/video
Expand Down
8 changes: 8 additions & 0 deletions lib/youtube_it/client.rb
Expand Up @@ -182,6 +182,14 @@ def subscribe_channel(channel_name)
client.subscribe_channel(channel_name)
end

def unsubscribe_channel(subscription_id)
client.unsubscribe_channel(subscription_id)
end

def subscriptions(user_id = nil)
client.subscriptions(user_id)
end

def enable_http_debugging
client.enable_http_debugging
end
Expand Down
7 changes: 7 additions & 0 deletions lib/youtube_it/model/subscription.rb
@@ -0,0 +1,7 @@
class YouTubeIt
module Model
class Subscription < YouTubeIt::Record
attr_reader :id, :title, :published
end
end
end
28 changes: 26 additions & 2 deletions lib/youtube_it/parser.rb
Expand Up @@ -2,8 +2,7 @@ class YouTubeIt
module Parser #:nodoc:
class FeedParser #:nodoc:
def initialize(content)
p content
@content = open(content).read #rescue content
@content = open(content).read rescue content
end

def parse
Expand Down Expand Up @@ -120,6 +119,31 @@ def parse_content(content)
)
end
end

class SubscriptionFeedParser < FeedParser #:nodoc:

def parse_content(content)
doc = REXML::Document.new(content.body)
feed = doc.elements["feed"]

subscriptions = []
feed.elements.each("entry") do |entry|
subscriptions << parse_entry(entry)
end
return subscriptions
end

protected

def parse_entry(entry)
YouTubeIt::Model::Subscription.new(
:title => entry.elements["title"].text,
:id => entry.elements["id"].text[/subscription([^<]+)/, 1].sub(':',''),
:published => entry.elements["published"] ? entry.elements["published"].text : nil
)
end
end


class VideoFeedParser < FeedParser #:nodoc:

Expand Down
43 changes: 43 additions & 0 deletions lib/youtube_it/request/video_upload.rb
Expand Up @@ -503,6 +503,28 @@ def rate_video(video_id, rating)
{:code => response.code, :body => response.body}
end

def subscriptions(user_id)
subscription_url = "https://gdata.youtube.com/feeds/api/users/%s/subscriptions?v=2" % (user_id ? user_id : "default")

response = nil
header = {
"Content-Type" => "application/atom+xml; charset=UTF-8"
}

if @access_token.nil?
header.merge!(authorization_headers)
http_connection do |session|
response = session.get(subscription_url, header)
end
else
header.merge!(authorization_headers_for_oauth)
response = @access_token.get(subscription_url, header)
end

raise_on_faulty_response(response)
return YouTubeIt::Parser::SubscriptionFeedParser.new(response).parse
end

def subscribe_channel(channel_name)
response = nil
subscribe_body = video_xml_for(:subscribe => channel_name)
Expand All @@ -525,6 +547,27 @@ def subscribe_channel(channel_name)
{:code => response.code, :body => response.body}
end

def unsubscribe_channel(subscription_id)
response = nil
unsubscribe_header = {
"Content-Type" => "application/atom+xml"
}
unsubscribe_url = "/feeds/api/users/default/subscriptions/%s" % subscription_id

if @access_token.nil?
unsubscribe_header.merge!(authorization_headers)
http_connection do |session|
response = session.delete(unsubscribe_url, unsubscribe_header)
end
else
unsubscribe_header.merge!(authorization_headers_for_oauth)
response = @access_token.post("http://%s%s" % [base_url, unsubscribe_url], unsubscribe_header)
end
raise_on_faulty_response(response)
{:code => response.code, :body => response.body}
end


def favorites(opts = {})
favorite_url = "/feeds/api/users/default/favorites?#{opts.to_param}"
if @access_token.nil?
Expand Down

0 comments on commit 0ac756c

Please sign in to comment.