Permalink
Browse files

initial import

  • Loading branch information...
0 parents commit ca364518fc3c52afe8dbe5483409061161112517 @mattetti committed Sep 19, 2008
Showing with 391 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +4 −0 README
  3. +47 −0 Rakefile
  4. +4 −0 TODO
  5. +171 −0 lib/gvideo.rb
  6. +14 −0 script/destroy
  7. +14 −0 script/generate
  8. +114 −0 spec/gvideo_spec.rb
  9. +3 −0 spec/spec_helper.rb
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 YOUR NAME
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 README
@@ -0,0 +1,4 @@
+gvideo
+======
+
+A gem that provides...
@@ -0,0 +1,47 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+require 'rubygems/specification'
+require 'date'
+
+GEM = "gvideo"
+GEM_VERSION = "0.0.1"
+AUTHOR = "Your Name"
+EMAIL = "Your Email"
+HOMEPAGE = "http://example.com"
+SUMMARY = "A gem that provides..."
+
+spec = Gem::Specification.new do |s|
+ s.name = GEM
+ s.version = GEM_VERSION
+ s.platform = Gem::Platform::RUBY
+ s.has_rdoc = true
+ s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
+ s.summary = SUMMARY
+ s.description = s.summary
+ s.author = AUTHOR
+ s.email = EMAIL
+ s.homepage = HOMEPAGE
+
+ # Uncomment this to add a dependency
+ # s.add_dependency "foo"
+
+ s.require_path = 'lib'
+ s.autorequire = GEM
+ s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
+desc "install the gem locally"
+task :install => [:package] do
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
+end
+
+desc "create a gemspec file"
+task :make_spec do
+ File.open("#{GEM}.gemspec", "w") do |file|
+ file.puts spec.to_ruby
+ end
+end
4 TODO
@@ -0,0 +1,4 @@
+TODO:
+Fix LICENSE with your name
+Fix Rakefile with your name and contact info
+Add your code to lib/<%= name %>.rb
@@ -0,0 +1,171 @@
+require 'rubygems'
+require 'hpricot'
+require 'open-uri'
+
+# Retrieve the videos associated to a user (limited to 100 videos)
+#
+# The user id can be found by clicking on
+# Usage example
+# 005148908335059515423
+# user = Gvideo::User.new("005148908335059515423")
+# 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
+#
+# $Gvideo_verbose = true # set this constant to true in your app to see some debugging output
+
+
+
+module Gvideo
+
+ class GvideoException < RuntimeError
+ def initialize (message)
+ super(message)
+ end
+ end
+
+ # A generic record that initializes instance variables from the supplied hash
+ # mapping symbol names to their respective values.
+ # borrowed from http://www.rubyforge.org/projects/google-video/
+ class Record
+ def initialize (params)
+ if params != nil
+ params.each do |key, value|
+ name = key.to_s
+ instance_variable_set("@#{name}", value) if respond_to?(name)
+ end
+ end
+ end
+ end
+
+ # Video object represent a google video object with various attributes
+ # such as its docid, title, video url (to playback the video), duration, thumbnail_url
+ #
+ class Video < Record
+
+ attr_reader :docid
+ attr_reader :title
+ attr_reader :video_url
+ attr_reader :duration
+ attr_reader :duration_in_minutes
+ attr_reader :thumbnail_url
+
+
+ def embed_player(width=400, height=326)
+ "<embed id='VideoPlayback-#{docid}' src='http://video.google.com/googleplayer.swf?docid=#{docid}&hl=en&fs=true' style='width:#{width}px;height:#{height}px' allowFullScreen='true' allowScriptAccess='always' type='application/x-shockwave-flash'></embed>"
+ end
+
+ end
+
+ class User
+
+ # Google user id
+ # Example: "005148908335059515423"
+ attr_reader :g_id
+
+ def initialize (google_user_id)
+ @g_id = google_user_id
+ @user_video_url = "http://video.google.com/videomorefrom?q=source%3#{@g_id}&filter=0&hl=en&num=100&start=0"
+ @video_raw_data = {}
+ @video_count = nil
+ end
+
+ # returns an array of videos
+ def fetch_videos
+ 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)
+ end
+
+ videos.flatten
+ end
+
+ # returns the amount of videos uploaded by the user
+ def video_count
+ if @video_count.nil?
+ count = video_raw_data.at("div#morefromuser div").inner_text
+ @video_count = count.nil? ? nil : count.to_i
+ else
+ @video_count
+ end
+ end
+
+ private
+
+ def user_video_url(cursor=0)
+ "http://video.google.com/videomorefrom?q=source%3#{@g_id}&filter=0&hl=en&num=100&start=#{cursor}"
+ end
+
+ #
+ # creates a video instance using the scrapped data
+ #
+ def extract_video(element)
+ docid = element['docid']
+ duration = element['dur']
+ 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
+
+ Video.new( { :docid => docid,
+ :duration => duration,
+ :title => title,
+ :thumbnail_url => thumbnail_url,
+ :video_url => video_url,
+ :duration_in_minutes => duration_in_minutes } )
+ end
+
+
+ #
+ # 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)
+ videos = []
+ video_raw_data(cursor).search("div.video-list-item").each do |element|
+ videos << extract_video(element)
+ end
+ videos
+ end
+
+ #
+ # retrieves video data if cached otherwise fetches it
+ #
+ def video_raw_data(cursor=0)
+ @video_raw_data[cursor] ||= fetch_video_raw_data(cursor)
+ p "at cursor: #{cursor} first docid: #{@video_raw_data[cursor].search("div.video-list-item").first['docid']}" if $Gvideo_verbose
+ @video_raw_data[cursor]
+ end
+
+ #
+ # open and parse the user video url (raw data gets reset)
+ # pass a cursor to start fetching data after a certain point
+ #
+ def fetch_video_raw_data(cursor=0)
+ p "fetching raw video data for cursor #{cursor}" if $Gvideo_verbose
+
+ # reset video count
+ @video_count = nil unless cursor > 0
+
+ # open the user video url
+ begin
+ url = open(user_video_url(cursor))
+ rescue
+ raise GvideoException.new("failed to access user's video")
+ end
+ # parse the url and return the hpricot doc
+ begin
+ doc = Hpricot(url)
+ rescue
+ raise GvideoException.new("failed to parse raw Google video data")
+ else
+ @video_raw_data[cursor] = doc
+ end
+ end #of video_raw_data
+
+ end #of user
+
+end
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit]
+RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:newgem_simple, :test_unit]
+RubiGen::Scripts::Generate.new.run(ARGV)
Oops, something went wrong.

0 comments on commit ca36451

Please sign in to comment.