forked from biblys/isbn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-ranges.php
72 lines (62 loc) · 2.31 KB
/
update-ranges.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/*
* This file is part of the biblys/isbn package.
*
* (c) Clément Bourgoin
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*
* This script is used to update the ISBN ranges. See README.md for more info.
*
*/
use GuzzleHttp\Client;
include('vendor/autoload.php');
echo "Requesting range information...\n";
$client = new Client();
$res = $client->request('POST', 'https://www.isbn-international.org/?q=bl_proxy/GetRangeInformations', [
'form_params' => [
'format' => 1,
'language' => 'en',
'translatedTexts' => 'Printed;Last Change'
]
]);
$result = json_decode($res->getBody()->getContents());
$value = $result->result->value;
$filename = $result->result->filename;
$url = sprintf('https://www.isbn-international.org/?q=download_range/%s/%s', $value, $filename);
echo "Getting XML from $url...\n";
$res = $client->request('GET', $url);
$xml = $res->getBody()->getContents();
echo "Converting to PHP array...\n";
$ranges = (array) simplexml_load_string($xml);
$ranges = json_encode($ranges);
$ranges = json_decode($ranges, true);
$prefixes = (array) $ranges['EAN.UCCPrefixes']['EAN.UCC'];
$groups = (array) $ranges['RegistrationGroups']['Group'];
foreach ($groups as &$group) {
// Fix entries with a single "range", converting it to an array
if (isset($group['Rules']['Rule']['Range'])) {
$group['Rules']['Rule'] = array($group['Rules']['Rule']);
}
}
$file = dirname(__FILE__) . '/src/Biblys/Isbn/ranges-array.php';
echo "Saving to $file...\n";
file_put_contents($file,
'<?php ' . "\n"
. '/*' . "\n"
. ' * This file is generated automatically by update-ranges.php, do not edit' . "\n"
. ' * manually! See README.md for more info. ' . "\n"
. ' *' . "\n"
. ' * This file is part of the biblys/isbn package.' . "\n"
. ' *' . "\n"
. ' * (c) Clément Bourgoin' . "\n"
. ' *' . "\n"
. ' * This package is Open Source Software. For the full copyright and license' . "\n"
. ' * information, please view the LICENSE file which was distributed with this' . "\n"
. ' * source code.' . "\n"
. ' */' . "\n\n"
. '$groups = ' . var_export($groups, TRUE) . ";\n"
. '$prefixes = ' . var_export($prefixes, TRUE) . ";\n"
);