def test_downloader(self): dl = replay.Downloader( mpd=MPD_CONTENT, output_dir='output_replay') output_file = 'output_replay.mp4' dl.download(output_file) self.assertTrue(os.path.isfile(output_file), '{0!s} not generated'.format(output_file))
def test_downloader_multiperiods(self): dl = replay.Downloader( mpd=MPD_CONTENT_MULTIPERIODS, output_dir='output_replay_multiperiods') output_file = 'output_replay_multiperiods.mp4' dl.download(output_file) self.assertTrue( os.path.isfile('output_replay_multiperiods-1.mp4'), '{0!s} not generated'.format('output_replay_multiperiods-1.mp4')) self.assertTrue( os.path.isfile('output_replay_multiperiods-2.mp4'), '{0!s} not generated'.format('output_replay_multiperiods-2.mp4'))
def test_downloader_skipffmpeg(self): dl = replay.Downloader( mpd=MPD_CONTENT, output_dir='output_replay_skipffmpeg') output_file = 'output_replay_skipffmpeg.mp4' dl.download(output_file, skipffmpeg=True) self.assertTrue( os.path.isfile('output_replay_skipffmpeg/replay_video.mp4'), 'Temp video file was cleared') self.assertTrue( os.path.isfile('output_replay_skipffmpeg/replay_audio.mp4'), 'Temp audio file was cleared') self.assertFalse(os.path.isfile(output_file), '{0!s} not generated'.format(output_file))
def test_downloader_cleartempfiles(self): dl = replay.Downloader( mpd=MPD_CONTENT, output_dir='output_replay_cleartempfile') output_file = 'output_replay_cleartempfile.mp4' dl.download(output_file, cleartempfiles=False) self.assertTrue( os.path.isfile('output_replay_cleartempfile/replay_video.mp4'), 'Temp video file was cleared') self.assertTrue( os.path.isfile('output_replay_cleartempfile/replay_audio.mp4'), 'Temp audio file was cleared') self.assertTrue(os.path.isfile(output_file), '{0!s} not generated'.format(output_file))
def test_downloader_badffmpeg(self): dl = replay.Downloader( mpd=MPD_CONTENT, output_dir='output_replay_badffmpeg', user_agent=None, ffmpeg_binary='ffmpegbad') output_file = 'output_replay_badffmpeg.mp4' dl.download(output_file, cleartempfiles=True) self.assertTrue( os.path.isfile('output_replay_badffmpeg/replay_video.mp4'), 'Temp video file was cleared') self.assertTrue( os.path.isfile('output_replay_badffmpeg/replay_audio.mp4'), 'Temp audio file was cleared') self.assertFalse(os.path.isfile(output_file), '{0!s} generated'.format(output_file))
def download_replays(broadcasts): try: try: log_info_green('Amount of replays : {:s}'.format( str(len(broadcasts)))) for replay_index, broadcast in enumerate(broadcasts): bc_dash_manifest = parseString(broadcast.get( 'dash_manifest')).getElementsByTagName('Period') bc_duration_raw = bc_dash_manifest[0].getAttribute("duration") bc_hours = (bc_duration_raw.split("PT"))[1].split("H")[0] bc_minutes = (bc_duration_raw.split("H"))[1].split("M")[0] bc_seconds = (( bc_duration_raw.split("M"))[1].split("S")[0]).split('.')[0] log_info_green( 'Replay {:s} duration : {:s} minutes and {:s} seconds'. format(str(replay_index + 1), bc_minutes, bc_seconds)) except Exception as e: log_warn( "An error occurred while getting replay duration information: {:s}" .format(str(e))) log_seperator() log_info_green("Downloading replays... press [CTRL+C] to abort.") log_seperator() for replay_index, broadcast in enumerate(broadcasts): exists = False if sys.version.split(' ')[0].startswith('2'): directories = (os.walk(settings.save_path).next()[1]) else: directories = (os.walk(settings.save_path).__next__()[1]) for directory in directories: if (str(broadcast.get('id')) in directory) and ("_live_" not in directory): log_info_blue( "Already downloaded a replay with ID '{:s}'.".format( str(broadcast.get('id')))) exists = True if not exists: current = replay_index + 1 log_info_green( "Downloading replay {:s} of {:s} with ID '{:s}'...".format( str(current), str(len(broadcasts)), str(broadcast.get('id')))) current_time = str(int(time.time())) output_dir = '{}{}_{}_{}_{}_replay_downloads'.format( settings.save_path, settings.current_date, user_to_download, broadcast.get('id'), settings.current_time) broadcast_downloader = replay.Downloader( mpd=broadcast.get('dash_manifest'), output_dir=output_dir, user_agent=instagram_api.user_agent) open(os.path.join(output_dir, 'folder.lock'), 'a').close() replay_mp4_file = '{}{}_{}_{}_{}_replay.mp4'.format( settings.save_path, settings.current_date, user_to_download, broadcast.get('id'), settings.current_time) replay_json_file = os.path.join( output_dir, '{}_{}_{}_{}_replay_comments.json'.format( settings.current_date, user_to_download, broadcast.get('id'), settings.current_time)) if settings.clear_temp_files.title() == "True": replay_saved = broadcast_downloader.download( replay_mp4_file, cleartempfiles=True) else: replay_saved = broadcast_downloader.download( replay_mp4_file, cleartempfiles=False) if settings.save_comments.title() == "True": log_info_green("Downloading replay comments...") try: get_replay_comments(instagram_api, broadcast, replay_json_file, broadcast_downloader) except Exception as e: log_error( 'An error occurred while downloading comments: {:s}' .format(str(e))) if (len(replay_saved) == 1): log_info_green( "Finished downloading replay {:s} of {:s}.".format( str(current), str(len(broadcasts)))) try: os.remove(os.path.join(output_dir, 'folder.lock')) except Exception: pass if (current != len(broadcasts)): log_seperator() else: log_warn( "No output video file was made, please merge the files manually if possible." ) log_warn( "Check if ffmpeg is available by running ffmpeg in your terminal/cmd prompt." ) log_whiteline() log_seperator() log_info_green("Finished downloading all available replays.") log_seperator() try: os.remove( os.path.join(settings.save_path, user_to_download + '.lock')) except Exception: pass sys.exit(0) except Exception as e: log_error('Could not save replay: {:s}'.format(str(e))) log_seperator() try: os.remove(os.path.join(output_dir, 'folder.lock')) except Exception: pass try: os.remove( os.path.join(settings.save_path, user_to_download + '.lock')) except Exception: pass sys.exit(1) except KeyboardInterrupt: log_seperator() log_info_blue('The download has been aborted by the user, exiting...') log_seperator() try: shutil.rmtree(output_dir) except Exception as e: log_error("Could not remove temp folder: {:s}".format(str(e))) sys.exit(1) try: os.remove( os.path.join(settings.save_path, user_to_download + '.lock')) except Exception: pass sys.exit(0)
def download_replays(): try: try: logger.info('Amount of replays : {:s}'.format( str(len(pil.replays_obj)))) for replay_index, replay_obj in enumerate(pil.replays_obj): bc_dash_manifest = parseString(replay_obj.get( 'dash_manifest')).getElementsByTagName('Period') bc_duration_raw = bc_dash_manifest[0].getAttribute("duration") bc_minutes = (bc_duration_raw.split("H"))[1].split("M")[0] bc_seconds = (( bc_duration_raw.split("M"))[1].split("S")[0]).split('.')[0] logger.info( 'Replay {:s} duration : {:s} minutes and {:s} seconds'. format(str(replay_index + 1), bc_minutes, bc_seconds)) except Exception as e: logger.warn( "An error occurred while getting replay duration information: {:s}" .format(str(e))) logger.separator() logger.info("Downloading replays, press [CTRL+C] to abort.") logger.separator() for replay_index, replay_obj in enumerate(pil.replays_obj): exists = False pil.livestream_obj = replay_obj if Constants.PYTHON_VER[0][0] == '2': directories = (os.walk(pil.dl_path).next()[1]) else: directories = (os.walk(pil.dl_path).__next__()[1]) for directory in directories: if (str(replay_obj.get('id')) in directory) and ("_live_" not in directory): logger.binfo( "Already downloaded a replay with ID '{:s}'.".format( str(replay_obj.get('id')))) exists = True if not exists: current = replay_index + 1 logger.info( "Downloading replay {:s} of {:s} with ID '{:s}'.".format( str(current), str(len(pil.replays_obj)), str(replay_obj.get('id')))) pil.live_folder_path = '{}{}_{}_{}_{}_replay_downloads'.format( pil.dl_path, pil.datetime_compat, pil.dl_user, pil.livestream_obj.get('id'), pil.epochtime) broadcast_downloader = replay.Downloader( mpd=replay_obj.get('dash_manifest'), output_dir=pil.live_folder_path, user_agent=pil.ig_api.user_agent, ffmpeg_binary=pil.ffmpeg_path) if pil.use_locks: helpers.create_lock_folder() replay_mp4_file = '{}{}_{}_{}_{}_replay.mp4'.format( pil.dl_path, pil.datetime_compat, pil.dl_user, pil.livestream_obj.get('id'), pil.epochtime) comments_json_file = '{}{}_{}_{}_{}_replay_comments.json'.format( pil.dl_path, pil.datetime_compat, pil.dl_user, pil.livestream_obj.get('id'), pil.epochtime) pil.comment_thread_worker = threading.Thread( target=get_replay_comments, args=(comments_json_file, )) broadcast_downloader.download( replay_mp4_file, cleartempfiles=pil.clear_temp_files) if pil.clear_temp_files: helpers.remove_temp_folder() if pil.dl_comments: logger.info("Downloading replay comments.") try: get_replay_comments(comments_json_file) except Exception as e: logger.error( 'An error occurred while downloading comments: {:s}' .format(str(e))) logger.info("Finished downloading replay {:s} of {:s}.".format( str(current), str(len(pil.replays_obj)))) helpers.remove_lock() if current != len(pil.replays_obj): logger.separator() logger.separator() logger.info("Finished downloading all available replays.") helpers.remove_lock() except Exception as e: logger.error('Could not save replay: {:s}'.format(str(e))) helpers.remove_lock() except KeyboardInterrupt: logger.separator() logger.binfo('The download has been aborted by the user, exiting.') logger.separator() helpers.remove_temp_folder() helpers.remove_lock()
def download_replays(broadcasts): try: log("[I] Downloading replays... press [CTRL+C] to abort.", "GREEN") seperator("GREEN") for replay_index, broadcast in enumerate(broadcasts): exists = False if sys.version.split(' ')[0].startswith('2'): directories = (os.walk(settings.save_path).next()[1]) else: directories = (os.walk(settings.save_path).__next__()[1]) for directory in directories: if (str(broadcast.get('id')) in directory) and ("_live_" not in directory): log("[W] Already downloaded a replay with ID '{:s}'.".format(str(broadcast.get('id'))), "YELLOW") exists = True if not exists: current = replay_index + 1 log("[I] Downloading replay {:s} of {:s} with ID '{:s}'...".format(str(current), str(len(broadcasts)), str(broadcast.get('id'))), "GREEN") current_time = str(int(time.time())) output_dir = settings.save_path + '{}_{}_{}_{}_replay_downloads'.format(settings.current_date, user_to_record, broadcast.get('id'), settings.current_time) broadcast_downloader = replay.Downloader( mpd=broadcast.get('dash_manifest'), output_dir=output_dir, user_agent=instagram_api.user_agent) replay_mp4_file = settings.save_path + '{}_{}_{}_{}_replay.mp4'.format(settings.current_date, user_to_record, broadcast.get('id'), settings.current_time) replay_json_file = settings.save_path + '{}_{}_{}_{}_replay_comments.json'.format(settings.current_date, user_to_record, broadcast.get('id'), settings.current_time) replay_comments_file = replay_json_file.replace(".json", ".log") replay_files = [replay_mp4_file] if settings.clear_temp_files.title() == "True": replay_saved = broadcast_downloader.download(replay_mp4_file, cleartempfiles=True) else: replay_saved = broadcast_downloader.download(replay_mp4_file, cleartempfiles=False) if settings.save_comments.title() == "True": log("[I] Checking for available comments to save...", "GREEN") if get_replay_comments(instagram_api, broadcast, replay_json_file, broadcast_downloader): replay_files.extend([replay_json_file, replay_comments_file]) if (len(replay_saved) == 1): log("[I] Finished downloading replay {:s} of {:s}.".format(str(current), str(len(broadcasts))), "GREEN") if settings.ftp_enabled: try: upload_ftp_files(replay_files) except Exception as e: log("[E] Could not upload replay files to FTP server: {:s}".format(str(e)), "RED") if (current != len(broadcasts)): seperator("GREEN") else: log("[W] No output video file was made, please merge the files manually if possible.", "YELLOW") log("[W] Check if ffmpeg is available by running ffmpeg in your terminal/cmd prompt.", "YELLOW") log("", "GREEN") seperator("GREEN") log("[I] Finished downloading all available replays.", "GREEN") seperator("GREEN") sys.exit(0) except Exception as e: log('[E] Could not save replay: {:s}'.format(str(e)), "RED") seperator("GREEN") sys.exit(1) except KeyboardInterrupt: seperator("GREEN") log('[I] The download has been aborted by the user.', "YELLOW") seperator("GREEN") try: shutil.rmtree(output_dir) except Exception as e: log("[E] Could not remove temp folder: {:s}".format(str(e)), "RED") sys.exit(1) sys.exit(0)