-
Notifications
You must be signed in to change notification settings - Fork 20
/
followrank.php
77 lines (66 loc) · 2.57 KB
/
followrank.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
<?php
$screenname = "ianbarber";
$matrix_file = $screenname . "tmatrix.php";
// First get a list of my followers
$following = getFromCache( $screenname );
if( !file_exists( $matrix_file ) ) {
// Now build a matrix of followers
$userlookup = array_flip( $following->ids );
$matrix = array();
$followercount = count( $following->ids );
foreach( $following->ids as $key => $id ) {
$user = array_fill( 0, $followercount, 0 );
$their_following = getFromCache( $id );
$intersect = array_intersect( $following->ids, $their_following->ids );
if( count( $intersect ) ) {
$divisor = 1 / count( $intersect );
foreach( $intersect as $shared_id ) {
$user[$userlookup[$shared_id]] = $divisor;
}
}
$matrix[$key] = $user;
}
file_put_contents( $matrix_file, '<?php $tmatrix = ' . var_export( $matrix, true ) . ";");
echo "Run again to calculate users\n";
} else {
include $matrix_file;
$vector = array();
$rc = Lapack::eigenValues( $tmatrix, $vector );
$result = array();
foreach( $vector[0] as $key => $value ) {
$result[$following->ids[$key]] = $value[0];
}
arsort( $result );
$url = "https://api.twitter.com/1/users/lookup.json?user_id=" .
implode(",", array_slice( array_keys( $result ), 0, 10 ) );
$contents = json_decode( file_get_contents( $url ) );
foreach( $contents as $i => $user ) {
echo $i, ": ", $user->screen_name, " ", "\n";
}
}
function getFromCache( $id ) {
$cacheFile = 'cache/' . $id . ".json";
if( !file_exists( $cacheFile ) ) {
$access = is_numeric( $id ) ? 'user_id=' . $id : "screen_name=" . $id;
// Using cURL so we can get the error code
$ch = curl_init();
curl_setopt ( $ch, CURLOPT_URL, "http://api.twitter.com/1/friends/ids.json?" . $access );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
$file = curl_exec( $ch );
if( curl_getinfo( $ch, CURLINFO_HTTP_CODE ) == 401 ) {
// account for private users
$data = new stdClass();
$data->ids = array();
$file = json_encode($data);
} else if( curl_getinfo( $ch, CURLINFO_HTTP_CODE ) == 200 ){
$data = json_decode($file);
} else {
die( "Twitter rate limit hit, try again later");
}
curl_close( $ch );
file_put_contents( $cacheFile, $file );
} else {
$data = json_decode( file_get_contents( $cacheFile ) );
}
return $data;
}