Skip to content

Instantly share code, notes, and snippets.

@hydra1983
Last active August 29, 2015 13:56
Show Gist options
  • Save hydra1983/9174709 to your computer and use it in GitHub Desktop.
Save hydra1983/9174709 to your computer and use it in GitHub Desktop.
从多看阅读的 **wifi 传书** 中下载上传的文件
# encoding: utf-8
# ----------------------------------------
# 最终的解决方案是利用这个脚本生成的URL,
# 通过迅雷下载所有的文件
# ----------------------------------------
Encoding.default_internal = Encoding::UTF_8
Encoding.default_external = Encoding::UTF_8
require 'open-uri'
require 'json'
DOMAIN = '192.168.1.104'
PORT = '12121'
REMOTE_ROOT = "http://#{DOMAIN}:#{PORT}/files"
FAILURES = []
def download(url, count)
if url.empty?
return
end
fileName = /([^\/]+)$/.match(url)[0]
if !File.exists?(fileName)
puts "[INFO] Downloading No.#{count} '#{fileName}'"
uri = URI::encode(url.strip)
puts "[INFO] --> #{uri}"
failed = false
begin
data = open(uri, 'User-Agent' => 'ruby'){|f| f.read}
rescue Exception => e
puts "[WARNING] Unable to download '#{fileName}'"
puts "[WARNING] #{e}"
failed = true
end
if !failed
file = File.new fileName, 'w+'
file.binmode
file << data
file.flush
file.close
else
FAILURES.push(fileName)
end
else
puts "[INFO] File exists '#{fileName}'"
end
end
def parse(url)
if url.empty?
return
end
uri = URI::encode(url.strip)
data = open(uri, 'User-Agent' => 'ruby'){|f| f.read}
files = JSON.parse(data)
return files.map{ |file|
file["name"]
};
end
def main
file_names = parse(REMOTE_ROOT).sort.reverse
puts "#{file_names.count} files to download"
count = 0
while (file_name = file_names.pop) != nil do
count += 1
download("#{REMOTE_ROOT}/#{file_name}", count)
end
end
main
puts FAILURES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment