Skip to content

Commit c7d2bc4

Browse files
authored
Merge pull request #2096 from PsyMan47/master
feat: Better implementation of live streaming methods
2 parents d80eac1 + a71847d commit c7d2bc4

File tree

1 file changed

+151
-16
lines changed

1 file changed

+151
-16
lines changed

instagrapi/mixins/media.py

Lines changed: 151 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,25 +1155,160 @@ def media_unpin(self, media_pk):
11551155
return self.media_pin(media_pk, True)
11561156

11571157

1158-
def media_schedule_livestream(self, title, auto_start=False):
1158+
def media_create_livestream(self, title="Instagram Live"):
1159+
"""
1160+
Create a new live broadcast.
1161+
1162+
Parameters
1163+
----------
1164+
title : str
1165+
The title of the live broadcast.
1166+
1167+
Returns
1168+
-------
1169+
dict
1170+
Information about the streaming server and the stream key.
1171+
"""
11591172
data = {
1173+
"_uuid": self.uuid,
1174+
"_uid": self.user_id,
1175+
"preview_height": 1920,
1176+
"preview_width": 1080,
11601177
"broadcast_message": title,
1161-
"internal_only": "false",
1162-
"source_type": "203",
1163-
"visibility": "0"
1178+
"broadcast_type": "RTMP",
1179+
"internal_only": 0,
1180+
"_csrftoken": self.token,
11641181
}
1165-
result = self.private_request("live/create/", data)
1166-
broadcast_id = result['broadcast_id']
1167-
if auto_start:
1168-
startRes = self.media_start_livestream(broadcast_id)
1169-
return result
1182+
try:
1183+
response = self.private_request("live/create/", data=data)
1184+
broadcast_id = response["broadcast_id"]
1185+
upload_url = response["upload_url"].split(str(broadcast_id))
1186+
if len(upload_url) >= 2:
1187+
stream_server = upload_url[0]
1188+
stream_key = f"{broadcast_id}{upload_url[1]}"
1189+
return {
1190+
"broadcast_id": broadcast_id,
1191+
"stream_server": stream_server,
1192+
"stream_key": stream_key,
1193+
}
1194+
except Exception as e:
1195+
self.logger.error(f"Error creating live broadcast: {e}")
1196+
raise
11701197

11711198
def media_start_livestream(self, broadcast_id):
1172-
result = self.private_request(f"live/{broadcast_id}/start/", {'empty': None})
1173-
return result["status"] == "ok"
1199+
"""
1200+
Start a live broadcast.
1201+
1202+
Parameters
1203+
----------
1204+
broadcast_id : str
1205+
The ID of the live broadcast.
1206+
1207+
Returns
1208+
-------
1209+
bool
1210+
True if the broadcast started successfully, False otherwise.
1211+
"""
1212+
data = {
1213+
"_uuid": self.uuid,
1214+
"_uid": self.user_id,
1215+
"should_send_notifications": 1,
1216+
"_csrftoken": self.token,
1217+
}
1218+
try:
1219+
response = self.private_request(f"live/{broadcast_id}/start/", data=data)
1220+
return response.get("status") == "ok"
1221+
except Exception as e:
1222+
self.logger.error(f"Error starting live broadcast: {e}")
1223+
return False
1224+
1225+
def media_end_livestream(self, broadcast_id):
1226+
"""
1227+
End a live broadcast.
11741228
1175-
def media_fetch_live_chat(self, broadcast_id, last_comment_ts=None):
1176-
params = None
1177-
if last_comment_ts:
1178-
params = {'last_comment_ts': last_comment_ts}
1179-
return self.private_request(f"live/{broadcast_id}/get_comment/", params=params)
1229+
Parameters
1230+
----------
1231+
broadcast_id : str
1232+
The ID of the live broadcast.
1233+
1234+
Returns
1235+
-------
1236+
bool
1237+
True if the broadcast ended successfully, False otherwise.
1238+
"""
1239+
data = {
1240+
"_uuid": self.uuid,
1241+
"_uid": self.user_id,
1242+
"_csrftoken": self.token,
1243+
}
1244+
try:
1245+
response = self.private_request(f"live/{broadcast_id}/end_broadcast/", data=data)
1246+
return response.get("status") == "ok"
1247+
except Exception as e:
1248+
self.logger.error(f"Error ending live broadcast: {e}")
1249+
return False
1250+
1251+
def media_get_livestream_info(self, broadcast_id):
1252+
"""
1253+
Retrieve information about the live broadcast.
1254+
1255+
Parameters
1256+
----------
1257+
broadcast_id : str
1258+
The ID of the live broadcast.
1259+
1260+
Returns
1261+
-------
1262+
dict
1263+
Information about the live broadcast.
1264+
"""
1265+
try:
1266+
response = self.private_request(f"live/{broadcast_id}/info/")
1267+
return response
1268+
except Exception as e:
1269+
self.logger.error(f"Error retrieving live info: {e}")
1270+
raise
1271+
1272+
def media_get_livestream_comments(self, broadcast_id):
1273+
"""
1274+
Retrieve comments from the live broadcast.
1275+
1276+
Parameters
1277+
----------
1278+
broadcast_id : str
1279+
The ID of the live broadcast.
1280+
1281+
Returns
1282+
-------
1283+
list
1284+
A list of comments.
1285+
"""
1286+
try:
1287+
response = self.private_request(f"live/{broadcast_id}/get_comment/")
1288+
if "comments" in response:
1289+
return [{"username": c["user"]["username"], "text": c["text"]} for c in response["comments"]]
1290+
return []
1291+
except Exception as e:
1292+
self.logger.error(f"Error retrieving live comments: {e}")
1293+
raise
1294+
1295+
def media_get_livestream_viewers(self, broadcast_id):
1296+
"""
1297+
Retrieve the list of viewers of the live broadcast.
1298+
1299+
Parameters
1300+
----------
1301+
broadcast_id : str
1302+
The ID of the live broadcast.
1303+
1304+
Returns
1305+
-------
1306+
list
1307+
A list of viewers.
1308+
"""
1309+
try:
1310+
response = self.private_request(f"live/{broadcast_id}/get_viewer_list/")
1311+
return [{"username": user["username"], "pk": user["pk"]} for user in response.get("users", [])]
1312+
except Exception as e:
1313+
self.logger.error(f"Error retrieving live viewers: {e}")
1314+
raise

0 commit comments

Comments
 (0)