forked from jfahrenkrug/WWDC-Downloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwwdc11downloader.rb
153 lines (123 loc) · 3.75 KB
/
wwdc11downloader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Have fun. Use at your own risk.
require 'rubygems'
require 'fileutils'
require 'net/http'
begin
require 'mechanize'
require 'json'
require 'highline/import'
rescue LoadError => e
puts
puts "You need to have the mechanize, json and highline gems installed."
puts "Install them by running"
puts
puts " gem install mechanize json highline"
puts
puts "or"
puts
puts " sudo gem install mechanize json highline"
puts
exit
end
puts "WWDC 2011 Session Material Downloader"
puts "by Johannes Fahrenkrug, @jfahrenkrug, springenwerk.com"
puts "See you next year!"
puts
if ARGV.size < 1
puts "Usage: ruby wwdc2011downloader.rb <your Apple ID> [<target-dir>]"
exit
end
BASE_URI = 'https://developer.apple.com/wwdc/scripts/services.php?type=get_session_data'
dl_dir = if ARGV.size > 1
ARGV.last
else
'wwdc2011-assets'
end
# Creates the given directory if it doesn't exist already.
def mkdir(dir)
Dir.mkdir dir unless File.exists?(dir)
end
a = Mechanize.new
# Login
wrong_password = true
while wrong_password do
password = ask("Enter your ADC password: ") { |q| q.echo = "*" }
a.get(BASE_URI) do |page|
my_page = page.form_with(:name => 'appleConnectForm') do |f|
f.theAccountName = ARGV[0]
f.theAccountPW = password
end.click_button
if my_page.body =~ /get_session_data/
wrong_password = false
else
puts "Wrong password, please try again."
end
end
end
# create dir
mkdir(dl_dir)
# get the sessions JSON
a.get(BASE_URI) do |page|
res = JSON.parse(page.body)
# Was there an error?
error = res['response']['error']
if (error)
STDERR.puts " Apple's API returned an error: '#{error}'"
exit
end
sessions = res['response']['sessions']
if sessions.size > 0
sessions.each do |session|
if session['type'] == 'Session'
title = session['title']
session_id = session['id']
puts "Session #{session_id} '#{title}'..."
# get the files
dirname = "#{dl_dir}/#{session_id}-#{title.gsub(/\/|&|!/, '')}"
puts " Creating #{dirname}"
mkdir(dirname)
begin
a.get(session['url']) do |page|
has_samplecode = false
page.links_with(:href => %r{/samplecode/} ).each do |link|
has_samplecode = true
code_base_url = File.dirname(link.href)
a.get("#{code_base_url}/book.json") do |book_json|
if book_json.body[0,1] == '<'
puts " Sorry, this samplecode apparently isn't available yet: #{code_base_url}/book.json"
else
book_res = JSON.parse(book_json.body)
filename = book_res["sampleCode"]
url = "#{code_base_url}/#{filename}"
puts " Downloading #{url}"
begin
a.get(url) do |downloaded_file|
open(dirname + "/" + filename, 'wb') do |file|
file.write(downloaded_file.body)
end
end
rescue Exception => e
puts " Download failed #{e}"
end
end
end
end
if !has_samplecode
puts " Sorry, this session doesn't have samplecode, cleaning up."
begin
Dir.delete( dirname )
rescue
end
end
end
rescue Mechanize::ResponseCodeError => e
STDERR.puts " Error retrieving list for session. Proceeding with next session (#{$!})"
next
end
end
end
else
print "No sessions :(.\n"
end
end
puts "Done."