Skip to content

Instantly share code, notes, and snippets.

@paralleltree
Created September 4, 2016 09:01
Show Gist options
  • Save paralleltree/6403446af841052b293b57476d94223f to your computer and use it in GitHub Desktop.
Save paralleltree/6403446af841052b293b57476d94223f to your computer and use it in GitHub Desktop.
#! /bin/sh
exec ruby -S -x "$0" "$@"
#! ruby
require 'time'
require 'open-uri'
require 'optparse'
## Example of execution
# timecrawler http://example.com/images/%Y%m%d/%H%M%S.png \
# -d /path/to/dir -since "2016-06-20 00:00:00" --until "2016-06-21 00:00:00" --interval $((60 * 5))
## strftime
# %Y 4-digit years
# %m 2-digit months
# %d 2-digit days
# %H 2-digit hours(24h)
# %M 2-digit minutes
# %S 2-digit seconds
# parse options
conf = {}.tap do |h|
ARGV << '-h' if ARGV.empty?
OptionParser.new("Usage: ruby timecrawler.rb [opions] [baseuri(formatted with strftime)]") do |opt|
opt.on('-y', '確認しない') { |v| h[:continue] = true }
opt.on('-q', '出力しない') { |v| h[:silent] = true }
opt.on('-d [VALUE]', '--dest [VALUE]', '保存先パス') { |v| h[:dest] = v }
opt.on('--interval [VALUE]', '間隔(秒) default:60') { |v| h[:interval] = v.to_i }
opt.on('--since VALUE', '開始時刻') { |v| h[:since] = v }
opt.on('--until [VALUE]', '終了時刻') { |v| h[:until] = v }
opt.on('--duration [VALUE]', '期間(秒)') { |v| h[:duration] = v.to_i }
opt.parse!(ARGV)
end
end
# validate parameters
conf[:interval] ||= 60
conf[:dest] ||= "."
baseuri = ARGV.pop
raise ArgumentError.new("Need to specify a uri to crawl") unless baseuri
raise ArgumentError.new("Need to specify either --until or --duration") unless conf[:until].nil? ^ conf[:duration].nil?
raise ArgumentError.new("--interval value must be greater than 0") unless conf[:interval] > 0
current = Time.parse(conf[:since])
finish = conf[:duration].nil? ? Time.parse(conf[:until]) : current + conf[:duration]
puts "The given range: #{current} -> #{finish}"
unless conf[:continue]
print "continue? (y/n)> "
exit 1 unless gets.chomp =~ /^y$/i
end
while (current <= finish)
uri = current.strftime(baseuri)
puts "Downloading #{uri} ..." unless conf[:silent]
begin
open(uri) do |s|
open(File.join(conf[:dest], File.basename(uri)), 'w') do
|f| f.write(s.read)
end
end
rescue => e
warn "Failed to download #{uri}. (#{e.inspect})"
end
current += conf[:interval]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment