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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/archaeo/cli.rb', line 16

def self.exit_on_failure?
  true
end

Instance Method Details

#after(url, timestamp) ⇒ Object



89
90
91
92
93
94
# File 'lib/archaeo/cli.rb', line 89

def after(url, timestamp)
  handle_errors do
    snap = CdxApi.new.after(url, timestamp: timestamp)
    output_snapshot(snap)
  end
end

#asset_audit(url, timestamp) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/archaeo/cli.rb', line 193

def asset_audit(url, timestamp)
  handle_errors do
    bundle = Fetcher.new.fetch_page_with_assets(
      url, timestamp: timestamp
    )
    report = build_audit_report(bundle)
    case options[:format]
    when "json"
      puts JSON.generate(report)
    else
      print_audit_report(report)
    end
  end
end

#available(url) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/archaeo/cli.rb', line 111

def available(url)
  handle_errors do
    result = AvailabilityApi.new.near(
      url, timestamp: options[:timestamp]
    )
    if result.available?
      puts "Available: #{result.archive_url}"
    else
      puts "Not available"
      exit 1
    end
  end
end

#before(url, timestamp) ⇒ Object



79
80
81
82
83
84
# File 'lib/archaeo/cli.rb', line 79

def before(url, timestamp)
  handle_errors do
    snap = CdxApi.new.before(url, timestamp: timestamp)
    output_snapshot(snap)
  end
end

#between(url, from, to) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/archaeo/cli.rb', line 100

def between(url, from, to)
  fmt = validate_output_format
  handle_errors do
    cdx = CdxApi.new
    snaps = cdx.between(url, from: from, to: to).to_a
    output_formatted(snaps, fmt)
  end
end

#count(url) ⇒ Object



248
249
250
251
252
# File 'lib/archaeo/cli.rb', line 248

def count(url)
  handle_errors do
    puts CdxApi.new.count(url)
  end
end

#diff(url, timestamp_a, timestamp_b) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/archaeo/cli.rb', line 177

def diff(url, timestamp_a, timestamp_b)
  handle_errors do
    bundle_a = Fetcher.new.fetch_page_with_assets(
      url, timestamp: timestamp_a
    )
    bundle_b = Fetcher.new.fetch_page_with_assets(
      url, timestamp: timestamp_b
    )
    output_diff(bundle_a.assets, bundle_b.assets,
                timestamp_a, timestamp_b)
  end
end

#download(url) ⇒ Object



218
219
220
221
222
223
224
225
226
# File 'lib/archaeo/cli.rb', line 218

def download(url)
  handle_errors do
    downloader = BulkDownloader.new(
      output_dir: options[:output],
      concurrency: options[:concurrency],
    )
    download_with_progress(downloader, url)
  end
end

#fetch(url, timestamp) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/archaeo/cli.rb', line 139

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

#fetch_assets(url, timestamp) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/archaeo/cli.rb', line 152

def fetch_assets(url, timestamp)
  handle_errors do
    bundle = Fetcher.new.fetch_page_with_assets(
      url, timestamp: timestamp
    )
    output_assets(bundle)
  end
end

#known_urls(domain) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/archaeo/cli.rb', line 230

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

#near(url, timestamp) ⇒ Object



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

def near(url, timestamp)
  handle_errors do
    snap = CdxApi.new.near(url, timestamp: timestamp)
    output_snapshot(snap)
  end
end

#newest(url) ⇒ Object



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

def newest(url)
  handle_errors do
    snap = CdxApi.new.newest(url)
    output_snapshot(snap)
  end
end

#num_pages(url) ⇒ Object



240
241
242
243
244
# File 'lib/archaeo/cli.rb', line 240

def num_pages(url)
  handle_errors do
    puts CdxApi.new.num_pages(url)
  end
end

#oldest(url) ⇒ Object



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

def oldest(url)
  handle_errors do
    snap = CdxApi.new.oldest(url)
    output_snapshot(snap)
  end
end

#rewrite(url, timestamp) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/archaeo/cli.rb', line 165

def rewrite(url, timestamp)
  handle_errors do
    coerced = Timestamp.coerce(timestamp)
    page = Fetcher.new.fetch(url, timestamp: coerced)
    rewritten = build_rewriter(url, coerced).rewrite_html(page.content)
    output_rewritten(rewritten)
  end
end

#save(url) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/archaeo/cli.rb', line 126

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

#snapshots(url) ⇒ Object



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

def snapshots(url)
  fmt = validate_output_format
  handle_errors do
    snaps = fetch_snapshots(url)
    output_formatted(snaps, fmt)
  end
end

#versionObject



21
22
23
# File 'lib/archaeo/cli.rb', line 21

def version
  puts "archaeo #{VERSION}"
end