This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
101 lines (81 loc) · 3.24 KB
/
functions.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
<?php
/* ***********************************************
* Call Mailchimp API v3
*/
function mailchimp_api($url, $data = NULL) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, 'osmbe:'.MAILCHIMP_API_KEY);
if (!is_null($data)) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
$r = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array(
'code' => $httpcode,
'response' => json_decode($r)
);
}
/* ***********************************************
* Call Mailchimp API v3
*/
function hdyc_api($username) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://hdyc.neis-one.org/search/'.urlencode($username));
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$r = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array(
'code' => $httpcode,
'response' => json_decode($r)
);
}
/* ***********************************************
* Create members.json
*/
function get_members($file) {
global $config;
$url_members = 'https://us13.api.mailchimp.com/3.0/lists/'.$config['mailchimp']['members.id'].'/members?count=250&status=subscribed';
$r = mailchimp_api($url_members); $members = $r['response']->members;
$json = array();
foreach ($members as $m) {
$member = new MembersList\Member();
$member->firstname = $m->merge_fields->FNAME;
$member->lastname = $m->merge_fields->LNAME;
$member->location = $m->merge_fields->PLACE;
$member->username = $m->merge_fields->OSMUSER;
if (!empty($member->username)) {
$hdyc = hdyc_api($member->username);
if ($hdyc['code'] == 200 && isset($hdyc['response']->contributor)) {
$func = function($value) { return explode('=', $value); };
$countries = (isset($hdyc['response']->countries->countries) ? explode(';', $hdyc['response']->countries->countries) : array());
$countries = array_map($func, $countries);
$belgium = NULL; foreach ($countries as $c) { if ($c[0] === 'Belgium' || $c[1] === 'be') { $belgium = array( $c[2], $c[3] ); break; } }
$member->statistics = new MembersList\Member\Statistics();
$member->statistics->changes = (isset($hdyc['response']->changesets->changes) ? $hdyc['response']->changesets->changes : 0);
$member->statistics->changes_be = (isset($belgium) ? $belgium[1] : 0);
$member->statistics->changesets = (isset($hdyc['response']->changesets->no) ? $hdyc['response']->changesets->no : 0);
$member->statistics->changesets_be = (isset($belgium) ? $belgium[0] : 0);
$member->statistics->since = $hdyc['response']->contributor->since;
}
}
$json[] = $member;
}
usort($json, 'sort_members');
return file_put_contents($file, json_encode($json));
}
/* ***********************************************
* Sort members.json
*/
function sort_members($a, $b) {
if (strcasecmp($a->lastname, $b->lastname) === 0) {
return strcasecmp($a->firstname, $b->firstname);
}
return strcasecmp($a->lastname, $b->lastname);
}