-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlive.php
More file actions
179 lines (159 loc) · 7.55 KB
/
live.php
File metadata and controls
179 lines (159 loc) · 7.55 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
<?php
// controlo che sia da cli e non web
if (php_sapi_name() !== "cli") {
die("You may only run this script inside of the PHP Command Line! If you did run this in the command line, please report: \"" . php_sapi_name());
}
logM("Loading InstaLive-PHP by zProAle");
require("config.php");
$verification_method = 1; // 0 = SMS 1 = Email per la challange
class ExtendedInstagram extends \InstagramAPI\Instagram {
public function changeUser($username2, $password2) {$this->_setUser( $username2, $password2 );}
}
function readln( $prompt ) { // funzione per inserire il codice di verifica
if ( PHP_OS === 'WINNT' ) {echo "$prompt ";return trim( (string) stream_get_line( STDIN, 6, "\n" ) );}
return trim( (string) readline( "$prompt " ) );
}
$ig = new ExtendedInstagram(false, $truncatedDebug);
if ($username == "USERNAME" || $password == "PASSWORD") { // controllo che non siano le predefinite
logM("Non hai cambiato username e password");
exit();
}
use InstagramAPI\Request\Live;
use InstagramAPI\Response\Model\User;
use InstagramAPI\Response\Model\Comment;
try {
// tento acesso all'account
$ig->login($username, $password);
} catch (\Exception $exception) {
$response = $exception->getResponse();
$fatalError = "Fatal error ".PHP_EOL;
file_put_contents("fatals.txt",$fatalError,FILE_APPEND);
file_put_contents("fatals.txt",$exception,FILE_APPEND);
file_put_contents("fatals.txt",$response,FILE_APPEND);
file_put_contents("fatals.txt",var_dump($exception),FILE_APPEND);
file_put_contents("fatals.txt",print_r($exception),FILE_APPEND);
if ($response->getErrorType() === 'checkpoint_challenge_required') { // effettuo la richiesta di challange
sleep(3);
$checkApiPath = substr( $response->getChallenge()->getApiPath(), 1);
$customResponse = $ig->request($checkApiPath)->setNeedsAuth(false)->addPost('choice', $verification_method)->addPost('_uuid', $ig->uuid)
->addPost('guid', $ig->uuid)->addPost('device_id', $ig->device_id)->addPost('_uid', $ig->account_id)->addPost('_csrftoken', $ig->client->getToken())->getDecodedResponse();
} else { // non posso risolvere il check point della challange
echo 'Non riesco a risolvere la pre-challange'.PHP_EOL;
exit();
}
try { // faccio inserire il codice ottenuto per verificare la challange
if ($customResponse['status'] === 'ok' && $customResponse['action'] === 'close') {
exit();
}
$code = readln( 'Inserisci il codice ricevuto via ' . ( $verification_method ? 'email' : 'sms' ) . ':' );
$ig->changeUser($username, $password);
$customResponse = $ig->request($checkApiPath)->setNeedsAuth(false)->addPost('security_code', $code)->addPost('_uuid', $ig->uuid)->addPost('guid', $ig->uuid)->addPost('device_id', $ig->device_id)->addPost('_uid', $ig->account_id)->addPost('_csrftoken', $ig->client->getToken())->getDecodedResponse();
if ($customResponse['status'] === 'ok' && (int) $customResponse['logged_in_user']['pk'] === (int) $user_id ) {
} else {
file_put_contents("customResponse.txt",var_dump($customResponse),FILE_APPEND);
file_put_contents("customResponse.txt",print_r($customResponse),FILE_APPEND);
}
echo 'Puo essere necessarrio riavviare il programma per fixare!';
}
catch ( Exception $ex ) {
echo $ex->getMessage();
}
}
try{
if (!$ig->isMaybeLoggedIn) {
logM("Non sei loggato!");
exit();
}
logM("Creazione streaming in corso...");
// creo la stream
$stream = $ig->live->create();
$broadcastId = $stream->getBroadcastId();
$ig->live->start($broadcastId);
$streamUploadUrl = $stream->getUploadUrl();
// grabbo id e url
$split = preg_split("[" . $broadcastId . "]", $streamUploadUrl);
$streamUrl = $split[0];
$streamKey = $broadcastId . $split[1];
// visualizzo url e key
logM("================================ Stream URL ================================\n" . $streamUrl . "\n================================ Stream URL ================================");
logM("======================== Current Stream Key ========================\n" . $streamKey . "\n======================== Current Stream Key ========================");
logM("\n^^ Perfavore avvia la diretta in OBS/Streaming tramite questo url e key ^^ \n");
logM("La live streams è inzia, scrivi un comando:");
newCommand($ig->live, $broadcastId, $streamUrl, $streamKey);
logM("Ci sono degli errori stoppo la diretta!");
$ig->live->getFinalViewerList($broadcastId);
$ig->live->end($broadcastId);
}catch (\Exception $e) {
echo 'Errore: impossibile creare la live: ' . $e->getMessage() . "\n";
}
/**
* The handler for interpreting the commands passed via the command line. thanks to https://github.com/machacker16/
*/
function newCommand(Live $live, $broadcastId, $streamUrl, $streamKey) {
print "\n> ";
$handle = fopen ("php://stdin","r");
$line = trim(fgets($handle));
if($line == 'ecomments') {
$live->enableComments($broadcastId);
logM("Commenti abilitati!");
} elseif ($line == 'dcomments') {
$live->disableComments($broadcastId);
logM("Commenti dibilitati!");
} elseif ($line == 'stop' || $line == 'end') {
fclose($handle);
$live->getFinalViewerList($broadcastId);
$live->end($broadcastId);
logM("La live è terminata! Vuoi salavare la live per 24h sul tuo profilo? Scegli \"si\" o \"no\" ");
print "> ";
$handle = fopen ("php://stdin","r");
$archived = trim(fgets($handle));
if ($archived == 'yes') {
logM("Aggiungo la live..");
$live->addToPostLive($broadcastId);
logM("Live aggiunta al profilo!");
}
logM("Chiusura in corso..");
exit();
} elseif ($line == 'url') {
logM("================================ Stream URL ================================\n".$streamUrl."\n================================ Stream URL ================================");
} elseif ($line == 'key') {
logM("======================== Current Stream Key ========================\n".$streamKey."\n======================== Current Stream Key ========================");
} elseif ($line == 'info') {
$info = $live->getInfo($broadcastId);
$status = $info->getStatus();
$muted = var_export($info->is_Messages(), true);
$count = $info->getViewerCount();
logM("Info:\nStato: $status\nMutata: $muted\nNumero visualizzatori: $count");
} elseif($line == 'commenta'){
logM("Inserisci il testo da inviare: ");
print "> ";
$handle = fopen ("php://stdin","r");
$text = trim(fgets($handle));
if ($text !== "") {
$live->comment($broadcastId, $text);
logM("Commento inviato!");
} else {
logM("Il testo del commento è vuoto!");
}
} elseif ($line == 'viewers') {
logM("Viewers:");
$live->getInfo($broadcastId);
foreach ($live->getViewerList($broadcastId)->getUsers() as &$cuser) {
logM("@".$cuser->getUsername()." (".$cuser->getFullName().")");
}
} elseif ($line == 'help') {
logM("Commandi:\nhelp - Stampa questo messaggio\nurl - Stampa url della diretta\nkey - Stampa la streams key\ninfo - Ottengo le info\nviewers - Stampo i viewers\necomments - Attivo i commenti\ndcomments - Disattivo i commenti\ncommenta - Manda un messaggio alla diretta\nstop - Interrompo la live");
} else {
logM("Comando non trovato! Scrivi \"help\" per la lista comandi");
}
fclose($handle);
newCommand($live, $broadcastId, $streamUrl, $streamKey);
}
/**
* Logs a message in console but it actually uses new lines.
* @param string $message message to be logged.
*/
function logM($message)
{
print $message . "\n";
}