-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwitter.php
212 lines (180 loc) · 5.66 KB
/
twitter.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
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
<?php
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
// You can get all these via https://dev.twitter.com/
$consumer_key = "";
$consumer_secret = "";
$access_token = "";
$access_token_secret = "";
$user_id = 0; // Twitter user ID
$connection = new TwitterOAuth(
$consumer_key,
$consumer_secret,
$access_token,
$access_token_secret
);
// Get our list
$lists = $connection->get("lists/list", ["user_id" => $user_id]);
$list = $lists[0]; // We only have one list, 'cycle'
$new_users = [];
$finished = false;
$raw_statuses = [];
$conditions = [
'user_id' => $user_id,
'include_rts' => true,
'exclude_replies' => false,
'count' => 3200
];
$cutoff_date = strtotime("1 month ago");
$max_id = 0;
echo "Going through the timeline...\n";
while (!$finished) {
$data = $connection->get(
"statuses/user_timeline",
$conditions
);
// Loop through and grab the oldest ID
foreach ($data as $status) {
if (strtotime($status->created_at) >= $cutoff_date) {
$raw_statuses[] = $status;
$max_id = $status->id;
} else {
break;
$finished = true;
}
}
$tweet_count = count($raw_statuses);
echo "{$tweet_count} tweets\n";
if (isset($conditions['max_id'])) {
if ($conditions['max_id'] == $max_id) {
$finished = true;
}
}
$conditions['max_id'] = $max_id;
}
// First, eliminate any tweets that are older than a month
echo "Grabbing interaction tweets...\n";
$statuses = array_filter($raw_statuses, function($status) {
if ($status->in_reply_to_user_id) {
return true;
}
if (isset($status->retweeted_status)) {
return true;
}
if (isset($status->quoted_status)) {
return true;
}
});
echo "Building list of new users you interacted with...\n";
foreach ($statuses as $status) {
if (isset($status->in_reply_to_user_id, $new_users)) {
$new_users[] = $status->in_reply_to_user_id;
} elseif (isset($status->retweeted_status)) {
$new_users[] = $status->retweeted_status->user->id;
} elseif (isset($status->quoted_status)) {
$new_users[] = $status->quoted_status->user->id;
} elseif (isset($status->entities)) {
foreach ($status->entities->user_mentions as $mention) {
if (isset($mention->id) && !in_array($mention->id, $new_users)) {
$new_users[] = $mention->id;
}
}
}
}
// Grab everyone we favourited
echo "Grabbing favorites...\n";
$finished = false;
$max_id = 0;
$conditions = ['user_id' => $user_id, 'count' => 200];
$favorites = [];
while (!$finished) {
$data = $connection->get(
"favorites/list",
$conditions
);
// Loop through and grab the oldest ID
foreach ($data as $favorite) {
if (strtotime($favorite->created_at) >= $cutoff_date) {
$favorites[] = $favorite;
$max_id = $favorite->id;
} else {
break;
$finished = true;
}
}
if (isset($conditions['max_id'])) {
if ($conditions['max_id'] == $max_id) {
$finished = true;
}
}
$conditions['max_id'] = $max_id;
}
foreach ($favorites as $favorite) {
$new_users[] = $favorite->user->id;
}
// Get current users on our list
echo "Grabbing users currently on our cycle list...\n";
$members = $connection->get(
"lists/members",
['list_id' => $list->id, 'count' => 5000]
);
$users = $members->users;
$current_users = [];
foreach ($users as $user) {
$current_users[] = $user->id;
}
echo "Currently have " . count($current_users) . " on the cycle list\n";
// Filter out all the duplicates from our array
$new_users = array_unique($new_users);
// Generate our list of users to add to the list and ones to remove
$users_to_add = array_diff($new_users, $current_users);
$users_to_remove = array_diff($current_users, $new_users);
if (!empty($users_to_remove)) {
// There is a limit that we can only add 100 users at a time
$offset = 0;
$finished = false;
while (!$finished) {
$user_slice = array_slice($users_to_remove, $offset, 100);
$response = $connection->post(
"lists/members/destroy_all",
['list_id' => $list->id, 'user_id' => implode(",", $user_slice)]
);
if ($connection->getLastHttpCode() !== 200) {
echo "ERROR while trying to remove users from the cycle list\n";
print_r($response);
exit();
}
// Look to see if we have more records to process
if (count($users_to_remove) > ($offset + 100)) {
$offset = $offset + 100;
} else {
$finished = true;
}
echo "Removed " . count($users_to_remove) . " accounts you didn't interact with\n";
}
}
// Next, add people who aren't already in the lists
if (!empty($users_to_add)) {
// There is a limit that you can only add 100 users at a time
$offset = 0;
$finished = false;
while (!$finished) {
$user_slice = array_slice($users_to_add, $offset, 100);
$response = $connection->post(
"lists/members/create_all",
['list_id' => $list->id, 'user_id' => implode(",", $user_slice)]
);
if ($connection->getLastHttpCode() !== 200) {
echo "ERROR while trying to add users to the cycle list\n";
print_r($response);
exit();
}
// Look to see if we have more records to process
if (count($users_to_add) > ($offset + 100)) {
$offset = $offset + 100;
} else {
$finished = true;
}
}
echo "Added " . count($users_to_add) . " to the cycle list\n";
}