#!/usr/bin/env ruby # # Copyright(C) Simon Howard # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # require 'uri' require 'open-uri' require 'cgi' class Video attr_reader :key attr_reader :title def initialize(key, title) @key = key @title = title end def flv_url data = nil url="http://youtube.com/watch?v=#{@key}&v2" open(url) do |f| data = f.readlines.join end if data !~ /\'\/watch_fullscreen\?(.*?)\'/m raise "Unable to get player URL for #{@key}" end keys = CGI.parse($1) "http://youtube.com/get_video?video_id=#{@key}&t=#{keys['t']}" end def filename @title.gsub(/[^\w]/, "_") + ".flv" end end if ARGV.length >= 1 && ARGV[0] =~ /v\=([A-Za-z0-9\-_]*)/ video = Video.new($1, $1) `wget '#{video.flv_url}' -O '#{video.filename}'` else puts "Usage: #{$0}