tech.guitarrapc.cóm

Technical updates

Internet ArchiveのWayback Machineのサービスリニューアル に合わせて PowerShell で Wayback Machine APIを叩いてみよう

10/27 PM22:00 に知ったのですが、 Internet ArchiveのWayback Machine が一新されました。

すちゃらかコーダー - Internet ArchiveのWayback Machineが大幅にサービスをリニューアル

その中で、特に興味を引いたのがWayback Availability JSON APIです。

Wayback Machine APIs

今回、このAPIをPowerShell で叩いてみましょう。

目次

API概要

公開されている APIは、公式ページで確認してください。

ここでは、September, 24, 2013 の内容に沿って動作確認をしています。

API のベースuri は、http://archive.org/wayback/availableです。

url クエリ

urlクエリとして、?url=サイトアドレス とすることで、対象サイトの Wayback Machine があるか存在を確認し、存在する場合は uriを返してくれます。

例えば、有名な黒いサイトならこうです。

http://archive.org/wayback/available?url=http://neue.cc

ブラウザで移動すると、以下のようなJSON が返ってきます。

{"archived_snapshots":{"closest":{"available":true,"url":"http://web.archive.org/web/20130615021329/http://neue.cc/","timestamp":"20130615021329","status":"200"}}}
timestamp クエリ

オプションとして、 &timestamp=対象日付 とすることで、指定した日付にもっとも近いWayback Machine の存在を確認できます。

例えばこうです。

http://archive.org/wayback/available?url=http://neue.cc&timestamp=2006

ブラウザで移動するとJSONを確認できますね。

{"archived_snapshots":{"closest":{"available":true,"url":"http://web.archive.org/web/20060108065907/http://neue.cc:80/","timestamp":"20060108065907","status":"200"}}}

PowerShell でさくっと問い合わせる

見ての通り、 JSON で返ってくるので、 Invoke-WebRequest ではなく、 Invoke-RestMethod を利用しましょう。

もちろんInvoke-WebRequestでも可能ですが、 JSONを変換する必要があるので、 PowerShell 4.0 の人なら Invoke-RestMethodがベターでしょう。*1

ただ問い合わせるなら簡単です。

$json = Invoke-RestMethod -Method Get -Uri "http://archive.org/wayback/available?url=http://neue.cc×tamp=2006"
$json.archived_snapshots.closest

これで、JSON がobject に変換されます。

available url                                                          timestamp      status
--------- ---                                                          ---------      ------
     True http://web.archive.org/web/20060108065907/http://neue.cc:80/ 20060108065907 200   

PowerShell で 同期、非同期、並列に取得したい

さて、PowerShell でさくっと取れるのは当然です。これを、さらに使いやすく高速化してみましょう。

考えたのは与えた複数のURIごとに、同期非同期並列で処理するパターンです。

GitHub

サンプルリポジトリを用意しました。

GitHub - guitarrapc / PS-WaybackMachineAvailavility

概要

Cmdletは処理に合わせて3つあります。

それぞれの特徴は次のチャートの通りです。

Cmdlet pipeline input mode PreferUrlCount
Get-WaybackMachineAvailavility O Synchronous urls < 5
Get-WaybackMachineAvailavilityAsync X Aynchronous urls >= 10
Get-WaybackMachineAvailavilityPrallel X Parallel urls < 10
同期Cmdlet

Get-WaybackMachineAvailavility Cmdlet

  • パイプラインからの入力をサポートします
  • 同期処理のため、少数のURIを対象にした場合が望ましいでしょう
  • 具体的には、5 URI以下です
非同期Cmdlet

Get-WaybackMachineAvailavilityAsync Cmdlet

  • パイプラインからの入力をサポートしません
  • 各URI に対して 非同期に動作します。そのため、大量のURIを調べることに向いています
  • 具体的には 10 URI以上です
並列Cmdlet

Get-WaybackMachineAvailavilityParallel Cmdlet

  • パイプラインからの入力をサポートしません
  • 各URI に対して 並列(順不同)に動作します。そのため、ほどほどのURIを調べることに向いています
  • 具体的には 10 URI以下です

利用方法例

url のみの場合

全て 同様のパラメーターをサポートしています。

# Synchronous invokation
Get-WaybackMachineAvailavility -urls "http://tech.guitarrapc.com","http://neue.cc"

# Asynchronous invokation
Get-WaybackMachineAvailavilityAsync -urls "http://tech.guitarrapc.com","http://neue.cc"

# Parallel invokation
Get-WaybackMachineAvailavilityParallel -urls "http://tech.guitarrapc.com","http://neue.cc"

最新の結果が返ってきます。

available        : True
status           : 200
timestamp        : 20131008132313
url              : http://web.archive.org/web/20131008132313/http://tech.guitarrapc.com
queryInformation : {queryUri, url}

available        : True
status           : 200
timestamp        : 20040430174213
url              : http://web.archive.org/web/20040430174213/http://neue.cc:80/
queryInformation : {queryUri, url}

また、Get-WaybackMachineAvailavilityは、パイプラインからの入力としても動作可能です。

# Synchronous pipeline invokation
"http://tech.guitarrapc.com","http://neue.cc" | Get-WaybackMachineAvailavility
timestamp付 のみの場合

これも、どのCmdletでも利用要領は変わりません。

# Synchronous invokation with 20060101
Get-WaybackMachineAvailavility -url "http://tech.guitarrapc.com","http://neue.cc" -timestamp 20060101

# Asynchronous invokation
Get-WaybackMachineAvailavilityAsync -urls "http://tech.guitarrapc.com","http://neue.cc" -timestamp 20060101

# Parallel invokation
Get-WaybackMachineAvailavilityParallel -urls "http://tech.guitarrapc.com","http://neue.cc" -timestamp 20060101

この場合は、 200601に最も近い結果が返ってきます。

available        : True
status           : 200
timestamp        : 20131008132313
url              : http://web.archive.org/web/20131008132313/http://tech.guitarrapc.com
queryInformation : {queryUri, url}

available        : True
status           : 200
timestamp        : 20060108065907
url              : http://web.archive.org/web/20060108065907/http://neue.cc:80/
queryInformation : {queryUri, url}

まとめ

一度試してみてください。

Module にするほどでもないため、さくっと書きましたが 比較になると思います。

次回は、 上記コードを使って 同期、非同期、並列処理について見てみましょう。

*1:PowerShell 3.0には、ヘッダが途中で読めない問題がありますが、本件では影響でない....はず?