Skip to content

Instantly share code, notes, and snippets.

@sorrycc
Created March 27, 2013 07:02
Show Gist options
  • Select an option

  • Save sorrycc/5252317 to your computer and use it in GitHub Desktop.

Select an option

Save sorrycc/5252317 to your computer and use it in GitHub Desktop.
require "net/http"
def get_diff_percentage(url)
res = nil
res_webp = nil
url.sub!("http://", "")
server, *path = url.split("/")
path = "/#{path.join("/")}"
path_webp = "#{path}_.webp"
Net::HTTP.start(server, 80) { |http|
res = http.head path
res_webp = http.head path_webp
}
size = res["Content-Length"].to_i
size_webp = res_webp["Content-Length"].to_i
ret = (size-size_webp).to_f / size.to_f
ret
end
def get_average(imgs)
total = 0.0
p "getting"
imgs.each do |img|
perc = get_diff_percentage(img)
p perc
total += perc
end
average = total.to_f / imgs.size
left = ((1-average)*100).round(2)
p "-------"
p "count: #{imgs.size}, average: #{average}, save: #{left}%"
p ""
p ""
end
index_p4p_imgs = [
"http://img02.taobaocdn.com/bao/uploaded/i2/17928031428644105/T1JBQ4XodXXXXXXXXX_!!0-item_pic.jpg_120x120.jpg",
"http://img02.taobaocdn.com/bao/uploaded/i2/899421123/T2w7YwXd8aXXXXXXXX_!!899421123.jpg_120x120.jpg",
"http://img01.taobaocdn.com/bao/uploaded/i1/815242864/T2wxvuXjpaXXXXXXXX_!!815242864.jpg_120x120.jpg",
"http://img02.taobaocdn.com/bao/uploaded/i2/11299020571160598/T1wt0LXClXXXXXXXXX_!!0-item_pic.jpg_120x120.jpg",
"http://img01.taobaocdn.com/bao/uploaded/i1/897618233/T2kUbrXnXXXXXXXXXX_!!897618233.jpg_120x120.jpg",
"http://img02.taobaocdn.com/bao/uploaded/i2/884392429/T2wHirXgNbXXXXXXXX_!!884392429.jpg_120x120.jpg",
"http://img04.taobaocdn.com/bao/uploaded/i4/1128267916/T2YVToXddXXXXXXXXX_!!1128267916.jpg_120x120.jpg",
"http://img02.taobaocdn.com/bao/uploaded/i2/1036315960/T2oAVXXnpOXXXXXXXX_!!1036315960.jpg_120x120.jpg",
"http://img02.taobaocdn.com/bao/uploaded/i2/1056092017/T28AYgXcxbXXXXXXXX_!!1056092017.jpg_120x120.jpg",
"http://img03.taobaocdn.com/bao/uploaded/i3/105638796/T2h2bxXbNaXXXXXXXX_!!105638796.jpg_120x120.jpg",
"http://img01.taobaocdn.com/bao/uploaded/i1/51289504/T2552lXgRXXXXXXXXX_!!51289504.jpg_120x120.jpg",
"http://img01.taobaocdn.com/bao/uploaded/i1/290867105/T2v9iPXltbXXXXXXXX_!!290867105.jpg_120x120.jpg"
]
detail_imgs = [
"http://img03.taobaocdn.com/imgextra/i3/82937928/T2c4zjXXlaXXXXXXXX_!!82937928.jpg",
"http://img03.taobaocdn.com/imgextra/i3/82937928/T2ZEbkXaFaXXXXXXXX_!!82937928.jpg",
"http://img03.taobaocdn.com/imgextra/i3/82937928/T2ZEbkXaFaXXXXXXXX_!!82937928.jpg",
"http://img03.taobaocdn.com/imgextra/i3/82937928/T2ZEbkXaFaXXXXXXXX_!!82937928.jpg",
"http://img03.taobaocdn.com/imgextra/i3/82937928/T2ZEbkXaFaXXXXXXXX_!!82937928.jpg",
"http://img04.taobaocdn.com/imgextra/i4/82937928/T2CO5.XXBaXXXXXXXX_!!82937928.jpg",
"http://img01.taobaocdn.com/imgextra/i1/82937928/T2VKaJXXxcXXXXXXXX_!!82937928.jpg",
"http://img03.taobaocdn.com/imgextra/i3/82937928/T22tfgXXVbXXXXXXXX_!!82937928.jpg",
"http://img01.taobaocdn.com/imgextra/i1/82937928/T2t7PjXkFXXXXXXXXX_!!82937928.jpg",
"http://img02.taobaocdn.com/imgextra/i2/82937928/T24k_jXiJXXXXXXXXX_!!82937928.jpg",
"http://img03.taobaocdn.com/imgextra/i3/82937928/T2u2rjXlhaXXXXXXXX_!!82937928.jpg",
"http://img03.taobaocdn.com/imgextra/i3/82937928/T22hnfXb8bXXXXXXXX_!!82937928.jpg",
"http://img04.taobaocdn.com/imgextra/i4/82937928/T2OoTiXolaXXXXXXXX_!!82937928.jpg",
"http://img03.taobaocdn.com/imgextra/i3/82937928/T24gnjXcVaXXXXXXXX_!!82937928.jpg",
"http://img01.taobaocdn.com/imgextra/i1/82937928/T236rjXhFXXXXXXXXX_!!82937928.jpg",
"http://img01.taobaocdn.com/imgextra/i1/82937928/T2kRTjXidXXXXXXXXX_!!82937928.jpg",
"http://img04.taobaocdn.com/imgextra/i4/82937928/T24srjXgRaXXXXXXXX_!!82937928.jpg",
"http://img02.taobaocdn.com/imgextra/i2/82937928/T2ICHjXeRXXXXXXXXX_!!82937928.jpg",
"http://img02.taobaocdn.com/imgextra/i2/899421123/T2Fn15XchbXXXXXXXX_!!899421123.jpg",
"http://img03.taobaocdn.com/imgextra/i3/899421123/T2NFDwXbFaXXXXXXXX_!!899421123.jpg",
"http://img04.taobaocdn.com/imgextra/i4/899421123/T22vYvXiRaXXXXXXXX_!!899421123.jpg",
"http://img04.taobaocdn.com/imgextra/i4/899421123/T2el6vXjxaXXXXXXXX_!!899421123.jpg",
"http://img02.taobaocdn.com/imgextra/i2/899421123/T2bjrwXl4XXXXXXXXX_!!899421123.jpg",
"http://img01.taobaocdn.com/imgextra/i1/899421123/T2Nm6oXodaXXXXXXXX_!!899421123.jpg",
"http://img01.taobaocdn.com/imgextra/i1/899421123/T2K6jvXhlaXXXXXXXX_!!899421123.jpg",
"http://img01.taobaocdn.com/imgextra/i1/899421123/T2bVYwXd4aXXXXXXXX_!!899421123.jpg",
"http://img02.taobaocdn.com/imgextra/i2/899421123/T2Ah2wXhBXXXXXXXXX_!!899421123.jpg",
"http://img02.taobaocdn.com/imgextra/i2/899421123/T2_6vbXb4bXXXXXXXX_!!899421123.jpg",
"http://img04.taobaocdn.com/imgextra/i4/899421123/T2.qDwXipaXXXXXXXX_!!899421123.jpg",
"http://img02.taobaocdn.com/imgextra/i2/899421123/T2g82vXf4aXXXXXXXX_!!899421123.jpg",
"http://img04.taobaocdn.com/imgextra/i4/899421123/T25lC3Xg8bXXXXXXXX_!!899421123.jpg",
"http://img02.taobaocdn.com/imgextra/i2/899421123/T2ZyzwXbhXXXXXXXXX_!!899421123.jpg",
"http://img04.taobaocdn.com/imgextra/i4/899421123/T2J1SPXgBbXXXXXXXX_!!899421123.jpg",
"http://img04.taobaocdn.com/imgextra/i4/899421123/T2bjPwXlBXXXXXXXXX_!!899421123.jpg",
"http://img04.taobaocdn.com/imgextra/i4/899421123/T22QOEXm4bXXXXXXXX_!!899421123.jpg",
"http://img04.taobaocdn.com/imgextra/i4/899421123/T26AWUXdpbXXXXXXXX_!!899421123.jpg"
]
get_average index_p4p_imgs
get_average detail_imgs
@sorrycc
Copy link
Author

sorrycc commented Mar 27, 2013

"getting"
0.7902973395931142
0.6857616863472236
0.7906659169411198
0.5745135770793244
0.6484659386375455
0.5130982863896002
0.6294546436285097
0.8282554728052358
0.7718571763419018
0.7712054660810151
0.6854607699294348
0.6322784204990917
"-------"
"count: 12, average: 0.6934428911894264, save: 30.66%"
""
""
"getting"
0.6531676130012966
0.7241379310344828
0.7241379310344828
0.7241379310344828
0.7241379310344828
0.5722226654964228
0.6647289536349629
0.26953125
0.8755439564008243
0.8750868716787705
0.8920375834706088
0.866639389487127
0.8785529798258749
0.8584061035021602
0.8795648585790382
0.8613427816324037
0.8541312805655794
0.8393498849897268
0.48623719496960105
0.7064035659926071
0.8056513486780549
0.9997686598539283
0.7017661348565426
0.6015760569993671
0.48376060973497315
0.814585635359116
0.829912582781457
0.8057104508571777
0.6910318746623447
0.49704674487475253
0.80663612221671
0.8126462482450211
0.8093083723348934
0.812484993997599
0.4925620414518118
0.5215805663907652
"-------"
"count: 36, average: 0.7337646416849845, save: 26.62%"
""
""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment