msp-youtube-g 0.4.7 → 0.4.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,10 @@
1
+ require 'logger'
2
+ require 'open-uri'
3
+ require 'net/https'
4
+ require 'digest/md5'
5
+ require 'rexml/document'
6
+ require 'cgi'
7
+
1
8
  require File.dirname(__FILE__) + '/youtube_g/client'
2
9
  require File.dirname(__FILE__) + '/youtube_g/record'
3
10
  require File.dirname(__FILE__) + '/youtube_g/parser'
@@ -9,12 +16,15 @@ require File.dirname(__FILE__) + '/youtube_g/model/playlist'
9
16
  require File.dirname(__FILE__) + '/youtube_g/model/rating'
10
17
  require File.dirname(__FILE__) + '/youtube_g/model/thumbnail'
11
18
  require File.dirname(__FILE__) + '/youtube_g/model/user'
12
- require File.dirname(__FILE__) + '/youtube_g/model/video'
19
+ require File.dirname(__FILE__) + '/youtube_g/model/video'
13
20
  require File.dirname(__FILE__) + '/youtube_g/model/upload_error'
21
+ require File.dirname(__FILE__) + '/youtube_g/request/base_search'
22
+ require File.dirname(__FILE__) + '/youtube_g/request/user_search'
23
+ require File.dirname(__FILE__) + '/youtube_g/request/standard_search'
14
24
  require File.dirname(__FILE__) + '/youtube_g/request/video_upload'
15
25
  require File.dirname(__FILE__) + '/youtube_g/request/video_search'
16
26
  require File.dirname(__FILE__) + '/youtube_g/response/video_search'
17
27
 
18
- class YouTubeG
19
- VERSION = '0.4.7'
28
+ class YouTubeG #:nodoc:
29
+ VERSION = '0.4.8.1'
20
30
  end
@@ -1,36 +1,81 @@
1
- require 'logger'
2
-
3
1
  class YouTubeG
4
2
  class Client
5
3
  attr_accessor :logger
6
4
 
7
- def initialize(logger=Logger.new(STDOUT))
8
- @logger = logger
5
+ def initialize(logger=false)
6
+ @logger = Logger.new(STDOUT) if logger
9
7
  end
10
-
11
- # Params can be one of :most_viewed, :top_rated, :recently_featured, :watch_on_mobile
12
- # Or :tags, :categories, :query, :user
8
+
9
+ # Retrieves an array of standard feed, custom query, or user videos.
10
+ #
11
+ # === Parameters
12
+ # If fetching videos for a standard feed:
13
+ # params<Symbol>:: Accepts a symbol of :top_rated, :top_favorites, :most_viewed,
14
+ # :most_popular, :most_recent, :most_discussed, :most_linked,
15
+ # :most_responded, :recently_featured, and :watch_on_mobile.
16
+ #
17
+ # You can find out more specific information about what each standard feed provides
18
+ # by visiting: http://code.google.com/apis/youtube/reference.html#Standard_feeds
19
+ #
20
+ # options<Hash> (optional):: Accepts the options of :time, :page (default is 1),
21
+ # and :per_page (default is 25). :offset and :max_results
22
+ # can also be passed for a custom offset.
23
+ #
24
+ # If fetching videos by tags, categories, query:
25
+ # params<Hash>:: Accepts the keys :tags, :categories, :query, :order_by,
26
+ # :author, :racy, :response_format, :video_format, :page (default is 1),
27
+ # and :per_page(default is 25)
28
+ #
29
+ # options<Hash>:: Not used. (Optional)
30
+ #
31
+ # If fetching videos for a particular user:
32
+ # params<Hash>:: Key of :user with a value of the username.
33
+ # options<Hash>:: Not used. (Optional)
34
+ # === Returns
35
+ # YouTubeG::Response::VideoSearch
13
36
  def videos_by(params, options={})
37
+ request_params = params.respond_to?(:to_hash) ? params : options
38
+ request_params[:page] ||= 1
39
+
40
+ unless request_params[:max_results]
41
+ request_params[:max_results] = request_params[:per_page] || 25
42
+ end
43
+
44
+ unless request_params[:offset]
45
+ request_params[:offset] = calculate_offset(request_params[:page], request_params[:max_results] )
46
+ end
47
+
14
48
  if params.respond_to?(:to_hash) and not params[:user]
15
- request = YouTubeG::Request::VideoSearch.new(params)
16
-
49
+ request = YouTubeG::Request::VideoSearch.new(request_params)
17
50
  elsif (params.respond_to?(:to_hash) && params[:user]) || (params == :favorites)
18
- request = YouTubeG::Request::UserSearch.new(params, options)
19
-
51
+ request = YouTubeG::Request::UserSearch.new(request_params, options)
20
52
  else
21
- request = YouTubeG::Request::StandardSearch.new(params, options)
53
+ request = YouTubeG::Request::StandardSearch.new(params, request_params)
22
54
  end
23
55
 
24
- logger.debug "Submitting request [url=#{request.url}]."
56
+ logger.debug "Submitting request [url=#{request.url}]." if logger
25
57
  parser = YouTubeG::Parser::VideosFeedParser.new(request.url)
26
58
  parser.parse
27
59
  end
28
60
 
61
+ # Retrieves a single YouTube video.
62
+ #
63
+ # === Parameters
64
+ # vid<String>:: The ID or URL of the video that you'd like to retrieve.
65
+ #
66
+ # === Returns
67
+ # YouTubeG::Model::Video
29
68
  def video_by(vid)
30
69
  video_id = vid =~ /^http/ ? vid : "http://gdata.youtube.com/feeds/videos/#{vid}"
31
70
  parser = YouTubeG::Parser::VideoFeedParser.new(video_id)
32
71
  parser.parse
33
72
  end
34
73
 
74
+ private
75
+
76
+ def calculate_offset(page, per_page)
77
+ page == 1 ? 1 : ((per_page * page) - per_page + 1)
78
+ end
79
+
35
80
  end
36
81
  end
@@ -1,5 +1,3 @@
1
- require 'logger'
2
-
3
1
  class YouTubeG
4
2
 
5
3
  # TODO: Why is this needed? Does this happen if running standalone w/o Rails?
@@ -1,7 +1,10 @@
1
1
  class YouTubeG
2
2
  module Model
3
3
  class Author < YouTubeG::Record
4
+ # *String*: Author's YouTube username.
4
5
  attr_reader :name
6
+
7
+ # *String*: Feed URL of the author.
5
8
  attr_reader :uri
6
9
  end
7
10
  end
@@ -1,7 +1,10 @@
1
1
  class YouTubeG
2
2
  module Model
3
3
  class Category < YouTubeG::Record
4
- attr_reader :label
4
+ # *String*:: Name of the YouTube category
5
+ attr_reader :label
6
+
7
+ # *String*:: Identifies the type of item described.
5
8
  attr_reader :term
6
9
  end
7
10
  end
@@ -1,7 +1,15 @@
1
1
  class YouTubeG
2
2
  module Model
3
3
  class Contact < YouTubeG::Record
4
+ # *String*:: Identifies the status of a contact.
5
+ #
6
+ # * The tag's value will be accepted if the authenticated user and the contact have marked each other as friends.
7
+ # * The tag's value will be requested if the contact has asked to be added to the authenticated user's contact list, but the request has not yet been accepted (or rejected).
8
+ # * The tag's value will be pending if the authenticated user has asked to be added to the contact's contact list, but the request has not yet been accepted or rejected.
9
+ #
4
10
  attr_reader :status
11
+
12
+ # *String*:: The Youtube username of the contact.
5
13
  attr_reader :username
6
14
  end
7
15
  end
@@ -1,10 +1,15 @@
1
1
  class YouTubeG
2
2
  module Model
3
3
  class Content < YouTubeG::Record
4
+ # *Boolean*:: Description of the video.
4
5
  attr_reader :default
6
+ # *Fixnum*:: Length of the video in seconds.
5
7
  attr_reader :duration
8
+ # YouTubeG::Model::Video::Format:: Specifies the video format of the video object
6
9
  attr_reader :format
10
+ # *String*:: Specifies the MIME type of the media object.
7
11
  attr_reader :mime_type
12
+ # *String*:: Specifies the URL for the media object.
8
13
  attr_reader :url
9
14
 
10
15
  alias :is_default? :default
@@ -1,6 +1,7 @@
1
1
  class YouTubeG
2
2
  module Model
3
3
  class Playlist < YouTubeG::Record
4
+ # *String*:: User entered description for the playlist.
4
5
  attr_reader :description
5
6
  end
6
7
  end
@@ -1,9 +1,16 @@
1
1
  class YouTubeG
2
2
  module Model
3
3
  class Rating < YouTubeG::Record
4
+ # *Float*:: Average rating given to the video
4
5
  attr_reader :average
6
+
7
+ # *Fixnum*:: Maximum rating that can be assigned to the video
5
8
  attr_reader :max
9
+
10
+ # *Fixnum*:: Minimum rating that can be assigned to the video
6
11
  attr_reader :min
12
+
13
+ # *Fixnum*:: Indicates how many people have rated the video
7
14
  attr_reader :rater_count
8
15
  end
9
16
  end
@@ -1,9 +1,16 @@
1
1
  class YouTubeG
2
2
  module Model
3
3
  class Thumbnail < YouTubeG::Record
4
+ # *String*:: URL for the thumbnail image.
4
5
  attr_reader :url
6
+
7
+ # *Fixnum*:: Height of the thumbnail image.
5
8
  attr_reader :height
9
+
10
+ # *Fixnum*:: Width of the thumbnail image.
6
11
  attr_reader :width
12
+
13
+ # *String*:: Specifies the time offset at which the frame shown in the thumbnail image appears in the video.
7
14
  attr_reader :time
8
15
  end
9
16
  end
@@ -0,0 +1,13 @@
1
+ class YouTubeG
2
+ module Model
3
+ class UploadError < YouTubeG::Record
4
+ attr_reader :domain
5
+ attr_reader :code
6
+ attr_reader :location
7
+
8
+ def to_s
9
+ "location: [#{location}] domain: [#{domain}] code: [#{code}]"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,38 +1,57 @@
1
+ # TODO
2
+ # * self atom feed
3
+ # * alternate youtube watch url
4
+ # * comments feedLink
5
+
1
6
  class YouTubeG
2
7
  module Model
3
8
  class Video < YouTubeG::Record
4
9
  # Describes the various file formats in which a Youtube video may be
5
10
  # made available and allows looking them up by format code number.
6
- #
7
11
  class Format
8
12
  @@formats = Hash.new
9
13
 
14
+ # Instantiates a new video format object.
15
+ #
16
+ # == Parameters
17
+ # :format_code<Fixnum>:: The Youtube Format code of the object.
18
+ # :name<Symbol>:: The name of the format
19
+ #
20
+ # == Returns
21
+ # YouTubeG::Model::Video::Format: Video format object
10
22
  def initialize(format_code, name)
11
23
  @format_code = format_code
12
24
  @name = name
13
25
 
14
26
  @@formats[format_code] = self
15
27
  end
16
-
28
+
29
+ # Allows you to get the video format for a specific format code.
30
+ #
31
+ # A full list of format codes is available at:
32
+ #
33
+ # http://code.google.com/apis/youtube/reference.html#youtube_data_api_tag_media:content
34
+ #
35
+ # == Parameters
36
+ # :format_code<Fixnum>:: The Youtube Format code of the object.
37
+ #
38
+ # == Returns
39
+ # YouTubeG::Model::Video::Format: Video format object
17
40
  def self.by_code(format_code)
18
41
  @@formats[format_code]
19
42
  end
20
43
 
21
- # Flash format on YouTube site. All videos are available in this
22
- # format.
23
- #
44
+ # Flash format on YouTube site. All videos are available in this format.
24
45
  FLASH = YouTubeG::Model::Video::Format.new(0, :flash)
25
46
 
26
- # RTSP streaming URL for mobile video playback. H.263 video (176x144)
27
- # and AMR audio.
28
- #
47
+ # RTSP streaming URL for mobile video playback. H.263 video (176x144) and AMR audio.
29
48
  RTSP = YouTubeG::Model::Video::Format.new(1, :rtsp)
30
49
 
31
50
  # HTTP URL to the embeddable player (SWF) for this video. This format
32
51
  # is not available for a video that is not embeddable.
33
- #
34
52
  SWF = YouTubeG::Model::Video::Format.new(5, :swf)
35
53
 
54
+ # RTSP streaming URL for mobile video playback. MPEG-4 SP video (up to 176x144) and AAC audio.
36
55
  THREE_GPP = YouTubeG::Model::Video::Format.new(6, :three_gpp)
37
56
  end
38
57
 
@@ -43,58 +62,114 @@ class YouTubeG
43
62
  attr_reader :state
44
63
  end
45
64
 
65
+ # *Fixnum*:: Duration of a video in seconds.
46
66
  attr_reader :duration
67
+
68
+ # *Boolean*:: Specifies that a video may or may not be embedded on other websites.
47
69
  attr_reader :noembed
70
+
71
+ # *Fixnum*:: Specifies the order in which the video appears in a playlist.
48
72
  attr_reader :position
73
+
74
+ # *Boolean*:: Specifies that a video is flagged as adult or not.
49
75
  attr_reader :racy
50
- attr_reader :statistics
51
76
 
77
+ # YouTubeG::Model::Video::AppControl The state of this video if post upload
52
78
  attr_reader :app_control
53
-
79
+
80
+ # *String*: Specifies a URI that uniquely and permanently identifies the video.
54
81
  attr_reader :video_id
82
+
83
+ # *Time*:: When the video was published on Youtube.
55
84
  attr_reader :published_at
85
+
86
+ # *Time*:: When the video's data was last updated.
56
87
  attr_reader :updated_at
88
+
89
+ # *Array*:: A array of YouTubeG::Model::Category objects that describe the videos categories.
57
90
  attr_reader :categories
91
+
92
+ # *Array*:: An array of words associated with the video.
58
93
  attr_reader :keywords
94
+
95
+ # *String*:: Description of the video.
59
96
  attr_reader :description
97
+
98
+ # *String*:: Title for the video.
60
99
  attr_reader :title
100
+
101
+ # *String*:: Description of the video.
61
102
  attr_reader :html_content
103
+
104
+ # YouTubeG::Model::Author:: Information about the YouTube user who owns a piece of video content.
62
105
  attr_reader :author
63
-
64
- # YouTubeG::Model::Content records describing the individual media content
65
- # data available for this video. Most, but not all, videos offer this.
106
+
107
+ # *Array*:: An array of YouTubeG::Model::Content objects describing the individual media content data available for this video. Most, but not all, videos offer this.
66
108
  attr_reader :media_content
67
-
68
- attr_reader :thumbnails # YouTubeG::Model::Thumbnail records
109
+
110
+ # *Array*:: An array of YouTubeG::Model::Thumbnail objects that contain information regarding the videos thumbnail images.
111
+ attr_reader :thumbnails
112
+
113
+ # *String*:: The link to watch the URL on YouTubes website.
69
114
  attr_reader :player_url
115
+
116
+ # YouTubeG::Model::Rating:: Information about the videos rating.
70
117
  attr_reader :rating
118
+
119
+ # *Fixnum*:: Number of times that the video has been viewed
71
120
  attr_reader :view_count
72
-
73
- # TODO:
74
- # self atom feed
75
- # alternate youtube watch url
76
- # responses feed
77
- # comments feedLink
78
-
121
+
122
+ attr_reader :statistics
123
+
124
+ # Videos related to the current video.
125
+ #
126
+ # === Returns
127
+ # YouTubeG::Response::VideoSearch
79
128
  def related
80
129
  YouTubeG::Parser::VideosFeedParser.new("http://gdata.youtube.com/feeds/api/videos/#{unique_id}/related").parse
81
130
  end
82
131
 
83
- # For convenience, the video_id with the URL stripped out, useful for searching for the video again
84
- # without having to store it anywhere. A regular query search, with this id will return the same video.
85
- # http://gdata.youtube.com/feeds/videos/ZTUVgYoeN_o
132
+ # Video responses to the current video.
133
+ #
134
+ # === Returns
135
+ # YouTubeG::Response::VideoSearch
136
+ def responses
137
+ YouTubeG::Parser::VideosFeedParser.new("http://gdata.youtube.com/feeds/api/videos/#{unique_id}/responses").parse
138
+ end
139
+
140
+ # The ID of the video, useful for searching for the video again without having to store it anywhere.
141
+ # A regular query search, with this id will return the same video.
142
+ #
143
+ # === Example
144
+ # >> video.unique_id
145
+ # => "ZTUVgYoeN_o"
146
+ #
147
+ # === Returns
148
+ # String: The Youtube video id.
86
149
  def unique_id
87
150
  video_id[/videos\/([^<]+)/, 1]
88
151
  end
89
152
 
90
- def can_embed?
153
+ # Allows you to check whether the video can be embedded on a webpage.
154
+ #
155
+ # === Returns
156
+ # Boolean: True if the video can be embedded, false if not.
157
+ def embeddable?
91
158
  not @noembed
92
159
  end
93
160
 
161
+ # Provides a URL and various other types of information about a video.
162
+ #
163
+ # === Returns
164
+ # YouTubeG::Model::Content: Data about the embeddable video.
94
165
  def default_media_content
95
166
  @media_content.find { |c| c.is_default? }
96
167
  end
97
168
 
169
+ # Gives you the HTML to embed the video on your website.
170
+ #
171
+ # === Returns
172
+ # String: The HTML for embedding the video on your website.
98
173
  def embed_html(width = 425, height = 350)
99
174
  <<EDOC
100
175
  <object width="#{width}" height="#{height}">
@@ -106,6 +181,10 @@ class YouTubeG
106
181
  EDOC
107
182
  end
108
183
 
184
+ # The URL needed for embedding the video in a page.
185
+ #
186
+ # === Returns
187
+ # String: Absolute URL for embedding video
109
188
  def embed_url
110
189
  @player_url.sub('watch?', '').sub('=', '/')
111
190
  end