-
Notifications
You must be signed in to change notification settings - Fork 4
/
get_follower_as_list.php
127 lines (117 loc) · 3.67 KB
/
get_follower_as_list.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
<?php
// äüö UTF8-FTW
if(isset($argv[1]) && !empty($argv[1])) {
$target_user = strtolower($argv[1]);
} else {
echo 'Error: No username provided'.PHP_EOL;
exit();
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Expect:', 'Content-Type: application/json', 'Client-ID: kimne78kx3ncx6brgo4mv6wki5h1ko']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_URL, 'https://gql.twitch.tv/gql');
$write_to_file = true;
if($write_to_file === true) {
$handle = fopen(__DIR__ . DIRECTORY_SEPARATOR . $target_user.'.tmp.txt', 'wb');
}
$error_counter = 0;
$cursor = '';
$keep_running = true;
while($keep_running === true) {
$gqlQuery = [[
'operationName' => 'Followers',
'variables' => [
'limit' => 100,
'login' => $target_user,
'order' => 'DESC',
],
'extensions' => [
'persistedQuery' => [
'version' => 1,
'sha256Hash' => 'deaf3a7c3227ae1bfb950a3d3a2ba8bd47a01a5b528c93ae603c20427e1d829d',
],
],
]];
if(!empty($cursor)) {
$gqlQuery[0]['variables']['cursor'] = $cursor;
}
redo_request:
if($error_counter >= 10) {
echo 'Over 10 errors after each other ... stopping here'.PHP_EOL;
if($write_to_file === true) {
fclose($handle);
unlink(__DIR__ . DIRECTORY_SEPARATOR . $target_user.'.tmp.txt');
}
exit();
}
// echo 'Requesting cursor: '.$cursor.PHP_EOL;
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($gqlQuery, JSON_UNESCAPED_UNICODE));
$curl_ouput = curl_exec($ch);
$curl_info = curl_getinfo($ch);
if($curl_info['http_code'] == 200) {
$json_decode = json_decode($curl_ouput, true);
if($json_decode !== null) {
$error_counter = 0;
if(isset($json_decode[0], $json_decode[0]['data'], $json_decode[0]['data']['user'], $json_decode[0]['data']['user']['followers'], $json_decode[0]['data']['user']['followers']['edges']) && !is_null($json_decode[0]['data']['user']['followers']['edges']) && count($json_decode[0]['data']['user']['followers']['edges']) > 0) {
$string = '';
foreach($json_decode[0]['data']['user']['followers']['edges'] as $follow) {
if(isset($follow['node']['login'])) {
$string .= $follow['node']['login'].PHP_EOL;
}
// Update cursor
$cursor = isset($follow['cursor']) ? $follow['cursor'] : '';
if(empty($cursor)) {
echo PHP_EOL.PHP_EOL.'Done!'.PHP_EOL;
$keep_running = false;
}
}
echo $string;
if($write_to_file === true && strlen($string) > 0) {
fwrite($handle, $string);
}
} else {
echo PHP_EOL.PHP_EOL.'Done!'.PHP_EOL;
$keep_running = false;
}
} else {
echo 'Json_decode error ('.json_last_error_msg().') on: '.$curl_ouput.PHP_EOL;
$error_counter++;
sleep(1);
goto redo_request;
}
} elseif($curl_info['http_code'] == 502) {
// Server error so retry
echo '502 error'.PHP_EOL;
$error_counter++;
sleep(1);
goto redo_request;
} elseif($curl_info['http_code'] == 503) {
// Server error so retry
echo '503 error'.PHP_EOL;
$error_counter++;
sleep(1);
goto redo_request;
} elseif($curl_info['http_code'] == 0) {
// Timeout so retry
echo 'Timeout error'.PHP_EOL;
echo 'Curl error: '.curl_error($ch).PHP_EOL;
$error_counter++;
sleep(1);
goto redo_request;
} else {
echo 'HTTP error: '.$curl_info['http_code'].PHP_EOL;
$error_counter++;
sleep(1);
goto redo_request;
}
}
if($write_to_file === true) {
fclose($handle);
rename(__DIR__ . DIRECTORY_SEPARATOR . $target_user.'.tmp.txt', __DIR__ . DIRECTORY_SEPARATOR . $target_user.'.txt');
}