MozReplを使ってYouTube遊び

MozReplって何,って人は,とりあえず落とそう.
http://dev.hyperstruct.net/mozlab


とりあえず考えたのは,「今表示してるYouTubeのページの動画のダウンロード」.

require 'net/telnet'
require 'net/http'
require 'uri'
require 'open-uri'

prompt = /^repl>/
begin
  repl = Net::Telnet.new("Host" => "localhost",
                         "Port" => 4242,
                         "Prompt" => prompt)
rescue
  puts "replが起動してないかも"
  exit
end

repl.waitfor(prompt)

exit unless repl.cmd("content.location.href") =~ /youtube.com/

# タイトルの取得
title = repl.cmd("title").split(/\n/).shift.sub(/YouTube - /, '').gsub(/\s/,'')

# video_idとその他get_videoに必要なパラメータの取得
uri = URI.parse("http://www.youtube.com/get_video")
regex = /video_id=[^&]+&l=\d+&t=[^\"]+/
repl.cmd("repl.enter(content)")
uri.query = repl.cmd("document.getElementById('interactDiv').textContent")[regex]

# ダウンロード
File.open("#{title}.flv", 'wb') do |f|
  puts "now getting #{title}..."
  f.write(open(Net::HTTP.get_response(uri)['location']).read)
end
puts "complete"

とりあえず動きます.なんか格好悪いので,そのうち格好良くする.