youtube 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/dirtywork.rb +66 -0
  2. data/youtube.rb +155 -0
  3. metadata +54 -0
@@ -0,0 +1,66 @@
1
+ #--
2
+ # Copyright (c) 2006 Shane Vitarana
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require 'net/http'
25
+ require 'xmlsimple'
26
+
27
+ HOST = 'http://youtube.com'
28
+ API = '/api2_rest'
29
+
30
+ # Module that does all the work to send out the HTTP request and retrieve
31
+ # the XML response. Inspired by the Flickr interface by Scott Raymond
32
+ # <http://redgreenblu.com/flickr/>.
33
+
34
+ module DirtyWork
35
+
36
+ # All API methods are implemented with this method.
37
+ # This method is like a remote method call, it encapsulates
38
+ # the request/response cycle to the remote host. It extracts
39
+ # the remote method API name based on the ruby method name.
40
+ private
41
+ def method_missing(method_id, *params)
42
+ # params[0] is the argument to the YouTube API
43
+ request(method_id.to_s.sub('_', '.'), *params)
44
+ end
45
+
46
+ private
47
+ def request(method, *params)
48
+ response = XmlSimple.xml_in(http_get(request_url(method, *params)), { 'ForceArray' => false })
49
+ raise response['error']['description'] if response['status'] != 'ok'
50
+ response
51
+ end
52
+
53
+ private
54
+ def request_url(method, *params)
55
+ param_list = String.new
56
+ params[0].each_pair do |k,v|
57
+ param_list << "&"+k.to_s+"="+v.to_s
58
+ end
59
+ url = "#{HOST}#{API}?method=youtube."+method+"&dev_id="+@dev_id+param_list
60
+ end
61
+
62
+ private
63
+ def http_get(url)
64
+ Net::HTTP.get_response(URI.parse(url)).body.to_s
65
+ end
66
+ end
@@ -0,0 +1,155 @@
1
+ #--
2
+ # Copyright (c) 2006 Shane Vitarana
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ # USAGE:
25
+ # require 'youtube'
26
+ # youtube = YouTube.new 'your dev id'
27
+ # profile = youtube.profile 'username'
28
+ # profile.age
29
+ # ...
30
+ # videos = youtube.videos_by_tag('malsky')
31
+ # video = video[0]
32
+ # video.title
33
+ # ...
34
+ # details = video.details
35
+ # details.channel_list
36
+
37
+ require 'dirtywork'
38
+
39
+ # YouTube client class. Requires a dev id.
40
+
41
+ class YouTube
42
+ include DirtyWork
43
+
44
+ def initialize(dev_id)
45
+ @dev_id = dev_id
46
+ end
47
+
48
+ # username = the user to get the profile for
49
+ def profile(username)
50
+ response = users_get_profile(:user => username)
51
+ Profile.new response['user_profile']
52
+ end
53
+
54
+ # tag = the tag to search for, must be one word
55
+ # optional: page = the "page" of results to retrieve (e.g. 1, 2, 3)
56
+ # optional: per_page = the number of results per page (default: 20, max 100)
57
+ def videos_by_tag(tag, page=1, per_page=20)
58
+ videos = Array.new
59
+ response = videos_list_by_tag(:tag => tag, :page => page, :per_page => per_page)
60
+ response['video_list']['video'].each do |video|
61
+ videos << Video.new(video, @dev_id)
62
+ end
63
+ videos
64
+ end
65
+
66
+ class Profile
67
+ attr_reader :first_name, :last_name, :about_me, :age, :video_upload_count,
68
+ :video_watch_count, :homepage, :hometown, :gender, :occupations,
69
+ :companies, :city, :country, :books, :hobbies, :movies,
70
+ :relationship, :friend_count, :favorite_video_count, :currently_on
71
+
72
+ def initialize(profile)
73
+ @first_name = profile['first_name']
74
+ @last_name = profile['last_name']
75
+ @about_me = profile['about_me']
76
+ @age = profile['age']
77
+ @video_upload_count = profile['video_upload_count']
78
+ @video_watch_count = profile['video_watch_count']
79
+ @homepage = profile['homepage']
80
+ @hometown = profile['hometown']
81
+ @gender = profile['gender']
82
+ @occupations = profile['occupations']
83
+ @companies = profile['companies']
84
+ @city = profile['city']
85
+ @country = profile['country']
86
+ @books = profile['books']
87
+ @hobbies = profile['hobbies']
88
+ @movies = profile['movies']
89
+ @relationship = profile['relationship']
90
+ @friend_count = profile['friend_count']
91
+ @favorite_video_count = profile['favorite_video_count']
92
+ @currently_on = profile['currently_on']
93
+ end
94
+ end
95
+
96
+ class Video
97
+ include DirtyWork
98
+ attr_reader :author, :id, :title, :length, :rating_avg, :rating_count,
99
+ :description, :view_count, :upload_time, :comment_count,
100
+ :tags, :url, :thumbnail_url, :embed_url
101
+
102
+ def initialize(video, dev_id)
103
+ @dev_id = dev_id
104
+ @author = video['author']
105
+ @id = video['id']
106
+ @title = video['title']
107
+ @length = video['length']
108
+ @rating_avg = video['rating_avg']
109
+ @rating_count = video['rating_count']
110
+ @description = video['description']
111
+ @view_count = video['view_count']
112
+ @upload_time = video['upload_time']
113
+ @comment_count = video['comment_count']
114
+ @tags = video['tags']
115
+ @url = video['url']
116
+ @thumbnail_url = video['thumbnail_url']
117
+
118
+ # the url from the API is made for viewing, not for embedding
119
+ # fix url to allow embedding
120
+ @embed_url = @url.delete('?').sub('=', '/')
121
+ end
122
+
123
+ def details
124
+ response = videos_get_details(:video_id => @id)
125
+ VideoDetails.new response['video_details']
126
+ end
127
+ end
128
+
129
+ class VideoDetails
130
+ attr_reader :author, :title, :rating_avg, :rating_count, :tags, :description,
131
+ :update_time, :view_count, :upload_time, :length_seconds,
132
+ :recording_date, :recoding_location, :recording_country,
133
+ :comment_list, :channel_list, :thumbnail_url
134
+
135
+ def initialize(details)
136
+ @author = details['author']
137
+ @title = details['title']
138
+ @rating_avg = details['rating_avg']
139
+ @rating_count = details['rating_count']
140
+ @tags = details['tags']
141
+ @description = details['description']
142
+ @update_time = details['update_time']
143
+ @view_count = details['view_count']
144
+ @upload_time = details['upload_time']
145
+ @length_seconds = details['length_seconds']
146
+ @recording_date = details['recording_date']
147
+ @recording_location = details['recording_location']
148
+ @recording_country = details['recording_country']
149
+ @comment_list = details['comment_list']
150
+ @channel_list = details['channel_list']
151
+ @thumbnail_url = details['thumbnail_url']
152
+ end
153
+ end
154
+
155
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: youtube
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2006-09-27 00:00:00 -05:00
8
+ summary: An interface in Ruby to the YouTube REST API. By Shane Vitarana.
9
+ require_paths:
10
+ - .
11
+ email: shanev@gmail.com
12
+ homepage: http://shanesbrain.net
13
+ rubyforge_project: youtube
14
+ description:
15
+ autorequire: youtube
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Shane Vitarana
30
+ files:
31
+ - youtube.rb
32
+ - dirtywork.rb
33
+ test_files: []
34
+
35
+ rdoc_options: []
36
+
37
+ extra_rdoc_files: []
38
+
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ requirements:
44
+ - YouTube Developer ID
45
+ dependencies:
46
+ - !ruby/object:Gem::Dependency
47
+ name: xml-simple
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Version::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.7
54
+ version: