downloader.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import sys
  2. import time
  3. import os
  4. import shutil
  5. import subprocess
  6. import threading
  7. from instagram_private_api_extensions import live, replay
  8. from instagram_private_api import ClientError
  9. from .logger import log, seperator
  10. from .comments import CommentsDownloader
  11. class NoLivestreamException(Exception):
  12. pass
  13. class NoReplayException(Exception):
  14. pass
  15. def main(api_arg, record_arg, settings_arg):
  16. global api
  17. global record
  18. global broadcast
  19. global mpd_url
  20. global settings
  21. settings = settings_arg
  22. api = api_arg
  23. record = record_arg
  24. get_user_info(record)
  25. def run_script(file):
  26. try:
  27. FNULL = open(os.devnull, 'w')
  28. if sys.version.split(' ')[0].startswith('2'):
  29. subprocess.call(["python", file], stdout=FNULL, stderr=subprocess.STDOUT)
  30. else:
  31. subprocess.call(["python3", file], stdout=FNULL, stderr=subprocess.STDOUT)
  32. except OSError as e:
  33. pass
  34. def get_stream_duration(compare_time, broadcast=None):
  35. try:
  36. if broadcast:
  37. record_time = int(time.time()) - int(compare_time)
  38. stream_time = int(time.time()) - int(broadcast['published_time'])
  39. stream_started_mins, stream_started_secs = divmod(stream_time - record_time, 60)
  40. else:
  41. stream_started_mins, stream_started_secs = divmod((int(time.time()) - int(compare_time)), 60)
  42. stream_duration_str = '%d minutes' % stream_started_mins
  43. if stream_started_secs:
  44. stream_duration_str += ' and %d seconds' % stream_started_secs
  45. return stream_duration_str
  46. except:
  47. return "not available"
  48. def record_stream(broadcast):
  49. try:
  50. def print_status(sep=True):
  51. heartbeat_info = api.broadcast_heartbeat_and_viewercount(broadcast['id'])
  52. viewers = broadcast.get('viewer_count', 0)
  53. if sep:
  54. seperator("GREEN")
  55. log('[I] Viewers : ' + str(int(viewers)) + " watching", "GREEN")
  56. log('[I] Airing time : ' + get_stream_duration(broadcast['published_time']), "GREEN")
  57. log('[I] Status : ' + heartbeat_info['broadcast_status'].title(), "GREEN")
  58. return heartbeat_info['broadcast_status'] not in ['active', 'interrupted']
  59. mpd_url = (broadcast.get('dash_manifest')
  60. or broadcast.get('dash_abr_playback_url')
  61. or broadcast['dash_playback_url'])
  62. output_dir = settings.save_path + '{}_{}_{}_{}_live_downloads'.format(settings.current_date, record, broadcast['id'], settings.current_time)
  63. dl = live.Downloader(
  64. mpd=mpd_url,
  65. output_dir=output_dir,
  66. user_agent=api.user_agent,
  67. max_connection_error_retry=3,
  68. duplicate_etag_retry=30,
  69. callback_check=print_status,
  70. mpd_download_timeout=5,
  71. download_timeout=10)
  72. except Exception as e:
  73. log('[E] Could not start downloading livestream: ' + str(e), "RED")
  74. seperator("GREEN")
  75. sys.exit(1)
  76. try:
  77. log('[I] Livestream found, beginning download...', "GREEN")
  78. if (broadcast['broadcast_owner']['username'] != record):
  79. log('[I] This livestream is a dual-live, the owner is "{}".'.format(broadcast['broadcast_owner']['username']), "YELLOW")
  80. seperator("GREEN")
  81. log('[I] Username : ' + record, "GREEN")
  82. print_status(False)
  83. log('[I] MPD URL : ' + mpd_url, "GREEN")
  84. seperator("GREEN")
  85. log('[I] Downloading livestream... press [CTRL+C] to abort.', "GREEN")
  86. if (settings.run_at_start is not "None"):
  87. try:
  88. thread = threading.Thread(target=run_script, args=(settings.run_at_start,))
  89. thread.daemon = True
  90. thread.start()
  91. log("[I] Executed file to run at start.", "GREEN")
  92. except Exception as e:
  93. log('[W] Could not run file: ' + str(e), "YELLOW")
  94. comment_thread_worker = None
  95. if settings.save_comments.title() == "True":
  96. try:
  97. comments_json_file = settings.save_path + '{}_{}_{}_{}_live_comments.json'.format(settings.current_date, record, broadcast['id'], settings.current_time)
  98. comment_thread_worker = threading.Thread(target=get_live_comments, args=(api, broadcast, comments_json_file, dl,))
  99. comment_thread_worker.start()
  100. except Exception as e:
  101. log('[E] An error occurred while checking comments: ' + e, "RED")
  102. dl.run()
  103. seperator("GREEN")
  104. log('[I] The livestream has ended.\n[I] Time recorded : {}\n[I] Stream duration : {}\n[I] Missing (approx.) : {}'.format(get_stream_duration(int(settings.current_time)), get_stream_duration(broadcast['published_time']), get_stream_duration(int(settings.current_time), broadcast)), "YELLOW")
  105. seperator("GREEN")
  106. stitch_video(dl, broadcast, comment_thread_worker)
  107. except KeyboardInterrupt:
  108. seperator("GREEN")
  109. log('[I] The download has been aborted by the user.\n[I] Time recorded : {}\n[I] Stream duration : {}\n[I] Missing (approx.) : {}'.format(get_stream_duration(int(settings.current_time)), get_stream_duration(broadcast['published_time']), get_stream_duration(int(settings.current_time), broadcast)), "YELLOW")
  110. seperator("GREEN")
  111. if not dl.is_aborted:
  112. dl.stop()
  113. stitch_video(dl, broadcast, comment_thread_worker)
  114. def stitch_video(dl, broadcast, comment_thread_worker):
  115. try:
  116. if comment_thread_worker and comment_thread_worker.is_alive():
  117. log("[I] Stopping comment downloading and saving comments (if any)...", "GREEN")
  118. comment_thread_worker.join()
  119. if (settings.run_at_finish is not "None"):
  120. try:
  121. thread = threading.Thread(target=run_script, args=(settings.run_at_finish,))
  122. thread.daemon = True
  123. thread.start()
  124. log("[I] Executed file to run at finish.", "GREEN")
  125. except Exception as e:
  126. log('[W] Could not run file: ' + e, "YELLOW")
  127. log('[I] Stitching downloaded files into video...', "GREEN")
  128. output_file = settings.save_path + '{}_{}_{}_{}_live.mp4'.format(settings.current_date, record, broadcast['id'], settings.current_time)
  129. try:
  130. if settings.clear_temp_files.title() == "True":
  131. dl.stitch(output_file, cleartempfiles=True)
  132. else:
  133. dl.stitch(output_file, cleartempfiles=False)
  134. log('[I] Successfully stitched downloaded files into video.', "GREEN")
  135. seperator("GREEN")
  136. sys.exit(0)
  137. except Exception as e:
  138. log('[E] Could not stitch downloaded files: ' + str(e), "RED")
  139. seperator("GREEN")
  140. sys.exit(1)
  141. except KeyboardInterrupt:
  142. log('[I] Aborted stitching process, no video was created.', "YELLOW")
  143. seperator("GREEN")
  144. sys.exit(0)
  145. def get_user_info(record):
  146. try:
  147. log('[I] Getting user info for "' + record + '"...', "GREEN")
  148. user_res = api.username_info(record)
  149. user_id = user_res['user']['pk']
  150. except Exception as e:
  151. log('[E] Could not get user info: ' + str(e), "RED")
  152. seperator("GREEN")
  153. sys.exit(1)
  154. except KeyboardInterrupt:
  155. log('[W] Aborted checking for user.', "YELLOW")
  156. seperator("GREEN")
  157. sys.exit(1)
  158. get_livestreams(user_id)
  159. if settings.save_replays.title() == "True":
  160. get_replays(user_id)
  161. else:
  162. seperator("GREEN")
  163. log("[I] Replay saving is disabled either with a flag or in the config file.", "BLUE")
  164. seperator("GREEN")
  165. sys.exit(0)
  166. def get_livestreams(user_id):
  167. try:
  168. seperator("GREEN")
  169. log('[I] Checking for ongoing livestreams...', "GREEN")
  170. broadcast = api.user_broadcast(user_id)
  171. if (broadcast is None):
  172. raise NoLivestreamException('There are no livestreams available.')
  173. else:
  174. try:
  175. record_stream(broadcast)
  176. except Exception as e:
  177. log('[E] An error occurred while trying to record livestream: ' + str(e), "RED")
  178. seperator("GREEN")
  179. sys.exit(1)
  180. except NoLivestreamException as e:
  181. log('[I] ' + str(e), "YELLOW")
  182. except Exception as e:
  183. if (e.__class__.__name__ is not NoLivestreamException):
  184. log('[E] Could not get livestreams info: ' + str(e), "RED")
  185. seperator("GREEN")
  186. sys.exit(1)
  187. def get_replays(user_id):
  188. try:
  189. seperator("GREEN")
  190. log('[I] Checking for available replays...', "GREEN")
  191. user_story_feed = api.user_story_feed(user_id)
  192. broadcasts = user_story_feed.get('post_live_item', {}).get('broadcasts', [])
  193. except Exception as e:
  194. log('[E] Could not get replay info: ' + str(e), "RED")
  195. seperator("GREEN")
  196. sys.exit(1)
  197. try:
  198. if (len(broadcasts) == 0):
  199. raise NoReplayException('There are no replays available.')
  200. else:
  201. log("[I] Available replays have been found to download, press [CTRL+C] to abort.", "GREEN")
  202. seperator("GREEN")
  203. for replay_index, broadcast in enumerate(broadcasts):
  204. exists = False
  205. if sys.version.split(' ')[0].startswith('2'):
  206. directories = (os.walk(settings.save_path).next()[1])
  207. else:
  208. directories = (os.walk(settings.save_path).__next__()[1])
  209. for directory in directories:
  210. if (str(broadcast['id']) in directory) and ("_live_" not in directory):
  211. log("[W] Already downloaded a replay with ID '" + str(broadcast['id']) + "', skipping...", "GREEN")
  212. exists = True
  213. if not exists:
  214. current = replay_index + 1
  215. log("[I] Downloading replay " + str(current) + " of " + str(len(broadcasts)) + " with ID '" + str(broadcast['id']) + "'...", "GREEN")
  216. current_time = str(int(time.time()))
  217. output_dir = settings.save_path + '{}_{}_{}_{}_replay_downloads'.format(settings.current_date, record, broadcast['id'], settings.current_time)
  218. dl = replay.Downloader(
  219. mpd=broadcast['dash_manifest'],
  220. output_dir=output_dir,
  221. user_agent=api.user_agent)
  222. if settings.clear_temp_files.title() == "True":
  223. replay_saved = dl.download(settings.save_path + '{}_{}_{}_{}_replay.mp4'.format(settings.current_date, record, broadcast['id'], settings.current_time), cleartempfiles=True)
  224. else:
  225. replay_saved = dl.download(settings.save_path + '{}_{}_{}_{}_replay.mp4'.format(settings.current_date, record, broadcast['id'], settings.current_time), cleartempfiles=False)
  226. if settings.save_comments.title() == "True":
  227. log("[I] Checking for available comments to save...", "GREEN")
  228. comments_json_file = settings.save_path + '{}_{}_{}_{}_replay_comments.json'.format(settings.current_date, record, broadcast['id'], settings.current_time)
  229. get_replay_comments(api, broadcast, comments_json_file, dl)
  230. if (len(replay_saved) == 1):
  231. log("[I] Finished downloading replay " + str(current) + " of " + str(len(broadcasts)) + ".", "GREEN")
  232. seperator("GREEN")
  233. else:
  234. log("[W] No output video file was made, please merge the files manually if possible.", "YELLOW")
  235. log("[W] Check if ffmpeg is available by running ffmpeg in your terminal/cmd prompt.", "YELLOW")
  236. log("", "GREEN")
  237. log("[I] Finished downloading all available replays.", "GREEN")
  238. seperator("GREEN")
  239. sys.exit(0)
  240. except NoReplayException as e:
  241. log('[I] ' + str(e), "YELLOW")
  242. seperator("GREEN")
  243. sys.exit(0)
  244. except Exception as e:
  245. log('[E] Could not save replay: ' + str(e), "RED")
  246. seperator("GREEN")
  247. sys.exit(1)
  248. except KeyboardInterrupt:
  249. seperator("GREEN")
  250. log('[I] The download has been aborted by the user.', "YELLOW")
  251. seperator("GREEN")
  252. try:
  253. shutil.rmtree(output_dir)
  254. except Exception as e:
  255. log("[E] Could not remove temp folder: " + str(e), "RED")
  256. sys.exit(1)
  257. sys.exit(0)
  258. def get_replay_comments(api, broadcast, comments_json_file, dl):
  259. cdl = CommentsDownloader(
  260. api=api, broadcast=broadcast, destination_file=comments_json_file)
  261. cdl.get_replay()
  262. try:
  263. if cdl.comments:
  264. comments_log_file = comments_json_file.replace('.json', '.log')
  265. CommentsDownloader.generate_log(
  266. cdl.comments, broadcast['published_time'], comments_log_file,
  267. comments_delay=0)
  268. if len(cdl.comments) == 1:
  269. log("[I] Successfully saved 1 comment to logfile.", "GREEN")
  270. else:
  271. log("[I] Successfully saved {} comments to logfile.".format(len(cdl.comments)), "GREEN")
  272. else:
  273. log("[I] There are no available comments to save.", "GREEN")
  274. except Exception as e:
  275. log('[E] Could not save comments to logfile: ' + str(e), "RED")
  276. def get_live_comments(api, broadcast, comments_json_file, dl):
  277. cdl = CommentsDownloader(
  278. api=api, broadcast=broadcast, destination_file=comments_json_file)
  279. first_comment_created_at = 0
  280. try:
  281. while not dl.is_aborted:
  282. if 'initial_buffered_duration' not in broadcast and dl.initial_buffered_duration:
  283. broadcast['initial_buffered_duration'] = dl.initial_buffered_duration
  284. cdl.broadcast = broadcast
  285. first_comment_created_at = cdl.get_live(first_comment_created_at)
  286. except ClientError as e:
  287. if not 'media has been deleted' in e.error_response:
  288. log("[W] Comment collection ClientError: %d %s" % (e.code, e.error_response), "YELLOW")
  289. try:
  290. if cdl.comments:
  291. cdl.save()
  292. comments_log_file = comments_json_file.replace('.json', '.log')
  293. CommentsDownloader.generate_log(
  294. cdl.comments, settings.current_time, comments_log_file,
  295. comments_delay=dl.initial_buffered_duration)
  296. if len(cdl.comments) == 1:
  297. log("[I] Successfully saved 1 comment to logfile.", "GREEN")
  298. else:
  299. log("[I] Successfully saved {} comments to logfile.".format(len(cdl.comments)), "GREEN")
  300. seperator("GREEN")
  301. else:
  302. log("[I] There are no available comments to save.", "GREEN")
  303. seperator("GREEN")
  304. except Exception as e:
  305. log('[E] Could not save comments to logfile: ' + str(e), "RED")