def get_replay_comments(comments_json_file): try: comments_downloader = CommentsDownloader(destination_file=comments_json_file) comments_downloader.get_replay() try: if comments_downloader.comments: comments_log_file = comments_json_file.replace('.json', '.log') comment_errors, total_comments = CommentsDownloader.generate_log( comments_downloader.comments, pil.livestream_obj.get('published_time'), comments_log_file, comments_delay=0) if total_comments == 1: logger.info("Successfully saved 1 comment to logfile.") os.remove(comments_json_file) logger.separator() return True else: if comment_errors: logger.warn( "Successfully saved {:s} comments but {:s} comments are (partially) missing.".format( str(total_comments), str(comment_errors))) else: logger.info("Successfully saved {:s} comments.".format(str(total_comments))) os.remove(comments_json_file) logger.separator() return True else: logger.info("There are no available comments to save.") return False except Exception as e: logger.error('Could not save comments to logfile: {:s}'.format(str(e))) return False except KeyboardInterrupt as e: logger.binfo("Downloading replay comments has been aborted.") return False
def get_live_comments(comments_json_file): try: comments_downloader = CommentsDownloader( destination_file=comments_json_file) first_comment_created_at = 0 try: while not pil.broadcast_downloader.is_aborted: if 'initial_buffered_duration' not in pil.livestream_obj and pil.broadcast_downloader.initial_buffered_duration: pil.livestream_obj[ 'initial_buffered_duration'] = pil.broadcast_downloader.initial_buffered_duration comments_downloader.broadcast = pil.livestream_obj first_comment_created_at = comments_downloader.get_live( first_comment_created_at) except ClientError as e: if not 'media has been deleted' in e.error_response: logger.warn("Comment collection ClientError: %d %s" % (e.code, e.error_response)) try: if comments_downloader.comments: comments_downloader.save() comments_log_file = comments_json_file.replace('.json', '.log') comment_errors, total_comments = CommentsDownloader.generate_log( comments_downloader.comments, pil.epochtime, comments_log_file, comments_delay=pil.broadcast_downloader. initial_buffered_duration) if len(comments_downloader.comments) == 1: logger.info("Successfully saved 1 comment.") os.remove(comments_json_file) logger.separator() return True else: if comment_errors: logger.warn( "Successfully saved {:s} comments but {:s} comments are (partially) missing." .format(str(total_comments), str(comment_errors))) else: logger.info("Successfully saved {:s} comments.".format( str(total_comments))) os.remove(comments_json_file) logger.separator() return True else: logger.info("There are no available comments to save.") logger.separator() return False except Exception as e: logger.error('Could not save comments: {:s}'.format(str(e))) return False except KeyboardInterrupt as e: logger.binfo("Downloading livestream comments has been aborted.") return False
def validate_inputs(config, args, unknown_args): error_arr = [] banner_shown = False try: if args.configpath: if os.path.isfile(args.configpath): pil.config_path = args.configpath else: logger.banner() banner_shown = True logger.warn("Custom config path is invalid, falling back to default path: {:s}".format(pil.config_path)) pil.config_path = os.path.join(os.getcwd(), "pyinstalive.ini") logger.separator() if not os.path.isfile(pil.config_path): # Create new config if it doesn't exist if not banner_shown: logger.banner() helpers.new_config() return False pil.config_path = os.path.realpath(pil.config_path) config.read(pil.config_path) if args.download: pil.dl_user = args.download if args.downloadfollowing or args.batchfile: logger.banner() logger.warn("Please use only one download method. Use -h for more information.") logger.separator() return False elif not args.clean and not args.info and not args.assemble and not args.downloadfollowing and not args.batchfile and not args.organize and not args.generatecomments: logger.banner() logger.error("Please use a download method. Use -h for more information.") logger.separator() return False if helpers.bool_str_parse(config.get('pyinstalive', 'log_to_file')) == "Invalid": pil.log_to_file = True error_arr.append(['log_to_file', 'True']) elif helpers.bool_str_parse(config.get('pyinstalive', 'log_to_file')): pil.log_to_file = True else: pil.log_to_file = False logger.banner() if args.batchfile: if os.path.isfile(args.batchfile): pil.dl_batchusers = [user.rstrip('\n') for user in open(args.batchfile)] if not pil.dl_batchusers: logger.error("The specified file is empty.") logger.separator() return False else: logger.info("Downloading {:d} users from batch file.".format(len(pil.dl_batchusers))) logger.separator() else: logger.error('The specified file does not exist.') logger.separator() return False if unknown_args: pil.uargs = unknown_args logger.warn("The following unknown argument(s) were provided and will be ignored: ") logger.warn(' ' + ' '.join(unknown_args)) logger.separator() pil.ig_user = config.get('pyinstalive', 'username') pil.ig_pass = config.get('pyinstalive', 'password') pil.dl_path = config.get('pyinstalive', 'download_path') pil.run_at_start = config.get('pyinstalive', 'run_at_start') pil.run_at_finish = config.get('pyinstalive', 'run_at_finish') pil.ffmpeg_path = config.get('pyinstalive', 'ffmpeg_path') pil.skip_merge = config.get('pyinstalive', 'skip_merge') pil.args = args pil.config = config pil.proxy = config.get('pyinstalive', 'proxy') if args.dlpath: pil.dl_path = args.dlpath if helpers.bool_str_parse(config.get('pyinstalive', 'show_cookie_expiry')) == "Invalid": pil.show_cookie_expiry = False error_arr.append(['show_cookie_expiry', 'False']) elif helpers.bool_str_parse(config.get('pyinstalive', 'show_cookie_expiry')): pil.show_cookie_expiry = True else: pil.show_cookie_expiry = False if helpers.bool_str_parse(config.get('pyinstalive', 'skip_merge')) == "Invalid": pil.skip_merge = False error_arr.append(['skip_merge', 'False']) elif helpers.bool_str_parse(config.get('pyinstalive', 'skip_merge')): pil.skip_merge = True else: pil.skip_merge = False if helpers.bool_str_parse(config.get('pyinstalive', 'use_locks')) == "Invalid": pil.use_locks = False error_arr.append(['use_locks', 'False']) elif helpers.bool_str_parse(config.get('pyinstalive', 'use_locks')): pil.use_locks = True else: pil.use_locks = False if helpers.bool_str_parse(config.get('pyinstalive', 'clear_temp_files')) == "Invalid": pil.clear_temp_files = False error_arr.append(['clear_temp_files', 'False']) elif helpers.bool_str_parse(config.get('pyinstalive', 'clear_temp_files')): pil.clear_temp_files = True else: pil.clear_temp_files = False if helpers.bool_str_parse(config.get('pyinstalive', 'do_heartbeat')) == "Invalid": pil.do_heartbeat = True error_arr.append(['do_heartbeat', 'True']) if helpers.bool_str_parse(config.get('pyinstalive', 'do_heartbeat')): pil.do_heartbeat = True if args.noheartbeat or not helpers.bool_str_parse(config.get('pyinstalive', 'do_heartbeat')): pil.do_heartbeat = False logger.warn("Getting livestream heartbeat is disabled, this may cause degraded performance.") logger.separator() if not args.nolives and helpers.bool_str_parse(config.get('pyinstalive', 'download_lives')) == "Invalid": pil.dl_lives = True error_arr.append(['download_lives', 'True']) elif helpers.bool_str_parse(config.get('pyinstalive', 'download_lives')): pil.dl_lives = True else: pil.dl_lives = False if not args.noreplays and helpers.bool_str_parse(config.get('pyinstalive', 'download_replays')) == "Invalid": pil.dl_replays = True error_arr.append(['download_replays', 'True']) elif helpers.bool_str_parse(config.get('pyinstalive', 'download_replays')): pil.dl_replays = True else: pil.dl_replays = False if helpers.bool_str_parse(config.get('pyinstalive', 'download_comments')) == "Invalid": pil.dl_comments = True error_arr.append(['download_comments', 'True']) elif helpers.bool_str_parse(config.get('pyinstalive', 'download_comments')): pil.dl_comments = True else: pil.dl_comments = False if args.nolives: pil.dl_lives = False if args.noreplays: pil.dl_replays = False if args.skip_merge: pil.skip_merge = True if not pil.dl_lives and not pil.dl_replays: logger.error("You have disabled both livestream and replay downloading.") logger.error("Please enable at least one of them and try again.") logger.separator() return False if pil.ffmpeg_path: if not os.path.isfile(pil.ffmpeg_path): pil.ffmpeg_path = None cmd = "where" if platform.system() == "Windows" else "which" logger.warn("Custom FFmpeg binary path is invalid, falling back to environment variable.") else: logger.binfo("Overriding FFmpeg binary path: {:s}".format(pil.ffmpeg_path)) else: if not helpers.command_exists('ffmpeg') and not args.info: logger.error("FFmpeg framework not found, exiting.") logger.separator() return False if not pil.ig_user or not len(pil.ig_user): raise Exception("Invalid value for 'username'. This value is required.") if not pil.ig_pass or not len(pil.ig_pass): raise Exception("Invalid value for 'password'. This value is required.") if not pil.dl_path.endswith('/'): pil.dl_path = pil.dl_path + '/' if not pil.dl_path or not os.path.exists(pil.dl_path): pil.dl_path = os.getcwd() + "/" if not args.dlpath: error_arr.append(['download_path', os.getcwd() + "/"]) else: logger.warn("Custom config path is invalid, falling back to default path: {:s}".format(pil.dl_path)) logger.separator() if pil.proxy and pil.proxy != '': parsed_url = urlparse(pil.proxy) if not parsed_url.netloc or not parsed_url.scheme: error_arr.append(['proxy', 'None']) pil.proxy = None if error_arr: for error in error_arr: logger.warn("Invalid value for '{:s}'. Using default value: {:s}".format(error[0], error[1])) logger.separator() if args.info: helpers.show_info() return False elif args.clean: helpers.clean_download_dir() return False elif args.assemble: pil.assemble_arg = args.assemble assembler.assemble() return False elif args.generatecomments: pil.gencomments_arg = args.generatecomments CommentsDownloader.generate_log(gen_from_arg=True) return False elif args.organize: organize.organize_files() return False return True except Exception as e: logger.error("An error occurred: {:s}".format(str(e))) logger.error("Make sure the config file and given arguments are valid and try again.") logger.separator() return False