-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
147 lines (125 loc) · 3.2 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<html>
<head>
<script type="text/javascript" src="sorttable.js"></script>
</head>
<body>
<table class="sortable">
<tr>
<td>PP</td><td>Facebook</td><td>Twitter</td>
<td>Population</td><td>FB per 100k</td><td>Twitter per 100k</td><td>Average per 100k</td>
</tr>
<?php
include "config.php"; // twitter api key
include "lib/twitter.php";
include "lib/fetchUsingCache.php";
$ppeuParties = [
"AT", "BE", "CZ", "DE", "EE", "ES", "FI", "FR", "GR",
"HR", "LU", "NL", "PL", "RO", "SE", "SI", "UK"
// TODO ,"IT", "CAT" << not in piratetimes api
];
$population = array(
"AT" => 8527230,
"BE" => 11203787,
"CZ" => 10517400,
"DE" => 80781000,
"EE" => 1315819,
"ES" => 46507800,
"FI" => 5462939,
"FR" => 65991000,
"GR" => 11123034,
"HR" => 4267558,
"IT" => 60762320,
"LU" => 549700,
"NL" => 16868500,
"PL" => 38496000,
"RO" => 19942642,
"SE" => 9705005,
"SI" => 2063998,
"UK" => 64105654
);
////////
$parties = array();
$twitterIds = array();
foreach ($ppeuParties as $partyCC) {
$pirateTimesURL = "http://api.piratetimes.net/api/v1/parties/pp".strtolower($partyCC);
if ($pirateTimesJSON = fetchJSONUsingCache($pirateTimesURL)) {
// PirateTimes
$fbId = $pirateTimesJSON->{"socialNetworks"}->{"facebook"}->{"id"};
$twitterId = $pirateTimesJSON->{"socialNetworks"}->{"twitter"}->{"username"};
$twitterIds[] = $twitterId;
// FB
$fbURL = "https://graph.facebook.com/".$fbId;
$fbJSON = fetchJSONUsingCache($fbURL);
$fbPop = $fbJSON->{"likes"};
$parties[$partyCC] = array(
"cc" => $partyCC,
"fbPop" => $fbPop,
"fbId" => $fbId,
"twitterId" => $twitterId
);
}
}
// Twitter
$twitterQuery = urlencode(trim(implode(',', $twitterIds)));
$twitterURL = "https://api.twitter.com/1.1/users/lookup.json?screen_name=".$twitterQuery;
$twitterJSON = fetchJSONUsingCache($twitterURL);
foreach ($twitterJSON as $twitterResult) {
foreach ($parties as $party) {
if (strtolower($party["twitterId"]) == strtolower($twitterResult->{"screen_name"})) {
$parties[$party["cc"]]["twitterPop"] = $twitterResult->{"followers_count"};
}
}
}
// Output
$popTotal = 0;
$fbPopTotal = 0;
$twitterPopTotal = 0;
foreach ($parties as $party) {
$pop = $population[$party['cc']];
$popTotal += $pop;
$fbPopTotal += $party['fbPop'];
$twitterPopTotal += $party['twitterPop'];
$fbPer100k = $party['fbPop']/($pop/100000);
$twitterPer100k = $party['twitterPop']/($pop/100000);
echo tableRow(array(
$party['cc'],
'<a href="https://facebook.com/profile.php?id='
.$party['fbId']
.'">'
.$party['fbPop']
.'</a>',
'<a href="https://twitter.com/'
.$party['twitterId']
.'">'
.$party['twitterPop']
.'</a>',
$pop,
round($fbPer100k),
round($twitterPer100k),
round(($fbPer100k+$twitterPer100k)/2)
));
}
$fbPer100kTotal = $fbPopTotal/($popTotal/100000);
$twitterPer100kTotal = $twitterPopTotal/($popTotal/100000);
echo tableRow(array(
'*',
$fbPopTotal,
$twitterPopTotal,
$popTotal,
round($fbPer100kTotal),
round($twitterPer100kTotal),
round(($fbPer100kTotal+$twitterPer100kTotal)/2)
));
////////// helpers
function tableRow($contentArray) {
$out = '<tr>';
foreach ($contentArray as $content) {
$out .= '<td>'.$content.'</td>'."\n";
}
$out .= '</tr>'."\n";
return $out;
}
?>
</table>
</body>
</html>