-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_fetch.rb
68 lines (56 loc) · 1.68 KB
/
cache_fetch.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
require 'md5'
require 'fileutils'
require 'net/http'
require 'uri'
module CacheFetch
@@cachedir = ''
def self.cachedir=(value)
@@cachedir = File.expand_path(value)
FileUtils::mkdir_p File.join(@@cachedir, 'archives') unless File.exists? File.join(@@cachedir, 'archives')
end
def self.cachedir
@@cachedir
end
def urlcache url
File.join @@cachedir, MD5.new(url).to_s
end
def fetch_cache_to_file url
hashpath = File.join @@cachedir, 'archives', MD5.new(url).to_s
FileUtils::mkdir_p(hashpath) unless File.exists?(hashpath)
return File.read(File.join(hashpath, '.filename')) if File.exists?(File.join(hashpath, '.filename'))
res = nil
STDERR.puts "Downloading #{url}"
res = Net::HTTP.get_response(URI.parse(url))
targetname = ''
targetname = $1 if res['content-disposition'].to_s =~ /filename="(.*?)"/
targetname = $& if targetname.empty? && url =~ /[^\/]+$/
targetname = 'files' if targetname.empty?
content = nil
content = 'rar' if res['content-type'].to_s =~ /rar/
targetname += ".#{content}" unless content.nil? || targetname =~ /#{content}$/
filename = File.join hashpath, targetname
File.open(File.join(hashpath, '.filename'), 'w') {|io| io.write(filename)}
File.open(filename, 'w') do |io|
io.write(res.body)
end
filename
end
def fetch_cache url
hashfile = urlcache url
if File.exists?(hashfile)
STDERR.puts "Reading #{url} from cache"
io = File.open(hashfile, 'r')
res = Marshal.load(io)
io.close
res
else
STDERR.puts "Downloading #{url}"
res = Net::HTTP.get_response(URI.parse(url))
obj = block_given? ? yield(res) : res
io = File.open(hashfile, 'w')
Marshal.dump(obj, io)
io.close
obj
end
end
end