Class: WWDCDownloader
- Inherits:
-
Object
- Object
- WWDCDownloader
- Defined in:
- lib/wwdcdownloader.rb
Constant Summary collapse
- WWDC_LIBRARIES =
[{:base => 'https://developer.apple.com/library/prerelease/ios', :lib => '/navigation/library.json'}, {:base => 'https://developer.apple.com/library/prerelease/mac', :lib => '/navigation/library.json'}]
Instance Attribute Summary collapse
-
#dl_dir ⇒ Object
Returns the value of attribute dl_dir.
-
#downloaded_files ⇒ Object
Returns the value of attribute downloaded_files.
-
#mech ⇒ Object
Returns the value of attribute mech.
-
#min_date ⇒ Object
Returns the value of attribute min_date.
Class Method Summary collapse
Instance Method Summary collapse
- #download_file(url, filename, dest_dir, duplicates_ok = true) ⇒ Object
- #download_sample_code_for_page(a_page_url, dest_dir, duplicates_ok = true) ⇒ Object
- #download_sample_code_from_book_json(book_json_url, code_base_url, dest_dir, duplicates_ok) ⇒ Object
-
#initialize(dl_dir, min_date) ⇒ WWDCDownloader
constructor
A new instance of WWDCDownloader.
- #load ⇒ Object
-
#login ⇒ Object
Login.
-
#mkdir(dir) ⇒ Object
Creates the given directory if it doesn’t exist already.
Constructor Details
#initialize(dl_dir, min_date) ⇒ WWDCDownloader
Returns a new instance of WWDCDownloader.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/wwdcdownloader.rb', line 34 def initialize(dl_dir, min_date) self.dl_dir = dl_dir self.min_date = min_date self.mech = Mechanize.new self.mech.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE self.mech.agent. = true self.downloaded_files = [] if ENV['http_proxy'] || ENV['HTTP_PROXY'] uri = (ENV['http_proxy']) ? ENV['http_proxy'] : ENV['HTTP_PROXY'] parsedUrl = URI.parse(uri) self.mech.set_proxy parsedUrl.host, parsedUrl.port end end |
Instance Attribute Details
#dl_dir ⇒ Object
Returns the value of attribute dl_dir.
32 33 34 |
# File 'lib/wwdcdownloader.rb', line 32 def dl_dir @dl_dir end |
#downloaded_files ⇒ Object
Returns the value of attribute downloaded_files.
32 33 34 |
# File 'lib/wwdcdownloader.rb', line 32 def downloaded_files @downloaded_files end |
#mech ⇒ Object
Returns the value of attribute mech.
32 33 34 |
# File 'lib/wwdcdownloader.rb', line 32 def mech @mech end |
#min_date ⇒ Object
Returns the value of attribute min_date.
32 33 34 |
# File 'lib/wwdcdownloader.rb', line 32 def min_date @min_date end |
Class Method Details
.run!(*args) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/wwdcdownloader.rb', line 253 def self.run!(*args) puts "WWDC 2014 Session Material Downloader" puts "by Johannes Fahrenkrug, @jfahrenkrug, springenwerk.com" puts "See you next year!" puts if args.size < 1 puts "Usage: wwdcdownloader <your Apple ID> [<target-dir>]" exit end dl_dir = if args.size > 1 args.last else 'wwdc2014-assets' end w = WWDCDownloader.new(dl_dir, '2014-05-28') w.login w.load return 0 end |
Instance Method Details
#download_file(url, filename, dest_dir, duplicates_ok = true) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/wwdcdownloader.rb', line 113 def download_file(url, filename, dest_dir, duplicates_ok = true) did_download = false if duplicates_ok or !self.downloaded_files.include?(url) # remember what we downloaded self.downloaded_files << url puts " Downloading #{url}" begin self.mech.get(url) do |downloaded_file| open(dest_dir + "/" + filename, 'wb') do |file| file.write(downloaded_file.body) end did_download = true end rescue Exception => e puts " Download failed #{e}" end elsif !duplicates_ok puts " Already downloaded this file, skipping." end did_download end |
#download_sample_code_for_page(a_page_url, dest_dir, duplicates_ok = true) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/wwdcdownloader.rb', line 137 def download_sample_code_for_page(a_page_url, dest_dir, duplicates_ok = true) self.mech.get(a_page_url) do |page| has_samplecode = false page.links_with(:href => /\.zip/ ).each do |link| has_samplecode = true #code_base_url = File.dirname(link.href) #download_sample_code_from_book_json("#{code_base_url}/book.json", code_base_url, dest_dir, duplicates_ok) if link.href =~ /(\w+\.zip)$/ download_file(link.href, $1, dest_dir, duplicates_ok) else has_samplecode = false end end if !has_samplecode puts " Sorry, this session doesn't have samplecode, cleaning up." begin Dir.delete(dest_dir) rescue end end end end |
#download_sample_code_from_book_json(book_json_url, code_base_url, dest_dir, duplicates_ok) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/wwdcdownloader.rb', line 96 def download_sample_code_from_book_json(book_json_url, code_base_url, dest_dir, duplicates_ok) did_download = false self.mech.get(book_json_url) 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}" did_download = download_file(url, filename, dest_dir, duplicates_ok) end end did_download end |
#load ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/wwdcdownloader.rb', line 162 def load mkdir(dl_dir) # get the sessions JSON #self.mech.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 # download_sample_code_for_page(session['url'], dirname) # 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 # scrape the WWDC libraries... puts puts "Scraping the WWDC libraries (not all sample code might be linked up correctly yet)" WWDC_LIBRARIES.each do |lib_hash| lib = "#{lib_hash[:base]}#{lib_hash[:lib]}" self.mech.get(lib) do |page| body = page.body.gsub("''", '""') res = JSON.parse(body) docs = res['documents'] if docs.size > 0 docs.each do |doc| if doc[2] == 5 and doc[3] >= self.min_date # sample code and newer or equal to min date title = doc[0] puts "Sample Code '#{title}'..." # get the files dirname = "#{dl_dir}/#{title.gsub(/\/|&|!|:/, '')}" puts " Creating #{dirname}" did_create_dir = mkdir(dirname) segments = doc[9].split('/') url = "#{lib_hash[:base]}/samplecode/#{segments[2]}/book.json" begin puts url did_download = download_sample_code_from_book_json(url, "#{lib_hash[:base]}/samplecode/#{segments[2]}", dirname, false) if !did_download and did_create_dir Dir.delete( dirname ) end rescue Mechanize::ResponseCodeError => e STDERR.puts " Error retrieving list for sample code. Proceeding with next one (#{$!})" next end end end else print "No code samples :(.\n" end end end puts "Done." end |
#login ⇒ Object
Login
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 |
# File 'lib/wwdcdownloader.rb', line 60 def login wrong_password = true while wrong_password do password = ask("Enter your ADC password: ") { |q| q.echo = "*" } self.mech.get('https://developer.apple.com/membercenter/') do |page| my_page = page.form_with(:name => 'form2') do |f| f.appleId = ARGV[0] f.accountPassword = password end. if my_page.body =~ /incorrect/ puts "Wrong password, please try again." else wrong_password = false # do we still need to select a team? if my_page.body =~ /saveTeamSelection/ team_form = my_page.form_with(:name => 'saveTeamSelection') # select first team team_select = team_form.field_with(:id => 'teams') team_option = team_select.[0] team_select.value = team_option puts "Selecting Team #{team_option.text}" = team_form.(:value => "Continue") team_select_result_page = team_form.() end end end end end |
#mkdir(dir) ⇒ Object
Creates the given directory if it doesn’t exist already.
50 51 52 53 54 55 56 57 |
# File 'lib/wwdcdownloader.rb', line 50 def mkdir(dir) if File.exists?(dir) false else Dir.mkdir dir true end end |