-
Notifications
You must be signed in to change notification settings - Fork 142
/
goLive.php
133 lines (119 loc) · 5.17 KB
/
goLive.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
<?php
if (php_sapi_name() !== "cli") {
die("You may only run this inside of the PHP Command Line! If you did run this in the command line, please report: \"".php_sapi_name()."\" to the InstagramLive-PHP Repo!");
}
logM("Loading InstagramLive-PHP v0.3...");
set_time_limit(0);
date_default_timezone_set('America/New_York');
//Load Depends from Composer...
require __DIR__.'/vendor/autoload.php';
use InstagramAPI\Instagram;
use InstagramAPI\Request\Live;
require_once 'config.php';
/////// (Sorta) Config (Still Don't Touch It) ///////
$debug = false;
$truncatedDebug = false;
/////////////////////////////////////////////////////
if (IG_USERNAME == "USERNAME" || IG_PASS == "PASSWORD") {
logM("Default Username and Passwords have not been changed! Exiting...");
exit();
}
//Login to Instagram
logM("Logging into Instagram...");
$ig = new Instagram($debug, $truncatedDebug);
try {
$ig->login(IG_USERNAME, IG_PASS);
} catch (\Exception $e) {
echo 'Error While Logging in to Instagram: '.$e->getMessage()."\n";
exit(0);
}
//Block Responsible for Creating the Livestream.
try {
if (!$ig->isMaybeLoggedIn) {
logM("Couldn't Login! Exiting!");
exit();
}
logM("Logged In! Creating Livestream...");
$stream = $ig->live->create();
$broadcastId = $stream->getBroadcastId();
$ig->live->start($broadcastId);
// Switch from RTMPS to RTMP upload URL, since RTMPS doesn't work well.
$streamUploadUrl = preg_replace(
'#^rtmps://([^/]+?):443/#ui',
'rtmp://\1:80/',
$stream->getUploadUrl()
);
//Grab the stream url as well as the stream key.
$split = preg_split("[".$broadcastId."]", $streamUploadUrl);
$streamUrl = $split[0];
$streamKey = $broadcastId.$split[1];
logM("================================ Stream URL ================================\n".$streamUrl."\n================================ Stream URL ================================");
logM("======================== Current Stream Key ========================\n".$streamKey."\n======================== Current Stream Key ========================");
logM("^^ Please Start Streaming in OBS/Streaming Program with the URL and Key Above ^^");
logM("Live Stream is Ready for Commands:");
newCommand($ig->live, $broadcastId, $streamUrl, $streamKey);
logM("Something Went Super Wrong! Attempting to At-Least Clean Up!");
$ig->live->getFinalViewerList($broadcastId);
$ig->live->end($broadcastId);
} catch (\Exception $e) {
echo 'Error While Creating Livestream: '.$e->getMessage()."\n";
}
/**
* The handler for interpreting the commands passed via the command line.
*/
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("Enabled Comments!");
} elseif ($line == 'dcomments') {
$live->disableComments($broadcastId);
logM("Disabled Comments!");
} elseif ($line == 'stop' || $line == 'end') {
fclose($handle);
//Needs this to retain, I guess?
$live->getFinalViewerList($broadcastId);
$live->end($broadcastId);
logM("Stream Ended!\nWould you like to keep the stream archived for 24 hours? Type \"yes\" to do so or anything else to not.");
print "> ";
$handle = fopen ("php://stdin","r");
$archived = trim(fgets($handle));
if ($archived == 'yes') {
logM("Adding to Archive!");
$live->addToPostLive($broadcastId);
logM("Livestream added to archive!");
}
logM("Wrapping up and exiting...");
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:\nStatus: $status\nMuted: $muted\nViewer Count: $count");
} elseif ($line == 'viewers') {
logM("Viewers:");
$live->getInfo($broadcastId);
foreach ($live->getViewerList($broadcastId)->getUsers() as &$cuser) {
logM("@".$cuser->getUsername()." (".$cuser->getFullName().")");
}
} elseif ($line == 'help') {
logM("Commands:\nhelp - Prints this message\nurl - Prints Stream URL\nkey - Prints Stream Key\ninfo - Grabs Stream Info\nviewers - Grabs Stream Viewers\necomments - Enables Comments\ndcomments - Disables Comments\nstop - Stops the Live Stream");
} else {
logM("Invalid Command. Type \"help\" for help!");
}
fclose($handle);
newCommand($live, $broadcastId, $streamUrl, $streamKey);
}
/**
* Logs a message in console but it actually uses new lines.
*/
function logM($message) {
print $message."\n";
}