Skip to content

Commit

Permalink
Merge branch 'pr/89'
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib/wayback_machine_downloader.rb
  • Loading branch information
hartator committed Oct 27, 2017
2 parents 058fc62 + 5116b77 commit a7c3d9b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
4 changes: 4 additions & 0 deletions bin/wayback_machine_downloader
Expand Up @@ -18,6 +18,10 @@ option_parser = OptionParser.new do |opts|
options[:directory] = t
end

opts.on("-s", "--all-timestamps", "Download all snapshots/timestamps for a given website") do |t|
options[:all_timestamps] = true
end

opts.on("-f", "--from TIMESTAMP", Integer, "Only files on or after timestamp supplied (ie. 20060716231334)") do |t|
options[:from_timestamp] = t
end
Expand Down
50 changes: 44 additions & 6 deletions lib/wayback_machine_downloader.rb
Expand Up @@ -16,14 +16,15 @@ class WaybackMachineDownloader

VERSION = "2.1.1"

attr_accessor :base_url, :exact_url, :directory,
attr_accessor :base_url, :exact_url, :directory, :all_timestamps
:from_timestamp, :to_timestamp, :only_filter, :exclude_filter,
:all, :maximum_pages, :threads_count

def initialize params
@base_url = params[:base_url]
@exact_url = params[:exact_url]
@directory = params[:directory]
@all_timestamps = params[:all_timestamps]
@from_timestamp = params[:from_timestamp].to_i
@to_timestamp = params[:to_timestamp].to_i
@only_filter = params[:only_filter]
Expand Down Expand Up @@ -127,12 +128,49 @@ def get_file_list_curated
file_list_curated
end

def get_file_list_all_timestamps
file_list_curated = Hash.new
get_all_snapshots_to_consider.each_line do |line|
next unless line.include?('/')
file_timestamp = line[0..13].to_i
file_url = line[15..-2]
file_id = file_url.split('/')[3..-1].join('/')
file_id_and_timestamp = [file_timestamp, file_id].join('/')
file_id_and_timestamp = CGI::unescape file_id_and_timestamp
file_id_and_timestamp = file_id_and_timestamp.tidy_bytes unless file_id_and_timestamp == ""
if file_id.nil?
puts "Malformed file url, ignoring: #{file_url}"
else
if match_exclude_filter(file_url)
puts "File url matches exclude filter, ignoring: #{file_url}"
elsif not match_only_filter(file_url)
puts "File url doesn't match only filter, ignoring: #{file_url}"
elsif file_list_curated[file_id_and_timestamp]
puts "Duplicate file and timestamp combo, ignoring: #{file_id}" if @verbose
else
file_list_curated[file_id_and_timestamp] = {file_url: file_url, timestamp: file_timestamp}
end
end
end
puts "file_list_curated: " + file_list_curated.count.to_s
file_list_curated
end


def get_file_list_by_timestamp
file_list_curated = get_file_list_curated
file_list_curated = file_list_curated.sort_by { |k,v| v[:timestamp] }.reverse
file_list_curated.map do |file_remote_info|
file_remote_info[1][:file_id] = file_remote_info[0]
file_remote_info[1]
if @all_timestamps
file_list_curated = get_file_list_all_timestamps
file_list_curated.map do |file_remote_info|
file_remote_info[1][:file_id] = file_remote_info[0]
file_remote_info[1]
end
else
file_list_curated = get_file_list_curated
file_list_curated = file_list_curated.sort_by { |k,v| v[:timestamp] }.reverse
file_list_curated.map do |file_remote_info|
file_remote_info[1][:file_id] = file_remote_info[0]
file_remote_info[1]
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/test_wayback_machine_downloader.rb
Expand Up @@ -85,6 +85,11 @@ def test_file_download
assert_includes linux_page.read, "Linux Games"
end

def test_all_timestamps_being_respected
@wayback_machine_downloader.all_timestamps = true
assert_equal 68, @wayback_machine_downloader.get_file_list_curated.size
end

def test_from_timestamp_being_respected
@wayback_machine_downloader.from_timestamp = 20050716231334
file_url = @wayback_machine_downloader.get_file_list_curated["linux.htm"][:file_url]
Expand Down

0 comments on commit a7c3d9b

Please sign in to comment.