Archive.org の View のカウントを取得

Archive.org の View のカウントの取得の API が 2019年1月から新しくなりました。

API の説明
[Views data service api](https://archive.org/services/docs/api/views_api.html)

次のページを例として、取得してみます。
>トータルの閲覧数は各ページに表示されますが、API を使うことで、最近１ヶ月と、最近１週間の閲覧数が取得できます。

[思い出 (Omoide) by Dazai Osamu](https://archive.org/details/omoide_dazai)
[Gusukoubudori no denki](https://archive.org/details/gusukoubudorinodenki)
[Saigo no Kokyuuhiki (最後の胡弓弾き)]
(https://archive.org/details/saigonokokyuuhiki)

1) Curl で取得

```bash
curl https://be-api.us.archive.org/views/v1/short/omoide_dazai
curl https://be-api.us.archive.org/views/v1/short/gusukoubudorinodenki
curl https://be-api.us.archive.org/views/v1/short/saigonokokyuuhiki
```

2) Python3 で取得

```py:get_views.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	fetch_view.py
#
#					Jan/24/2019
#
# ------------------------------------------------------------------
import	sys
import	os
import	json
import	requests
#
#
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
url_archive = sys.argv[1]
sys.stderr.write(url_archive + "\n")
#
url_target = "https://be-api.us.archive.org/views/v1/short/" + url_archive
args = {}
rr=requests.get(url_target,args)
print(rr.text)
unit_aa = json.loads(rr.text)
#
print("all_time = %d" % unit_aa[url_archive]['all_time'])
print("last_30day = %d" % unit_aa[url_archive]['last_30day'])
print("last_7day = %d" % unit_aa[url_archive]['last_7day'])
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
```

実行方法

```bash
./get_views.py omoide_dazai
```

3) Node.js で取得

```js:get_views.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	get_views.js
//
//				  Jan/24/2019
//
// ---------------------------------------------------------------
console.error ("*** 開始 ***")
//
const url_archive = process.argv[2]
console.log(url_archive)
const url_target = "https://be-api.us.archive.org/views/v1/short/" + url_archive

var Client = require('node-rest-client').Client

var client = new Client()

client.get(url_target, function (data, response) {
	console.log(data)
//
	console.log("all_time = " + data[url_archive]['all_time'])
	console.log("last_30day = " + data[url_archive]['last_30day'])
	console.log("last_7day = " + data[url_archive]['last_7day'])
	console.error ("*** 終了 ***")
})
//
// ---------------------------------------------------------------
```

実行方法

```bash
./get_views.js gusukoubudorinodenki
```

4) PHP で取得
>Arch Linux でのライブラリーのインストール

>```bash
yay php-requests
yay php-igbinary
```

```php:get_views.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
//	get_views.php
//
//					  Jan/24/2019
//
// ------------------------------------------------------------------
// include('Requests/library/Requests.php');
include('/usr/share/pear/Requests.php');

fputs (STDERR,"*** 開始 ***\n");
$url_archive = $argv[1];

Requests::register_autoloader();

$url_target = "https://be-api.us.archive.org/views/v1/short/" . $url_archive;

$request = Requests::get($url_target, array('Accept' => 'application/json'));

var_dump($request->status_code);

$json_string = $request->body;
$unit_aa = json_decode ($json_string,true);
print("-----------\n");
// var_dump($unit_aa);
print("all_time = " . $unit_aa[$url_archive]['all_time'] . "\n");
print("last_30day = " . $unit_aa[$url_archive]['last_30day'] . "\n");
print("last_7day = " . $unit_aa[$url_archive]['last_7day'] . "\n");

fputs (STDERR,"*** 終了 ***\n");
// ------------------------------------------------------------------
?>
```

実行方法

```bash
./get_views.php saigonokokyuuhiki
```