/
spider
executable file
·129 lines (111 loc) · 3.04 KB
/
spider
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
#!/usr/bin/env ruby
#
# Spider for downloading assets from CDNs. Requires Ruby 1.9.
#
if RUBY_VERSION.split('.')[1].to_i < 9
puts "Requires Ruby 1.9."; exit
end
def usage
script = File.basename(__FILE__)
puts "Usage:"
puts " #{script} [options] <cdn> <library> <version>"
puts ""
puts "Options:"
puts " -w, --overwrite overwrite existing files"
puts " -h, --help print this help"
puts ""
puts "Example:"
puts " #{script} google jquery 1.5.2"
end
require 'fileutils'
require 'net/http'
require 'uri'
require 'getoptlong'
#
# parse arguments
#
$overwrite = false
GetoptLong.new(
['--help' , '-h' , GetoptLong::NO_ARGUMENT],
['--overwrite' , '-w' , GetoptLong::NO_ARGUMENT]
).each do |opt,arg|
case opt
when "--help"
usage; exit
when "--overwrite"
$overwrite = true
end
end
$cdn = ARGV[0]
$library = ARGV[1]
$version = ARGV[2]
if $version.nil? or $version == ""
usage; exit
end
def base_url
{
"google" => "http://ajax.googleapis.com/ajax/libs/",
"microsoft" => "http://ajax.aspnetcdn.com/ajax/",
}[$cdn]
end
def verify_directory(dirname)
FileUtils.mkdir_p(dirname) unless Dir.exists?(dirname)
end
def need_file?(file)
local = "#{$cdn}/#{$library}/#{file}"
$overwrite or !File.exists?(local)
end
def get_http(file)
file = "#{$library}/#{file}"
uri = URI.parse(base_url + file)
http = Net::HTTP.new(uri.host, uri.port)
http.request(Net::HTTP::Get.new(uri.request_uri))
end
def read_file(file)
res = get_http(file)
res.body if res.code == "200"
end
def fetch_file(file)
return unless need_file?(file)
print "Fetching #{file} "
res = get_http(file)
if res.code == "200"
local = "#{$cdn}/#{$library}/#{file}"
verify_directory(File.dirname(local))
content = res.body
File.open(local, 'w'){ |f| f.write(content) }
puts "[OK]"
else
content = nil
puts "[error: #{res.code}]"
end
content
end
case $library
when 'jquery'
if $cdn == "google"
fetch_file "#{$version}/jquery.js"
fetch_file "#{$version}/jquery.min.js"
elsif $cdn == "microsoft"
fetch_file "jquery-#{$version}.js"
fetch_file "jquery-#{$version}.min.js"
end
when 'jqueryui'
$library = "jquery.ui" if $cdn == "microsoft"
fetch_file "#{$version}/jquery-ui.min.js"
themes = %w[base ui-lightness ui-darkness smoothness start redmond sunny
overcast le-frog flick pepper-grinder eggplant dark-hive cupertino
south-street blitzer humanity hot-sneaks excite-bike vader dot-luv
mint-choc black-tie trontastic swanky-purse]
themes.each do |theme|
file = "#{$version}/themes/#{theme}/jquery-ui.css"
if css = need_file?(file) ? fetch_file(file) : read_file(file)
fetch_file("#{$version}/themes/#{theme}/minified/jquery-ui.min.css") if theme == "base"
images = css.scan(/url\(\s*['"]?([^'"\s)]+)['"]?\s*\)/).map{ |i| i[0] }.uniq
images.reject!{ |i| i.match(/^data:/) } # embedded image data
images.each{ |image| fetch_file("#{$version}/themes/#{theme}/#{image}") }
else
puts "Error: #{theme} theme not found."
end
end
end