tech.guitarrapc.cóm

Technical updates

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

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=対象日付とすることで、指定した日付にもっとも近いWayback Machineの存在を確認できます。

http://archive.org/wayback/available?url=http://neue.cc×tamp=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&timestamp=2006"
$json.archived_snapshots.closest

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

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

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

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

GitHub

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

guitarrapc/PS-WaybackMachineAvailavility | GitHub

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

タイムスタンプ付 のみの場合

これも、どのCmdletでも利用要領は変わりません。この場合は、 200601に最も近い結果が返ってきます。

# 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

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には、ヘッダが途中で読めない問題がありますが、本件では影響でない....はず?