Last active
January 21, 2024 13:36
-
-
Save mackuba/c0adfb7aa4321ad7d2a6254768636f65 to your computer and use it in GitHub Desktop.
Script for downloading all WWDC videos from a JSON feed
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/env ruby | |
# 06/2021: https://devimages-cdn.apple.com/wwdc-services/pb9e2d31/0CF104D6-F0B3-4AE8-B9F0-6F2D9F54A502/contents.json | |
# 11/2020: https://devimages-cdn.apple.com/wwdc-services/n233a99f/5D23F1E9-9551-4768-ACF3-E3920F9C572D/contents.json | |
# 5/2019: https://devimages-cdn.apple.com/wwdc-services/j06970e2/296E57DA-8CE8-4526-9A3E-F0D0E8BD6543/contents.json | |
# 8/2017: https://devimages-cdn.apple.com/wwdc-services/h8a19f8f/049CCC2F-0D8A-4F7D-BAB9-2D8F5BAA7030/contents.json | |
# 5/2016: http://devimages.apple.com.edgekey.net/wwdc-services/g7tk3guq/xhgbpyutb6wvn2xcrbcz/videos.json | |
require 'fileutils' | |
require 'json' | |
if ARGV.count != 2 | |
puts "Usage: #{$PROGRAM_NAME} <feed.json> <target_dir>" | |
exit 1 | |
end | |
feed_file, target_dir = ARGV | |
if !File.exist?(feed_file) | |
puts "File #{feed_file} doesn't exist" | |
exit 1 | |
end | |
if !File.exist?(target_dir) | |
FileUtils.mkdir_p(target_dir) | |
elsif !File.directory?(target_dir) | |
puts "#{target_dir} is not a directory" | |
exit 1 | |
end | |
def download(session, url, dir) | |
filename = url.split('/').last | |
id = session['id'].split('-').last.to_i | |
if filename.gsub(/[hs]d/i, '').gsub(/wwdc/i, '').gsub(/\.\w+$/, '') !~ /[a-z]/ | |
title = session['title'].downcase | |
.gsub(/\-/, '_') | |
.gsub(/@/, '_at_') | |
.gsub(/[^\w\s]+/, '') | |
.gsub(/\s+/, '_') | |
filename = filename.sub(/#{session['eventId']}[_\-]/, '') | |
.sub("#{id}_", "#{id}_#{title}_") | |
.sub("#{id}-", "#{id}-#{title}-") | |
.sub("#{id}.", "#{id}-#{title}.") | |
end | |
path = File.join(dir, filename) | |
return if File.exist?(path) | |
tries = 0 | |
max = 20 | |
while tries < max | |
begin | |
puts ">> (#{tries}) #{path}" | |
puts | |
system("curl -C - -o \"#{path}\" #{url}", exception: true) | |
break | |
rescue Exception => e | |
tries += 1 | |
end | |
end | |
if tries == max | |
File.delete(path) | |
exit 1 | |
end | |
end | |
feed = JSON.parse(File.read(feed_file)) | |
if feed.is_a?(Hash) && feed['contents'] | |
sessions = feed['contents'] | |
.select { |x| x['media'] && x['media']['downloadHD'] } | |
.sort_by { |x| w = x['id'].split('-'); [w[0...-1].join, -w.last.to_i] } | |
.reverse | |
sessions.each do |session| | |
event = session['eventId'].gsub(/wwdc/, '') | |
subdir = File.join(target_dir, event) | |
FileUtils.mkdir_p(subdir) | |
download(session, session['media']['downloadHD'], subdir) | |
download(session, session['media']['slides'], subdir) if session['media']['slides'] | |
end | |
elsif feed.is_a?(Hash) && feed['sessions'] | |
feed['sessions'].select { |x| x['date'] == 2012 }.each do |session| | |
subdir = File.join(target_dir, '2012') | |
FileUtils.mkdir_p(subdir) | |
title_slug = session['title'].downcase | |
.gsub(/([a-z])[\-\']([a-z])/, "\\1\\2").gsub(/: /, '__').gsub(/ \+ /, '__').gsub(/\W+/, '_') | |
filename = "session_#{session['id']}__#{title_slug}.mov" | |
path = File.join(subdir, filename) | |
unless File.exist?(path) | |
puts "https://download.developer.apple.com/videos/wwdc_2012__hd/#{filename}" | |
end | |
end | |
elsif feed.is_a?(Array) | |
feed.each do |session| | |
subdir = File.join(target_dir, session['year']) | |
FileUtils.mkdir_p(subdir) | |
['HD', 'PDF'].each do |type| | |
link = session['links'].detect { |l| l[0] == type } | |
next unless link | |
url = link[1] | |
filename = url.split('/').last | |
path = File.join(subdir, filename) | |
unless File.exist?(path) | |
puts "#{type.rjust(3)}: #{url}" | |
end | |
end | |
puts | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment