Created
December 21, 2012 01:08
-
-
Save lfzawacki/4350000 to your computer and use it in GitHub Desktop.
Ever needed to get all thumbnails for a youtube video?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'open-uri' | |
def classy_exit | |
puts "I need a youtube video URL" | |
exit 0 | |
end | |
def get_video_id url | |
# papa loves you | |
reg = /((?:https?\:\/\/)?(?:www\.)?youtube(?:-nocookie)?.com(?:\/(?:v|embed)\/([a-zA-Z0-9_-]+))|(?:watch\?.*v=([a-zA-Z0-9_-]+))).*/ | |
data = reg.match url | |
data[2] || data[3] if data | |
end | |
classy_exit if ARGV[0].nil? | |
id = get_video_id(ARGV[0]) | |
classy_exit if id.nil? | |
Dir.mkdir(id) | |
(0..5).each do |i| | |
filename = "#{i}.jpg" | |
begin | |
content = open("http://img.youtube.com/vi/#{id}/#{filename}").read | |
File.open(File.join(id, filename), "wb") { |f| f << content } | |
puts "Created #{filename}" | |
rescue OpenURI::HTTPError | |
# some videos wont have all thumbs | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment