Class: RubyTubeNoAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_tube_no_auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dev_key = "") ⇒ RubyTubeNoAuth

Returns a new instance of RubyTubeNoAuth.



4
5
6
7
8
9
10
# File 'lib/ruby_tube_no_auth.rb', line 4

def initialize(dev_key="")
	@client = GData::Client::YouTube.new
	@client.source = "RubyTube"
	if dev_key
		@client.developer_key = dev_key
	end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



2
3
4
# File 'lib/ruby_tube_no_auth.rb', line 2

def client
  @client
end

Instance Method Details

#comments(id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby_tube_no_auth.rb', line 38

def comments(id)
	res = @client.get("http://gdata.youtube.com/feeds/api/videos/#{id}/comments")
	xml = Hpricot.XML(res.body)
	comments = Array.new
	if (xml/"entry").nitems > 0
		(xml/"entry").each do |entry|
			cmt = YTComment.new({
				:title => (entry/"title").text,
				:content => (entry/"content").text,
				:author => (entry/"author").search("name").text,
				:author_uri => (entry/"author").search("uri").text,
				:video_uri => (entry/"link[@rel='related']").attr("href")
			})
			comments << cmt
		end
	end
	comments
end

#find(id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_tube_no_auth.rb', line 12

def find(id)
	res = @client.get("http://gdata.youtube.com/feeds/api/videos/#{id}")
	xml = Hpricot.XML(res.body)
	entry = xml.at("entry")
	vid = YTVideo.new({
		:id => (entry/"yt:videoid").text,
		:title => (entry/"title").text,
		:description => (entry/"media:description").text,
		:keywords => (entry/"media:keywords").text,
		:duration => (entry/"yt:duration").attr("seconds").to_i,
		:player_uri => (entry/"link[@rel='alternate']").attr("href"),
		:ratings_uri => (entry/"link[@rel$='ratings']").attr("href"),
		:comments_uri => (entry/"gd:comments").search("gd:feedlink").attr("href"),
		:comment_count => (entry/"gd:comments").search("gd:feedlink").attr("countHint").to_i,
		:published_at => Time.parse((entry/"published").text),
		:updated_at => Time.parse((entry/"updated").text),
		:view_count => (entry/"yt:statistics").nil? ? 0 : (entry/"yt:statistics").attr("viewCount"),
		:favorite_count => (entry/"yt:statistics").nil? ? 0 : (entry/"yt:statistics").attr("favoriteCount"),
		:comments => comments((entry/"yt:videoid").text),
		:ratings => ratings((entry/"yt:videoid").text),
		:status => status,
		:thumbnails => process_thumbnail_urls(entry)
	})
	vid
end

#ratings(id) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/ruby_tube_no_auth.rb', line 57

def ratings(id)
	response = Hpricot.XML(@client.get("http://gdata.youtube.com/feeds/api/videos/#{id}").body)
	ratings = (response/"gd:rating")
	if ratings.nitems > 0
		return ratings
	else
		return nil
	end
end