-
Notifications
You must be signed in to change notification settings - Fork 1
/
json2csv.php
129 lines (116 loc) · 3.12 KB
/
json2csv.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
//$file = "ep_meps_current";
$file = "ep_mep_active";
$countries = array();
$sum = 0;
$tt = json_decode (file_get_contents ("./countries.json"));
foreach ($tt as $t) {
$countries [$t->name] = $t->iso_code;
$countrySum [ $t->iso_code] = 0;
}
$fp = fopen($file.'.csv', 'w');
$meps= json_decode (file_get_contents($file.'.json'));
$out=array ('epid','country','first_name','last_name','email','birthdate','gender','eugroup','party','phone','building','office','committee','substitute','delegation', 'twitter','tttpid','since');
fputcsv($fp, $out);
foreach ($meps as $mep){
if (!$mep->active) {
echo "\nskip ". $mep->Name->full;
continue;
}
if (!isset( $mep->Birth)) {
// echo "\npotential skip ghost ". $mep->Name->full;
// continue;
}
if (!isset ($mep->Mail))
$mep->Mail = [""];
if (isset($mep->Birth))
//bday = strtotime(substr($mep->Birth->date,0,10));
$bday = substr($mep->Birth->date,0,10);
else
$bday="";
if (isset($mep->Gender))
$gender=$mep->Gender;
else
$gender="";
$out = array (
$mep->UserID,
//$countries[$mep->Constituencies[0]->country],
$mep->Constituencies[0]->country,
$mep->Name->sur,
$mep->Name->family,
$mep->Mail[0],
$bday,
$gender);
if (isset( $mep->Groups)) {
$since="9999-99-99";
foreach ($mep->Groups as $g) {
if ($since > $g->start) $since=substr($g->start,0,10);
}
if (is_array ($mep->Groups[0]->groupid)) {
$mep->Groups[0]->groupid = implode ("/",$mep->Groups[0]->groupid);
}
$out[]=$mep->Groups[0]->groupid;
}
$out[]=$mep->Constituencies[0]->party;
if (isset($mep->Addresses) && isset ($mep->Addresses->Brussels)) {
$out[] = $mep->Addresses->Brussels->Phone;
$out[] = $mep->Addresses->Brussels->Address->Building;
$out[] = $mep->Addresses->Brussels->Address->Office;
} else {
$out[]= null;
$out[]= null;
$out[]= null;
}
if (is_array($out[6]))
$out[6] = implode('/',$out[6]);
if (isset($mep->Committees)) {
$com = array();
$sub = array();
foreach ($mep->Committees as $c) {
if ($c->end != "9999-12-31T00:00:00") {
continue;
}
if ($c->role == "Substitute") {
$sub [] = $c->abbr;
} else {
$com [] = $c->abbr;
}
}
//$out[] = str_replace (",","",implode(' ',$com));
//$out[] = str_replace (",","",implode(' ',$sub));
$out[] = implode(' ',$com);
$out[] = implode(' ',$sub);
} else {
$out [] = '';
$out [] = '';
}
if (isset($mep->Delegations)){
$del = array();
foreach ($mep->Delegations as $d) {
if ($d->end != "9999-12-31T00:00:00") {
continue;
}
if (!empty($d->abbr)){
$del [] = $d->abbr;
} else {
$del [] = str_replace (array ("Delegation for relations with the ","Delegation to the "),array ("",""),$d->Organization);
}
}
$out[] = implode(',',$del);
} else {
$out [] = '';
}if (property_exists($mep,"Twitter") && is_array($mep->Twitter)){
$out [] = trim($mep->Twitter[0]);
}else {
$out [] = '';
}
$out[]= "mep_tttp_". $mep->UserID;
$out[]= $since;
$countrySum[ $countries[$out[1]]] += 1;
$sum +=1;
fputcsv($fp, $out);
if ($mep->UserID==96730) {echo "sven";}
}
print_r($countrySum);
echo "\nMEPs found $sum\n";
fclose ($fp);