Class: Archaeo::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/archaeo/cli.rb

Overview

Command-line interface powered by Thor.

Constant Summary collapse

CDX_OPTION_MAP =
{
  from: :from,
  to: :to,
  match_type: :match_type,
  filter: :filters,
  collapse: :collapse,
  sort: :sort,
  limit: :limit,
}.freeze

Instance Method Summary collapse

Instance Method Details

#available(url) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/archaeo/cli.rb', line 59

def available(url)
  result = AvailabilityApi.new.near(url)
  if result.available?
    puts "Available: #{result.archive_url}"
  else
    puts "Not available"
    exit 1
  end
end

#download(url) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/archaeo/cli.rb', line 100

def download(url)
  downloader = BulkDownloader.new(output_dir: options[:output])

  downloader.download(
    url,
    from: options[:from],
    to: options[:to],
    resume: options[:resume],
  ) do |current, total, snap|
    warn "[#{current}/#{total}] " \
         "#{snap.timestamp} #{snap.original_url}"
  end
end

#fetch(url, timestamp) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/archaeo/cli.rb', line 81

def fetch(url, timestamp)
  page = Fetcher.new.fetch(
    url, timestamp: timestamp,
         identity: options[:identity]
  )

  if options[:output]
    write_output(options[:output], page.content)
  else
    $stdout.write(page.content)
  end
end

#known_urls(domain) ⇒ Object



116
117
118
119
120
# File 'lib/archaeo/cli.rb', line 116

def known_urls(domain)
  CdxApi.new.known_urls(domain).each do |u|
    puts u
  end
end

#near(url, timestamp) ⇒ Object



41
42
43
44
# File 'lib/archaeo/cli.rb', line 41

def near(url, timestamp)
  snap = CdxApi.new.near(url, timestamp: timestamp)
  puts snap.archive_url
end

#newest(url) ⇒ Object



53
54
55
56
# File 'lib/archaeo/cli.rb', line 53

def newest(url)
  snap = CdxApi.new.newest(url)
  puts snap.archive_url
end

#oldest(url) ⇒ Object



47
48
49
50
# File 'lib/archaeo/cli.rb', line 47

def oldest(url)
  snap = CdxApi.new.oldest(url)
  puts snap.archive_url
end

#save(url) ⇒ Object



70
71
72
73
74
# File 'lib/archaeo/cli.rb', line 70

def save(url)
  result = SaveApi.new.save(url)
  label = result.cached? ? "Cached" : "Saved"
  puts "#{label}: #{result.archive_url}"
end

#snapshots(url) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/archaeo/cli.rb', line 28

def snapshots(url)
  cdx = CdxApi.new
  opts = build_cdx_options(options)
  snaps = cdx.snapshots(url, **opts).to_a
  case options[:format]
  when "json" then output_json(snaps)
  when "csv" then output_csv(snaps)
  else output_table(snaps)
  end
end

#versionObject



13
14
15
# File 'lib/archaeo/cli.rb', line 13

def version
  puts "archaeo #{VERSION}"
end