forked from vinch/kloutbe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresh.php
67 lines (51 loc) · 1.92 KB
/
refresh.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
<?php
date_default_timezone_set(timezone_name_from_abbr('CET'));
include_once 'config.inc.php';
include_once 'db.inc.php';
$query = mysql_query("SELECT twitter_screen_name, kscore, kchange FROM users ORDER BY last_update ASC LIMIT 0, 100"); // only 50 at a time
$users = array(
'usernames' => array(),
'scores' => array()
);
$results = array();
while ($user = mysql_fetch_assoc($query)) {
$users['usernames'][] = $user['twitter_screen_name'];
// Array with additonal information which will be used the calculate the change
$users['scores'][$user['twitter_screen_name']] = array(
'score' => $user['kscore'],
'change' => $user['kchange']
);
}
$users_chunked = array_chunk($users['usernames'], 5);
foreach ($users_chunked as $chunk) {
$result = json_decode(file_get_contents($api_endpoint.'?key='.$key.'&users='.urlencode(implode(',', $chunk))), true);
foreach ($result['users'] as $item) {
$results[$item['twitter_screen_name']] = $item;
}
// Avoid Klout API limitations - max 10/req second
sleep(0.5);
}
$now = date('Y-m-d H:i:s');
// Run trough the users to update them
foreach ($users['usernames'] as $username) {
// Check if there is a result for this user
if(isset($results[$username])) {
// Get the user's previous score
$old_score = $users['scores'][$username]['score'];
// Get the Klout query result
$result = $results[$username];
// Calculate the change if the score changed
if(!empty($old_score) && $result['kscore'] != $old_score) {
// Calculate the different between the old and the new score
$change = $result['kscore'] - $old_score;
}
else {
// Put the old change back
$change = $users['scores'][$username]['change'];
}
// Compose the fields that need to be updated
$set_sql = sprintf('kscore = \'%f\', kchange=\'%f\', ', $result['kscore'], $change);
}
// Update the user
mysql_query("UPDATE users SET $set_sql last_update = '$now' WHERE twitter_screen_name = '$username'");
}