c3d2-wiki/Youtube-Scraping.mw

34 lines
995 B
Plaintext

==Suchen & Saugen==
<source lang="ruby">require 'ubygems'
require 'mechanize'
if ARGV.size < 1
puts "Usage: #{$0} [ keywords ... ]"
exit -1
end
agent = WWW::Mechanize.new
homepage = agent.get('http://www.youtube.com/')
searchform = homepage.forms.select { |f| f.action == '/results' }.first
searchform['search_query'] = ARGV.join(' ')
result = searchform.submit
result.root.traverse_element('td') do |td|
if td.classes == ['vinfo']
a = td.find_element('a') || raise
puts a.children.to_s
watch = agent.click(a)
if watch.root.to_s =~ /\?video_id=(.+?)\&l=(.+?)\&t=(.+?)\&/
video_id, l, t = $1, $2, $3
p [video_id, l, t]
video_id.gsub!(/[^a-zA-Z0-9_-]/, '')
video_id = a.get_attribute('href').split('?v=').last
system("wget -O #{video_id}.flv 'http://www.youtube.com/get_video?video_id=#{video_id}&t=#{t}'")
else
puts "Warning: cannot find video URL"
end
end
end</source>
[[Kategorie:Ruby]]
{{Rübÿ Spëëd Mëtäl Cödïng}}