youtube_it 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +52 -0
- data/Manifest.txt +32 -0
- data/README.txt +123 -0
- data/Rakefile +59 -0
- data/TODO.txt +16 -0
- data/VERSION +1 -0
- data/lib/youtube_it.rb +67 -0
- data/lib/youtube_it/chain_io.rb +71 -0
- data/lib/youtube_it/client.rb +114 -0
- data/lib/youtube_it/model/author.rb +11 -0
- data/lib/youtube_it/model/category.rb +11 -0
- data/lib/youtube_it/model/contact.rb +16 -0
- data/lib/youtube_it/model/content.rb +18 -0
- data/lib/youtube_it/model/playlist.rb +8 -0
- data/lib/youtube_it/model/rating.rb +17 -0
- data/lib/youtube_it/model/thumbnail.rb +17 -0
- data/lib/youtube_it/model/user.rb +20 -0
- data/lib/youtube_it/model/video.rb +194 -0
- data/lib/youtube_it/parser.rb +175 -0
- data/lib/youtube_it/record.rb +12 -0
- data/lib/youtube_it/request/base_search.rb +26 -0
- data/lib/youtube_it/request/standard_search.rb +40 -0
- data/lib/youtube_it/request/user_search.rb +43 -0
- data/lib/youtube_it/request/video_search.rb +93 -0
- data/lib/youtube_it/request/video_upload.rb +237 -0
- data/lib/youtube_it/response/video_search.rb +41 -0
- data/lib/youtube_it/version.rb +4 -0
- data/pkg/youtube_it-0.0.1.gem +0 -0
- data/test/helper.rb +6 -0
- data/test/test.mov +0 -0
- data/test/test_chain_io.rb +63 -0
- data/test/test_client.rb +301 -0
- data/test/test_video.rb +38 -0
- data/test/test_video_search.rb +134 -0
- data/youtube_it.gemspec +78 -0
- metadata +100 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
class YouTubeIt
|
2
|
+
module Request #:nodoc:
|
3
|
+
class BaseSearch #:nodoc:
|
4
|
+
attr_reader :url
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def base_url
|
9
|
+
"http://gdata.youtube.com/feeds/api/"
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_instance_variables( variables )
|
13
|
+
variables.each do |key, value|
|
14
|
+
name = key.to_s
|
15
|
+
instance_variable_set("@#{name}", value) if respond_to?(name)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def build_query_params(params)
|
20
|
+
qs = params.to_a.map { | k, v | v.nil? ? nil : "#{YouTubeIt.esc(k)}=#{YouTubeIt.esc(v)}" }.compact.sort.join('&')
|
21
|
+
qs.empty? ? '' : "?#{qs}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class YouTubeIt
|
2
|
+
module Request #:nodoc:
|
3
|
+
class StandardSearch < BaseSearch #:nodoc:
|
4
|
+
attr_reader :max_results # max_results
|
5
|
+
attr_reader :order_by # orderby, ([relevance], viewCount, published, rating)
|
6
|
+
attr_reader :offset # start-index
|
7
|
+
attr_reader :time # time
|
8
|
+
|
9
|
+
TYPES = [ :top_rated, :top_favorites, :most_viewed, :most_popular,
|
10
|
+
:most_recent, :most_discussed, :most_linked, :most_responded,
|
11
|
+
:recently_featured, :watch_on_mobile ]
|
12
|
+
|
13
|
+
def initialize(type, options={})
|
14
|
+
if TYPES.include?(type)
|
15
|
+
@max_results, @order_by, @offset, @time = nil
|
16
|
+
set_instance_variables(options)
|
17
|
+
@url = base_url + type.to_s << build_query_params(to_youtube_params)
|
18
|
+
else
|
19
|
+
raise "Invalid type, must be one of: #{ TYPES.map { |t| t.to_s }.join(", ") }"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def base_url
|
26
|
+
super << "standardfeeds/"
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_youtube_params
|
30
|
+
{
|
31
|
+
'max-results' => @max_results,
|
32
|
+
'orderby' => @order_by,
|
33
|
+
'start-index' => @offset,
|
34
|
+
'time' => @time
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class YouTubeIt
|
2
|
+
module Request #:nodoc:
|
3
|
+
class UserSearch < BaseSearch #:nodoc:
|
4
|
+
attr_reader :max_results # max_results
|
5
|
+
attr_reader :order_by # orderby, ([relevance], viewCount, published, rating)
|
6
|
+
attr_reader :offset # start-index
|
7
|
+
|
8
|
+
def initialize(params, options={})
|
9
|
+
@max_results, @order_by, @offset = nil
|
10
|
+
@url = base_url
|
11
|
+
|
12
|
+
if params == :favorites
|
13
|
+
@url << "#{options[:user]}/favorites"
|
14
|
+
set_instance_variables(options)
|
15
|
+
elsif params[:user] && options[:favorites]
|
16
|
+
@url << "#{params[:user]}/favorites"
|
17
|
+
set_instance_variables(params)
|
18
|
+
return
|
19
|
+
elsif params[:user]
|
20
|
+
@url << "#{params[:user]}/uploads"
|
21
|
+
set_instance_variables(params)
|
22
|
+
end
|
23
|
+
|
24
|
+
@url << build_query_params(to_youtube_params)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def base_url
|
30
|
+
super << "users/"
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_youtube_params
|
34
|
+
{
|
35
|
+
'max-results' => @max_results,
|
36
|
+
'orderby' => @order_by,
|
37
|
+
'start-index' => @offset
|
38
|
+
}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
class YouTubeIt
|
2
|
+
module Request #:nodoc:
|
3
|
+
class VideoSearch < BaseSearch #:nodoc:
|
4
|
+
# From here: http://code.google.com/apis/youtube/reference.html#yt_format
|
5
|
+
ONLY_EMBEDDABLE = 5
|
6
|
+
|
7
|
+
attr_reader :max_results # max_results
|
8
|
+
attr_reader :order_by # orderby, ([relevance], viewCount, published, rating)
|
9
|
+
attr_reader :offset # start-index
|
10
|
+
attr_reader :query # vq
|
11
|
+
attr_reader :response_format # alt, ([atom], rss, json)
|
12
|
+
attr_reader :tags # /-/tag1/tag2
|
13
|
+
attr_reader :categories # /-/Category1/Category2
|
14
|
+
attr_reader :video_format # format (1=mobile devices)
|
15
|
+
attr_reader :racy # racy ([exclude], include)
|
16
|
+
attr_reader :author
|
17
|
+
|
18
|
+
def initialize(params={})
|
19
|
+
# Initialize our various member data to avoid warnings and so we'll
|
20
|
+
# automatically fall back to the youtube api defaults
|
21
|
+
@max_results, @order_by,
|
22
|
+
@offset, @query,
|
23
|
+
@response_format, @video_format,
|
24
|
+
@racy, @author = nil
|
25
|
+
@url = base_url
|
26
|
+
|
27
|
+
# Return a single video (base_url + /T7YazwP8GtY)
|
28
|
+
return @url << "/" << params[:video_id] if params[:video_id]
|
29
|
+
|
30
|
+
@url << "/-/" if (params[:categories] || params[:tags])
|
31
|
+
@url << categories_to_params(params.delete(:categories)) if params[:categories]
|
32
|
+
@url << tags_to_params(params.delete(:tags)) if params[:tags]
|
33
|
+
|
34
|
+
set_instance_variables(params)
|
35
|
+
|
36
|
+
if( params[ :only_embeddable ] )
|
37
|
+
@video_format = ONLY_EMBEDDABLE
|
38
|
+
end
|
39
|
+
|
40
|
+
@url << build_query_params(to_youtube_params)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def base_url
|
46
|
+
super << "videos"
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_youtube_params
|
50
|
+
{
|
51
|
+
'max-results' => @max_results,
|
52
|
+
'orderby' => @order_by,
|
53
|
+
'start-index' => @offset,
|
54
|
+
'vq' => @query,
|
55
|
+
'alt' => @response_format,
|
56
|
+
'format' => @video_format,
|
57
|
+
'racy' => @racy,
|
58
|
+
'author' => @author
|
59
|
+
}
|
60
|
+
end
|
61
|
+
|
62
|
+
# Convert category symbols into strings and build the URL. GData requires categories to be capitalized.
|
63
|
+
# Categories defined like: categories => { :include => [:news], :exclude => [:sports], :either => [..] }
|
64
|
+
# or like: categories => [:news, :sports]
|
65
|
+
def categories_to_params(categories)
|
66
|
+
if categories.respond_to?(:keys) and categories.respond_to?(:[])
|
67
|
+
s = ""
|
68
|
+
s << categories[:either].map { |c| c.to_s.capitalize }.join("%7C") << '/' if categories[:either]
|
69
|
+
s << categories[:include].map { |c| c.to_s.capitalize }.join("/") << '/' if categories[:include]
|
70
|
+
s << ("-" << categories[:exclude].map { |c| c.to_s.capitalize }.join("/-")) << '/' if categories[:exclude]
|
71
|
+
s
|
72
|
+
else
|
73
|
+
categories.map { |c| c.to_s.capitalize }.join("/") << '/'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Tags defined like: tags => { :include => [:football], :exclude => [:soccer], :either => [:polo, :tennis] }
|
78
|
+
# or tags => [:football, :soccer]
|
79
|
+
def tags_to_params(tags)
|
80
|
+
if tags.respond_to?(:keys) and tags.respond_to?(:[])
|
81
|
+
s = ""
|
82
|
+
s << tags[:either].map { |t| YouTubeIt.esc(t.to_s) }.join("%7C") << '/' if tags[:either]
|
83
|
+
s << tags[:include].map { |t| YouTubeIt.esc(t.to_s) }.join("/") << '/' if tags[:include]
|
84
|
+
s << ("-" << tags[:exclude].map { |t| YouTubeIt.esc(t.to_s) }.join("/-")) << '/' if tags[:exclude]
|
85
|
+
s
|
86
|
+
else
|
87
|
+
tags.map { |t| YouTubeIt.esc(t.to_s) }.join("/") << '/'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,237 @@
|
|
1
|
+
class YouTubeIt
|
2
|
+
|
3
|
+
module Upload
|
4
|
+
class UploadError < YouTubeIt::Error; end
|
5
|
+
class AuthenticationError < YouTubeIt::Error; end
|
6
|
+
|
7
|
+
# Implements video uploads/updates/deletions
|
8
|
+
#
|
9
|
+
# require 'youtube_it'
|
10
|
+
#
|
11
|
+
# uploader = YouTubeIt::Upload::VideoUpload.new("user", "pass", "dev-key")
|
12
|
+
# uploader.upload File.open("test.m4v"), :title => 'test',
|
13
|
+
# :description => 'cool vid d00d',
|
14
|
+
# :category => 'People',
|
15
|
+
# :keywords => %w[cool blah test]
|
16
|
+
#
|
17
|
+
class VideoUpload
|
18
|
+
|
19
|
+
def initialize user, pass, dev_key, client_id = 'youtube_it'
|
20
|
+
@user, @pass, @dev_key, @client_id = user, pass, dev_key, client_id
|
21
|
+
end
|
22
|
+
|
23
|
+
#
|
24
|
+
# Upload "data" to youtube, where data is either an IO object or
|
25
|
+
# raw file data.
|
26
|
+
# The hash keys for opts (which specify video info) are as follows:
|
27
|
+
# :mime_type
|
28
|
+
# :filename
|
29
|
+
# :title
|
30
|
+
# :description
|
31
|
+
# :category
|
32
|
+
# :keywords
|
33
|
+
# :private
|
34
|
+
# Specifying :private will make the video private, otherwise it will be public.
|
35
|
+
#
|
36
|
+
# When one of the fields is invalid according to YouTube,
|
37
|
+
# an UploadError will be raised. Its message contains a list of newline separated
|
38
|
+
# errors, containing the key and its error code.
|
39
|
+
#
|
40
|
+
# When the authentication credentials are incorrect, an AuthenticationError will be raised.
|
41
|
+
def upload data, opts = {}
|
42
|
+
@opts = { :mime_type => 'video/mp4',
|
43
|
+
:title => '',
|
44
|
+
:description => '',
|
45
|
+
:category => '',
|
46
|
+
:keywords => [] }.merge(opts)
|
47
|
+
|
48
|
+
@opts[:filename] ||= generate_uniq_filename_from(data)
|
49
|
+
|
50
|
+
post_body_io = generate_upload_io(video_xml, data)
|
51
|
+
|
52
|
+
upload_headers = authorization_headers.merge({
|
53
|
+
"Slug" => "#{@opts[:filename]}",
|
54
|
+
"Content-Type" => "multipart/related; boundary=#{boundary}",
|
55
|
+
"Content-Length" => "#{post_body_io.expected_length}", # required per YouTube spec
|
56
|
+
# "Transfer-Encoding" => "chunked" # We will stream instead of posting at once
|
57
|
+
})
|
58
|
+
|
59
|
+
path = '/feeds/api/users/%s/uploads' % @user
|
60
|
+
|
61
|
+
Net::HTTP.start(uploads_url) do | session |
|
62
|
+
|
63
|
+
# Use the chained IO as body so that Net::HTTP reads into the socket for us
|
64
|
+
post = Net::HTTP::Post.new(path, upload_headers)
|
65
|
+
post.body_stream = post_body_io
|
66
|
+
|
67
|
+
response = session.request(post)
|
68
|
+
raise_on_faulty_response(response)
|
69
|
+
|
70
|
+
return uploaded_video_id_from(response.body)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Updates a video in YouTube. Requires:
|
75
|
+
# :title
|
76
|
+
# :description
|
77
|
+
# :category
|
78
|
+
# :keywords
|
79
|
+
# The following are optional attributes:
|
80
|
+
# :private
|
81
|
+
# When the authentication credentials are incorrect, an AuthenticationError will be raised.
|
82
|
+
def update(video_id, options)
|
83
|
+
@opts = options
|
84
|
+
|
85
|
+
update_body = video_xml
|
86
|
+
|
87
|
+
update_header = authorization_headers.merge({
|
88
|
+
"Content-Type" => "application/atom+xml",
|
89
|
+
"Content-Length" => "#{update_body.length}",
|
90
|
+
})
|
91
|
+
|
92
|
+
update_url = "/feeds/api/users/#{@user}/uploads/#{video_id}"
|
93
|
+
|
94
|
+
Net::HTTP.start(base_url) do | session |
|
95
|
+
response = session.put(update_url, update_body, update_header)
|
96
|
+
raise_on_faulty_response(response)
|
97
|
+
|
98
|
+
return YouTubeIt::Parser::VideoFeedParser.new(response.body).parse
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Delete a video on YouTube
|
103
|
+
def delete(video_id)
|
104
|
+
delete_header = authorization_headers.merge({
|
105
|
+
"Content-Type" => "application/atom+xml; charset=UTF-8",
|
106
|
+
"Content-Length" => "0",
|
107
|
+
})
|
108
|
+
|
109
|
+
delete_url = "/feeds/api/users/#{@user}/uploads/#{video_id}"
|
110
|
+
|
111
|
+
Net::HTTP.start(base_url) do |session|
|
112
|
+
response = session.delete(delete_url, delete_header)
|
113
|
+
raise_on_faulty_response(response)
|
114
|
+
return true
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def get_upload_token(options, nexturl)
|
119
|
+
@opts = options
|
120
|
+
|
121
|
+
token_body = video_xml
|
122
|
+
token_header = authorization_headers.merge({
|
123
|
+
"Content-Type" => "application/atom+xml; charset=UTF-8",
|
124
|
+
"Content-Length" => "#{token_body.length}",
|
125
|
+
})
|
126
|
+
token_url = "/action/GetUploadToken"
|
127
|
+
|
128
|
+
Net::HTTP.start(base_url) do | session |
|
129
|
+
response = session.post(token_url, token_body, token_header)
|
130
|
+
raise_on_faulty_response(response)
|
131
|
+
return {:url => "#{response.body[/<url>(.+)<\/url>/, 1]}?nexturl=#{nexturl}",
|
132
|
+
:token => response.body[/<token>(.+)<\/token>/, 1]}
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
private
|
137
|
+
|
138
|
+
def uploads_url
|
139
|
+
["uploads", base_url].join('.')
|
140
|
+
end
|
141
|
+
|
142
|
+
def base_url
|
143
|
+
"gdata.youtube.com"
|
144
|
+
end
|
145
|
+
|
146
|
+
def boundary
|
147
|
+
"An43094fu"
|
148
|
+
end
|
149
|
+
|
150
|
+
def authorization_headers
|
151
|
+
{
|
152
|
+
"Authorization" => "GoogleLogin auth=#{auth_token}",
|
153
|
+
"X-GData-Client" => "#{@client_id}",
|
154
|
+
"X-GData-Key" => "key=#{@dev_key}"
|
155
|
+
}
|
156
|
+
end
|
157
|
+
|
158
|
+
def parse_upload_error_from(string)
|
159
|
+
REXML::Document.new(string).elements["//errors"].inject('') do | all_faults, error|
|
160
|
+
location = error.elements["location"].text[/media:group\/media:(.*)\/text\(\)/,1]
|
161
|
+
code = error.elements["code"].text
|
162
|
+
all_faults + sprintf("%s: %s\n", location, code)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def raise_on_faulty_response(response)
|
167
|
+
if response.code.to_i == 403
|
168
|
+
raise AuthenticationError, response.body[/<TITLE>(.+)<\/TITLE>/, 1]
|
169
|
+
elsif response.code.to_i / 10 != 20 # Response in 20x means success
|
170
|
+
raise UploadError, parse_upload_error_from(response.body)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def uploaded_video_id_from(string)
|
175
|
+
xml = REXML::Document.new(string)
|
176
|
+
xml.elements["//id"].text[/videos\/(.+)/, 1]
|
177
|
+
end
|
178
|
+
|
179
|
+
# If data can be read, use the first 1024 bytes as filename. If data
|
180
|
+
# is a file, use path. If data is a string, checksum it
|
181
|
+
def generate_uniq_filename_from(data)
|
182
|
+
if data.respond_to?(:path)
|
183
|
+
Digest::MD5.hexdigest(data.path)
|
184
|
+
elsif data.respond_to?(:read)
|
185
|
+
chunk = data.read(1024)
|
186
|
+
data.rewind
|
187
|
+
Digest::MD5.hexdigest(chunk)
|
188
|
+
else
|
189
|
+
Digest::MD5.hexdigest(data)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def auth_token
|
194
|
+
@auth_token ||= begin
|
195
|
+
http = Net::HTTP.new("www.google.com", 443)
|
196
|
+
http.use_ssl = true
|
197
|
+
body = "Email=#{YouTubeIt.esc @user}&Passwd=#{YouTubeIt.esc @pass}&service=youtube&source=#{YouTubeIt.esc @client_id}"
|
198
|
+
response = http.post("/youtube/accounts/ClientLogin", body, "Content-Type" => "application/x-www-form-urlencoded")
|
199
|
+
raise UploadError, response.body[/Error=(.+)/,1] if response.code.to_i != 200
|
200
|
+
@auth_token = response.body[/Auth=(.+)/, 1]
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# TODO: isn't there a cleaner way to output top-notch XML without requiring stuff all over the place?
|
205
|
+
def video_xml
|
206
|
+
b = Builder::XmlMarkup.new
|
207
|
+
b.instruct!
|
208
|
+
b.entry(:xmlns => "http://www.w3.org/2005/Atom", 'xmlns:media' => "http://search.yahoo.com/mrss/", 'xmlns:yt' => "http://gdata.youtube.com/schemas/2007") do | m |
|
209
|
+
m.tag!("media:group") do | mg |
|
210
|
+
mg.tag!("media:title", @opts[:title], :type => "plain")
|
211
|
+
mg.tag!("media:description", @opts[:description], :type => "plain")
|
212
|
+
mg.tag!("media:keywords", @opts[:keywords].join(","))
|
213
|
+
mg.tag!('media:category', @opts[:category], :scheme => "http://gdata.youtube.com/schemas/2007/categories.cat")
|
214
|
+
mg.tag!('yt:private') if @opts[:private]
|
215
|
+
end
|
216
|
+
end.to_s
|
217
|
+
end
|
218
|
+
|
219
|
+
def generate_upload_io(video_xml, data)
|
220
|
+
post_body = [
|
221
|
+
"--#{boundary}\r\n",
|
222
|
+
"Content-Type: application/atom+xml; charset=UTF-8\r\n\r\n",
|
223
|
+
video_xml,
|
224
|
+
"\r\n--#{boundary}\r\n",
|
225
|
+
"Content-Type: #{@opts[:mime_type]}\r\nContent-Transfer-Encoding: binary\r\n\r\n",
|
226
|
+
data,
|
227
|
+
"\r\n--#{boundary}--\r\n",
|
228
|
+
]
|
229
|
+
|
230
|
+
# Use Greedy IO to not be limited by 1K chunks
|
231
|
+
YouTubeIt::GreedyChainIO.new(post_body)
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|