-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmatch.php
64 lines (62 loc) · 2.32 KB
/
match.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
<?php
$fh = fopen(__DIR__ . '/offshore_leaks_csvs/Officers.csv', 'r');
/*
Array
(
[0] => name
[1] => icij_id
[2] => valid_until
[3] => country_codes
[4] => countries
[5] => node_id
[6] => sourceID
)
*/
$officers = array();
$header = fgetcsv($fh, 4096);
while ($line = fgetcsv($fh, 4096)) {
$name = strtolower(preg_replace('/[^a-z]/i', '', $line[0]));
if (!isset($officers[$name])) {
$officers[$name] = array();
}
$officers[$name][$line[5]] = $line[6];
}
$jsons = array('2012.json', '2014.json', '2016.json');
$fh = fopen(__DIR__ . '/match.csv', 'w');
fputcsv($fh, array('公職', '推薦政黨', '中文姓名', '英文姓名', 'node_id', '選舉黃頁網址'));
foreach ($jsons AS $jsonFile) {
$json = json_decode(file_get_contents(__DIR__ . '/' . $jsonFile), true);
foreach ($json AS $item) {
$nameFound = false;
if (!empty($item['names'])) {
foreach ($item['names'] AS $name) {
if (!in_array(count($name), array(2, 3))) {
continue;
}
if (false === $nameFound) {
$nameKey = strtolower($name[0] . $name[1] . (isset($name[2]) ? $name[2] : ''));
if (isset($officers[$nameKey])) {
$nameFound = implode(',', $name);
}
}
if (false === $nameFound) {
$nameKey = strtolower($name[1] . (isset($name[2]) ? $name[2] : '') . $name[0]);
if (isset($officers[$nameKey])) {
$nameFound = implode(',', $name);
}
}
}
}
if (false !== $nameFound) {
foreach ($item['candidates'] AS $candidate) {
$icij = array();
foreach ($officers[$nameKey] AS $nodeId => $source) {
$icij[] = "[{$source}]{$nodeId}";
}
$pos = strrpos($candidate[3], '/');
$data = json_decode(file_get_contents('http://localhost/~kiang/elections/api/candidates/view/' . substr($candidate[3], $pos + 1)), true);
fputcsv($fh, array("[{$candidate[0]}]{$candidate[1]}", $data['candidate']['Candidate']['party'], $candidate[2], $nameFound, implode('|', $icij), $candidate[3]));
}
}
}
}