Skip to content

Instantly share code, notes, and snippets.

@unycone
Created March 15, 2013 11:35
Show Gist options
  • Save unycone/5169273 to your computer and use it in GitHub Desktop.
Save unycone/5169273 to your computer and use it in GitHub Desktop.
To retrieve raw video ulrs from youtube.
require 'uri'
require 'net/http'
require 'cgi'
class ScrapeYoutube
@@youtube_url = 'http://www.youtube.com'
@@youtube_info_path = '/get_video_info?video_id='
@@key = "url_encoded_fmt_stream_map"
def self.showWebmUrl(video_id)
map = getVideoInfo(video_id)
map[@@key].each do |m|
if (m["type"].start_with?("video/webm")) then
puts m["url"]
end
end
end
def self.showInfo(video_id)
printBody(getVideoInfo(video_id))
end
def self.getVideoInfo(video_id)
url = URI.parse(@@youtube_url+@@youtube_info_path+video_id)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
map = toMap(res.body)
map[@@key] = parseFmt(map[@@key])
return map
end
def self.printBody(map, indent="")
map.each_pair do |k, v|
if (v.kind_of?(Hash)) then
puts indent + k + " =>"
printBody(v, " "+indent)
elsif (v.kind_of?(Array)) then
puts indent + k + " =>"
v.each do |e|
printBody(e, " "+indent)
puts("")
end
else
puts indent + k + " => " + v
end
end
end
def self.toMap(str)
map = Hash::new
str.split('&').each do |e|
ary = e.split('=')
map[ary[0]] = URI.decode(ary[1] || "")
end
return map
end
def self.parseFmt(str)
dst = []
map = Hash::new
str.split(',').each do |e|
dst << toMap(e)
end
return dst
end
end
ScrapeYoutube::showWebmUrl("sPasebVMIW4")
ScrapeYoutube::showInfo("sPasebVMIW4")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment