-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.php
More file actions
275 lines (209 loc) · 9.72 KB
/
Copy pathmain.php
File metadata and controls
275 lines (209 loc) · 9.72 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* main.php
*
* Created by Pamela L. Gay
* Use: StarStryder
* Date: 7/10/17
* Time: 12:49 PM
*
* This file
* 0) Initialize Everything
* 2) Find out who else the Followers Follow
* 3) Get list of unique accoutns being followed and how often each is followed
* 4) Get the details for hubs followed by a large fraction of the people following the seed
* 5) Let's map the network by finding out how connected each of the hubs are to one another
*
*/
/***********************************************************************************************
* 0) Iniitalize everything
***********************************************************************************************/
// INCLUDES ------------------------------------------------------------------------------------
// Tests to make sure code is not doing evil
require_once('tests.php');
// Library for connecting to Twitter API
require_once('TwitterAPIExchange.php');
// Setup all need tokens and values
require_once('config.php'); // COPY config_SAMPLE.php to config.php and add in your values
echo "starting...\n";
// Initial variables ---------------------------------------------------------------------------
$ids_array = array(); // we'll store all user ids in this to reduce DB calls
/***********************************************************************************************
* 2) Find out who else the Followers Follow
***********************************************************************************************/
// NOTES: We are getting the list of people the followers follow - for some this list is
// unreasonably large. The typical person follows around 2000, but it doesn't cost any extra
// server time to get up to 5000 followers
// ASSUMPTION: If more than 5000 followed, listed isn't curated in a way that makes it entirely
// useful for this research. This number can be reset if decide this still returns non-useful.
$cutoff = 5000;
// Put all the userid information into an array so their "friends" (those they are following
// can be pulled from the twitter API.) 15 calls of 5000 per 15 min
// https://dev.twitter.com/rest/reference/get/friends/ids
// IF YOU NEED TO RESTART, COMMENT OUT EVERYTHING ABOVE HERE AND RESTART CODE
$query = "SELECT * FROM tweeps WHERE friends < 5001 AND DONE = 0";
$i = 0;
$tweeps = test_mysql_q($query, $conn);
foreach ($tweeps as $tweep) {
array_push($ids_array, $tweep['tweep_id']);
$i++;
}
echo "Ready to process $i followers.\n";
$call = 1; // Flag to check how many times this had been called
foreach ($ids_array as $id) {
echo "Processing $id";
$query = "UPDATE tweeps SET done = 1 WHERE tweep_id = $id";
test_mysql_q($query, $conn);
$url = "https://api.twitter.com/1.1/friends/ids.json";
$getfield = '?user_id='.$id;
$objs = json_decode($twitter->setGetfield($getfield)->buildOauth($url, 'GET')->performRequest(), true);
test_twlist($objs);
// these are 5000 responses max
if (isset($objs['ids'])) {
if (is_array($objs['ids'])) {
echo ".";
$cursor = $objs['next_cursor'];
foreach ($objs['ids'] as $chk_id) {
$query = "INSERT INTO connections (tweep_id, friend_id) VALUES ('$id', '$chk_id')";
test_mysql_q($query, $conn);
}
}
else echo "error: malformed Objs";
}
else { // If it's not set and it's not an array, something went wrong, dump an error
// Check if the person locked their account
if (!strncmp("Not auth", $objs['error'], 8)) {
echo "- Protected account";
echo ".\n";
}
else
{
var_dump($objs);
echo "\n";
}
$cursor = 0;
}
$call++;
if ($call > 15) {
$call = 1;
echo "PAUSE";
sleep(15.1*60);
}
echo "\n";
}
/***********************************************************************************************
* 3) Get list of unique accoutns being followed and how often each is followed
***********************************************************************************************/
// Start by getting distinct and inserting
$query = "SELECT distinct friend_id FROM connections";
$tweep_network = test_mysql_q($query, $conn);
foreach ($tweep_network as $tweep) {
$query = "INSERT INTO network (tweep_id, count) VALUES ('" . $tweep['friend_id'] . "', '0')";
test_mysql_q($query, $conn);
}
// Now see how often each of these is in the network and update the count
foreach ($tweep_network as $tweep) {
$query = "SELECT count(*) as N FROM connections WHERE friend_id ='" . $tweep['friend_id'] ."'";
$results = test_mysql_q($query, $conn);
$result = mysqli_fetch_assoc($results);
foreach($result as $N);
$query = "UPDATE network SET count = $N WHERE tweep_id = '" . $tweep['friend_id'] ."'";
test_mysql_q($query, $conn);
}
/***********************************************************************************************
* 4) Get the details for hubs followed by a large fraction of the people following the seed
***********************************************************************************************/
// NOTES: Calculate how many people need to follow someone for them to be a hub
// This actually only returns how many people are non-protected accounts, which is
// fine since we know nothing about protected accounts.
// hubSize = Total Follower Number * Depth
$query = "select count(distinct tweep_id) as N FROM connections";
$result = test_mysql_q($query, $conn);
$followers = mysqli_fetch_assoc($result);
foreach ($followers as $total);
$hubSize = $total * $depth;
// Find all the hubs
$call = 1;
$curr = 0;
$query = "SELECT * FROM network WHERE count >= $hubSize ORDER BY count DESC";
$hubs = test_mysql_q($query, $conn);
echo "getting hub details. \n";
// Get all their details
foreach ($hubs as $hub) {
// see documentation https://dev.twitter.com/rest/reference/get/users/show
$url = 'https://api.twitter.com/1.1/users/show.json';
$getfield = '?user_id='.$hub['tweep_id'];
$obj = json_decode($twitter->setGetfield($getfield)->buildOauth($url, 'GET')->performRequest(), true);
test_twshow($obj);
$user_id = $obj['id_str'];
$user_name = $obj['screen_name'];
$name = addslashes($obj['name']);
$descrip = addslashes($obj['description']);
$followers = $obj['followers_count'];
$friends = $obj['friends_count'];
$tweets = $obj['statuses_count'];
$created = $obj['created_at'];
if ($obj['verified']) $verify = 1; else $verify = 0;
$status = $obj['status'];
$update = $status['created_at'];
$query = "UPDATE network SET tweep_id = '$user_id', tweep_username = '$user_name', tweep_name='$name', description='$descrip',
followers=$followers, friends=$friends, tweets=$tweets, done=1, verified=$verify, created_at='$created', update_at='$update'
WHERE tweep_id = '".$hub['tweep_id']."'";
test_mysql_q($query, $conn);
$call++; $curr++;
if ($call > 900) {
$call = 1;
echo "Pause before starting $curr";
sleep(15.1*60);
}
}
/***********************************************************************************************
* 5) Let's map the network by finding out how connected each of the hubs are to one another
*
* This is a multi-step process
***********************************************************************************************/
// 1. Put all the hubs into an array
$hubs_array = array();
$i = 0;
foreach ($hubs as $hub) {
$hubs_array[$i] = array("tweep_id"=>$hub['tweep_id'], "tweep_username"=>$hub['tweep_username']);
$i++;
}
// 2. Create a table of all possible combinations
$max = $i-1; // because 0 indexed
for($i = 0; $i<$max; $i++) {
for($j=$max; $j != $i; $j--){
$query = "INSERT INTO combos (HubA_id, HubA_name, HubB_id, HubB_name) values
('".$hubs_array[$i]['tweep_id']."', '".$hubs_array[$i]['tweep_username']."', '".$hubs_array[$j]['tweep_id']."', '".$hubs_array[$j]['tweep_username']."' )";
$result = test_mysql_q($query, $conn);
}
}
// 3. Put all combinations into a variable to step through
$query = "SELECT * FROM combos";
$combos = test_mysql_q($query, $conn);
// 4. For each follower, using a cutoff from aboe
$query = "SELECT * FROM tweeps WHERE friends < $cutoff";
$tweeps = test_mysql_q($query, $conn);
echo "checking ";
foreach ($tweeps as $tweep) {
echo " " . $tweep['id'] . " ";
// a. put the list of accounts they follow - their friends - in an array
$chk_array = array();
$chk_id = $tweep['tweep_id'];
$query = "SELECT * FROM connections WHERE tweep_id = '$chk_id'";
$friends = test_mysql_q($query, $conn);
foreach ($friends as $friend) {
array_push( $chk_array, $friend['friend_id'] );
}
// b. check if the combos are in the arrray, if they are, increment that count
foreach ($combos as $combo) {
if(in_array($combo['HubA_id'], $chk_array) && in_array($combo['HubB_id'], $chk_array)) {
$query = "UPDATE combos SET count = count + 1 WHERE id =" . $combo['id'];
test_mysql_q($query, $conn);
}
}
// c. release that array
unset($chk_array);
$chk_array = array();
}
// That *should* be it