Permalink
Browse files

added conditions when fetching videos so the dm-gvideo-adapter can wo…

…rk properly
  • Loading branch information...
1 parent 616c376 commit be35e3debc0ee35823d7ed8c6c8e6324a016ba88 @mattetti committed Sep 28, 2008
Showing with 65 additions and 12 deletions.
  1. +11 −2 README
  2. +27 −10 lib/gvideo.rb
  3. +27 −0 spec/gvideo_spec.rb
View
13 README
@@ -8,12 +8,21 @@ $ sudo gem install mattetti-gvideo
Usage:
- require 'rubygems'
+ begin
+ require 'minigems'
+ rescue LoadError
+ require 'rubygems'
+ end
require 'gvideo'
user = Gvideo::User.new("A005148908335059515423")
videos = user.fetch_videos
videos.first.embed_player
+or use conditions
+
+ user.fetch_videos(:title => "Durex: The Garden").first
+ videos = user.fetch_videos(:title => /Durex/)
+
Video methods and attributes:
---
@@ -29,4 +38,4 @@ Video methods and attributes:
Check lib/gvideo.rb for more
You can find a user's id by using firebug and inspect the call made to "see more videos from a user"
-I don't know a better way at the moment
+You can also click on one of the videos available from the "see more videos from a user" and click on one of the videos. The url will contain the user id.
View
37 lib/gvideo.rb
@@ -5,8 +5,8 @@
# Retrieve the videos associated to a user (limited to 100 videos)
#
# Usage example
-# 005148908335059515423
-# user = Gvideo::User.new("005148908335059515423")
+# A005148908335059515423
+# user = Gvideo::User.new("A005148908335059515423")
# videos = user.fetch_videos
#
# You can find a user's id by using firebug and inspect the call made to "see more videos from a user"
@@ -69,15 +69,22 @@ def initialize (google_user_id)
@video_count = nil
end
+
# returns an array of videos
- def fetch_videos
+ #
+ # pass a hash of conditions
+ # available conditions can only target
+ # the title or the duration of a video
+ #
+ # ---
+ # @api public
+ def fetch_videos(conditions = {})
videos = []
-
# if video_count > 100 get all the videos using a higher cursor
0.upto((video_count/100.to_f).ceil - 1) do |cursor|
p "fetching videos with cursor: #{cursor}" if $Gvideo_verbose
cursor = (cursor * 100) if cursor > 0
- videos << extract_video_elements_from_raw_data(video_raw_data(cursor), cursor)
+ videos << extract_video_elements_from_raw_data(video_raw_data(cursor), cursor, conditions)
end
videos.flatten
@@ -102,14 +109,24 @@ def user_video_url(cursor=0)
#
# creates a video instance using the scrapped data
#
- def extract_video(element)
+ # ---
+ # @api private
+ def extract_video(element, conditions)
docid = element['docid']
- duration = element['dur']
+ duration = element['dur'].to_i
title = element.at("div.vli-metadata span.vlim-title a")['title']
thumbnail_url = element.at("span.vli-thumbnail a img")['src']
video_url = element['url']
duration_in_minutes = element.at("div.vli-metadata span.vlim-duration").inner_text
+ if conditions.has_key?(:title)
+ return if conditions[:title].is_a?(String) && conditions[:title] != title
+ return if conditions[:title].is_a?(Regexp) && title.match(conditions[:title]).nil?
+ end
+ if conditions.has_key?(:duration)
+ return if conditions[:duration].is_a?(Integer) && conditions[:duration] != duration
+ return if conditions[:duration].is_a?(Range) && !conditions[:duration].include?(duration)
+ end
Video.new( { :docid => docid,
:duration => duration,
:title => title,
@@ -122,12 +139,12 @@ def extract_video(element)
#
# extract video elements from a raw video data and returns an array of Gvideo::Video objects
#
- def extract_video_elements_from_raw_data(video_raw_data, cursor=0)
+ def extract_video_elements_from_raw_data(video_raw_data, cursor=0, conditions={})
videos = []
video_raw_data(cursor).search("div.video-list-item").each do |element|
- videos << extract_video(element)
+ videos << extract_video(element, conditions)
end
- videos
+ videos.compact
end
#
View
27 spec/gvideo_spec.rb
@@ -34,6 +34,33 @@
videos.map{|v| v.docid}.uniq.size.should == videos.size
end
+ it "should be able to retrieve videos with a simple text condition" do
+ video = @user.fetch_videos(:title => "Durex: The Garden")
+ video.should_not be_nil
+ video.length.should == 1
+ end
+
+ it "should be able to retrieve videos with a regexp title condition" do
+ video = @user.fetch_videos(:title => /Durex: The Garden/)
+ video.should_not be_nil
+ video.length.should == 1
+ end
+
+ it "should be able to retrieve videos with a strict duration condition" do
+ video = @user.fetch_videos(:duration => 12)
+ video.should_not be_nil
+ # might fail is more videos get added by the user
+ video.length.should == 2
+ end
+
+ it "should be able to retrieve videos with a duration range condition" do
+ videos = @user.fetch_videos(:duration => 400..450)
+ videos.should_not be_nil
+ # might fail is more videos get added by the user
+ videos.length.should == 1
+ # videos.length.should < @user.fetch_videos.length
+ end
+
end
describe "Gvideo::Video" do

0 comments on commit be35e3d

Please sign in to comment.