downloader.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import sys
  2. import time
  3. import os
  4. import shutil
  5. from instagram_private_api_extensions import live, replay
  6. from .logger import log, seperator
  7. class NoLivestreamException(Exception):
  8. pass
  9. class NoReplayException(Exception):
  10. pass
  11. def main(api_arg, record_arg, save_path_arg):
  12. global api
  13. global record
  14. global save_path
  15. global current_date
  16. global current_time
  17. global broadcast
  18. global mpd_url
  19. current_time = str(int(time.time()))
  20. current_date = time.strftime("%Y%m%d")
  21. api = api_arg
  22. record = record_arg
  23. save_path = save_path_arg
  24. get_user_info(record)
  25. def record_stream(broadcast):
  26. try:
  27. def print_status():
  28. heartbeat_info = api.broadcast_heartbeat_and_viewercount(broadcast['id'])
  29. viewers = broadcast.get('viewer_count', 0)
  30. started_mins, started_secs = divmod((int(time.time()) - broadcast['published_time']), 60)
  31. started_label = '%d minutes' % started_mins
  32. if started_secs:
  33. started_label += ' and %d seconds' % started_secs
  34. log('[I] Viewers : ' + str(int(viewers)) + " watching", "GREEN")
  35. log('[I] Airing time : ' + started_label, "GREEN")
  36. log('[I] Status : ' + heartbeat_info['broadcast_status'].title(), "GREEN")
  37. seperator("GREEN")
  38. return heartbeat_info['broadcast_status'] not in ['active', 'interrupted']
  39. mpd_url = (broadcast.get('dash_manifest')
  40. or broadcast.get('dash_abr_playback_url')
  41. or broadcast['dash_playback_url'])
  42. output_dir = save_path + '{}_{}_{}_{}_live_downloads'.format(current_date, record, broadcast['id'], current_time)
  43. dl = live.Downloader(
  44. mpd=mpd_url,
  45. output_dir=output_dir,
  46. user_agent=api.user_agent,
  47. max_connection_error_retry=3,
  48. duplicate_etag_retry=30,
  49. callback_check=print_status,
  50. mpd_download_timeout=5,
  51. download_timeout=10)
  52. except Exception as e:
  53. log('[E] Could not start recording livestream: ' + str(e), "RED")
  54. seperator("GREEN")
  55. sys.exit(1)
  56. try:
  57. log('[I] Starting livestream recording:', "GREEN")
  58. log('[I] Username : ' + record, "GREEN")
  59. log('[I] MPD URL : ' + mpd_url, "GREEN")
  60. print_status()
  61. log('[I] Recording livestream... press [CTRL+C] to abort.', "GREEN")
  62. dl.run()
  63. stitch_video(dl, broadcast)
  64. except KeyboardInterrupt:
  65. log("", "GREEN")
  66. log('[W] Download has been aborted.', "YELLOW")
  67. log("", "GREEN")
  68. if not dl.is_aborted:
  69. dl.stop()
  70. stitch_video(dl, broadcast)
  71. def stitch_video(dl, broadcast):
  72. log('[I] Stitching downloaded files into video...', "GREEN")
  73. output_file = save_path + '{}_{}_{}_{}_live.mp4'.format(current_date, record, broadcast['id'], current_time)
  74. try:
  75. dl.stitch(output_file, cleartempfiles=False)
  76. log('[I] Successfully stitched downloaded files.', "GREEN")
  77. seperator("GREEN")
  78. sys.exit(0)
  79. except Exception as e:
  80. log('[E] Could not stitch downloaded files: ' + str(e), "RED")
  81. seperator("GREEN")
  82. sys.exit(1)
  83. def get_user_info(record):
  84. try:
  85. log('[I] Getting required user info for user "' + record + '"...', "GREEN")
  86. user_res = api.username_info(record)
  87. user_id = user_res['user']['pk']
  88. except Exception as e:
  89. log('[E] Could not get user info: ' + str(e), "RED")
  90. seperator("GREEN")
  91. sys.exit(1)
  92. get_livestreams(user_id)
  93. get_replays(user_id)
  94. def get_livestreams(user_id):
  95. try:
  96. log('[I] Checking for ongoing livestreams...', "GREEN")
  97. broadcast = api.user_broadcast(user_id)
  98. if (broadcast is None):
  99. raise NoLivestreamException('There are no livestreams available.')
  100. else:
  101. record_stream(broadcast)
  102. except NoLivestreamException as e:
  103. log('[W] ' + str(e), "YELLOW")
  104. except Exception as e:
  105. if (e.__class__.__name__ is not NoLivestreamException):
  106. log('[E] Could not get livestreams info: ' + str(e), "RED")
  107. seperator("GREEN")
  108. sys.exit(1)
  109. def get_replays(user_id):
  110. try:
  111. log('[I] Checking for available replays...', "GREEN")
  112. user_story_feed = api.user_story_feed(user_id)
  113. broadcasts = user_story_feed.get('post_live_item', {}).get('broadcasts', [])
  114. except Exception as e:
  115. log('[E] Could not get replay info: ' + str(e), "RED")
  116. seperator("GREEN")
  117. sys.exit(1)
  118. try:
  119. if (len(broadcasts) == 0):
  120. raise NoReplayException('There are no replays available.')
  121. else:
  122. log("[I] Available replays have been found to download, press [CTRL+C] to abort.", "GREEN")
  123. log("", "GREEN")
  124. for index, broadcast in enumerate(broadcasts):
  125. exists = False
  126. for directory in (os.walk(save_path).next()[1]):
  127. if (str(broadcast['id']) in directory) and ("_live_" not in directory):
  128. log("[W] Already downloaded a replay with ID '" + str(broadcast['id']) + "', skipping...", "GREEN")
  129. exists = True
  130. if exists is False:
  131. current = index + 1
  132. log("[I] Downloading replay " + str(current) + " of " + str(len(broadcasts)) + " with ID '" + str(broadcast['id']) + "'...", "GREEN")
  133. current_time = str(int(time.time()))
  134. output_dir = save_path + '{}_{}_{}_{}_replay_downloads'.format(current_date, record, broadcast['id'], current_time)
  135. dl = replay.Downloader(
  136. mpd=broadcast['dash_manifest'],
  137. output_dir=output_dir,
  138. user_agent=api.user_agent)
  139. replay_saved = dl.download(save_path + '{}_{}_{}_{}_replay.mp4'.format(current_date, record, broadcast['id'], current_time), cleartempfiles=False)
  140. if (len(replay_saved) == 1):
  141. log("[I] Finished downloading replay " + str(current) + " of " + str(len(broadcasts)) + ".", "GREEN")
  142. log("", "GREEN")
  143. else:
  144. log("[W] No output video file was made, please merge the files manually.", "RED")
  145. log("[W] Check if ffmpeg is available by running ffmpeg in your terminal.", "RED")
  146. log("", "GREEN")
  147. log("[I] Finished downloading available replays.", "GREEN")
  148. seperator("GREEN")
  149. sys.exit(0)
  150. except NoReplayException as e:
  151. log('[W] ' + str(e), "YELLOW")
  152. seperator("GREEN")
  153. sys.exit(0)
  154. except Exception as e:
  155. log('[E] Could not save replay: ' + str(e), "RED")
  156. seperator("GREEN")
  157. sys.exit(1)
  158. except KeyboardInterrupt:
  159. log("", "GREEN")
  160. log('[W] Download has been aborted.', "YELLOW")
  161. try:
  162. shutil.rmtree(output_dir)
  163. except Exception as e:
  164. log("[E] Could not remove temp folder: " + str(e), "RED")
  165. sys.exit(1)
  166. sys.exit(0)