Skip to content

Instantly share code, notes, and snippets.

@skojin
Created February 21, 2012 13:31
Show Gist options
  • Save skojin/1876590 to your computer and use it in GitHub Desktop.
Save skojin/1876590 to your computer and use it in GitHub Desktop.
download video from youtube subscription
# download video from youtube subscription
# depends: youtube-dl
# OS: windows
require 'open-uri'
require 'hpricot'
USERNAME = ''
YOUTUBE_DL_PATH = "python C:\\programs\\Video\\youtube\\youtube-dl\\youtube-dl.py"
YOUTUBE_DL_PARAMS = "--retries=30 --max-quality=22 -c -i -w"
feed = Hpricot(open("http://gdata.youtube.com/feeds/base/users/#{USERNAME}/newsubscriptionvideos"))
already_loaded = []
already_loaded = IO.read('.youtube.dat').split("\n") if File.exist?('.youtube.dat')
feed.search('entry').reverse.each do |entry|
video_id = entry.at('id').inner_html
video_id =~ /([a-z0-9_-]+)$/i
video_id = $1
next if already_loaded.include?(video_id)
video_url = "http://www.youtube.com/watch?v=#{video_id}"
puts "download #{video_url}"
title_format = entry.at('title').inner_html.include?(':') ? 'stitle' : 'title'
command = %{#{YOUTUBE_DL_PATH} #{YOUTUBE_DL_PARAMS} --output="%(upload_date)s %(#{title_format})s-%(id)s.%(ext)s" #{video_url}}
system %(start /MIN #{command})
already_loaded.unshift(video_id)
end
already_loaded = already_loaded[0, 100] # keep last 100
open('.youtube.dat', 'w'){|f| f.write already_loaded.join("\n") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment