Class: RubyTube

Inherits:
YTClient show all
Defined in:
lib/ruby_tube.rb

Constant Summary

Constants inherited from YTClient

YTClient::UPLOAD_URI

Instance Attribute Summary

Attributes inherited from YTClient

#client, #developer_key, #password, #token, #username

Instance Method Summary collapse

Methods inherited from YTClient

#all, #check_video, #delete, #update, #upload

Constructor Details

#initialize(username, password, key, options = {:refresh=>300}) ⇒ RubyTube

Returns a new instance of RubyTube.



9
10
11
# File 'lib/ruby_tube.rb', line 9

def initialize(username, password, key, options={:refresh=>300})
	super(username, password, key, options)
end

Instance Method Details

#comments(id) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ruby_tube.rb', line 72

def comments(id)
	xml = super
	comments = Array.new
	if (xml/"entry").nitems > 0
		(xml/"entry").each do |entry|
			comment = 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 << comment
		end
	end
	return comments
end

#countObject



68
69
70
# File 'lib/ruby_tube.rb', line 68

def count
	super
end

#delete_video(id) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/ruby_tube.rb', line 128

def delete_video(id)
	response = delete(id)
	if response.status_code == 200
		return true
	else
		return false
	end
end

#find(id) ⇒ Object



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

def find(id)
	xml = check_video(id)
	entry = (xml/"entry")
	status = (xml/"yt:state").empty? ? "ok" : (xml/"yt:state").attr("name")
	video = 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").empty? ? 0 : (entry/"yt:statistics").attr("viewCount"),
		:favorite_count => (entry/"yt:statistics").empty? ? 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)
	})
	return video
end

#find_allObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_tube.rb', line 39

def find_all
	@all = all()
	videos = Array.new
	(all/"entry").each do |entry|
		status = (entry/"yt:state").empty? ? "ok" : (entry/"yt:state").attr("name")
		video = 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").empty? ? 0 : (entry/"yt:statistics").attr("viewCount"),
			:favorite_count => (entry/"yt:statistics").empty? ? 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)
		})
		videos << video
	end
	return videos
end

#ratings(id) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ruby_tube.rb', line 90

def ratings(id)
	xml = super
	rating = nil
	if xml
		rating = YTRating.new({
			:num_raters => xml.attr("numRaters").to_i,
			:max => xml.attr("max").to_i,
			:min => xml.attr("min").to_i,
			:average => xml.attr("average").to_f
		})
	end
	return rating
end

#update_video(id, options) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ruby_tube.rb', line 104

def update_video(id, options)
	video = find(id)
	if options[:title]
		video.title = options[:title]
	end
	if options[:description]
		video.description = options[:description]
	end
	if options[:keywords]
		video.keywords = options[:keywords]
	end
	entry = video.to_xml
	response = update(video.id, entry)
	if response.status_code == 200
		return video
	else
		return false
	end
end

#upload_video(filename, options = {}) ⇒ Object



124
125
126
# File 'lib/ruby_tube.rb', line 124

def upload_video(filename, options={})
	response = upload(filename, options)
end