Created
January 25, 2020 14:05
-
-
Save rkachowski/344d5567165fccb49209147e8900b88f to your computer and use it in GitHub Desktop.
download all ya wistia videos concurrently
This file contains hidden or 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
require 'faraday' | |
@max_threads = 8 | |
@download_mutex = Mutex.new | |
ROOT_URL = "https://embedwistia-a.akamaihd.net" | |
def download urls, folder | |
threads = Array.new(@max_threads) | |
threads.map! { | |
Thread.new(urls) { |urls| | |
loop do | |
@download_mutex.synchronize { | |
Thread.current[:url] = urls.pop | |
} | |
url = Thread.current[:url] | |
break if url == nil | |
puts "downloading - #{url}" | |
output_path = File.join(folder, File.basename(url)) | |
next if File.exist? output_path | |
resp = Faraday.new(url: url).get | |
@download_mutex.synchronize { | |
unless Dir.exist? folder | |
Dir.mkdir folder | |
end | |
} | |
File.open(output_path,"w") { |f| f << resp.body } | |
puts "#{url} - done" | |
break if urls.empty? | |
end | |
} | |
} | |
threads.each(&:join) | |
end | |
def get_urls_in_cwd | |
playlists = Dir["*.m3u"] | |
map = playlists.inject({}) do |h,f| | |
paths = File.open(f).read.lines.select { |p| p =~ /\.ts$/ } | |
h[f] = paths.map(&:chomp) | |
h | |
end | |
map.values.map! { |urls| urls.map! {|p| ROOT_URL + p }} | |
map | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment