require 'site_list/video_analyze' require 'requests/request' require 'progressbars/progressbar' require 'file_operats/file_operat_chatdata' require 'json' require 'nokogiri' class Youtubelive_analyze").strip end def chatinfo_script_cleanup(script_date) return script_date.split('window["ytInitialData"] = ',2)[1].strip.chomp("").strip.chomp(";") end def htmlpage_script_parse(respons,callback) script_body=callback.call(respons.to_s) return JSON.parse(script_body) end def chat_body_get(next_url, opt={'User-Agent' => @USER_AGENT}) chat_respons,chat_status=request_html_parse(next_url,opt) chat_respons.search('script').each do |script| if script.to_s.include?("window[\"ytInitialData\"]") then return htmlpage_script_parse(script,method(:chatinfo_script_cleanup)) end end return "" end def chat_scrape(log_flag=true,log_path=@chatlog_filepath,progressbar=true) chat_list=[] chat_count=0 next_path = @videoinfo_body.dig("contents", "twoColumnWatchNextResults", "conversationBar", "liveChatRenderer", "continuations", 0, "reloadContinuationData", "continuation") return chat_list if next_path.nil? next_url=@CHAT_REQUEST_URL + next_path while true do begin chat_body=chat_body_get(next_url) chat_body["continuationContents"]["liveChatContinuation"]["actions"][1..-1].each do |chat| chat_count+=1 chat_list.push chat end progressbar(chat_count,"chat_count_inf") if progressbar next_url=@CHAT_REQUEST_URL + chat_body["continuationContents"]["liveChatContinuation"]["continuations"][0]["liveChatReplayContinuationData"]["continuation"] sleep(1) rescue break end end file_write(chat_list,log_flag,log_path) return chat_list end public :chat_scrape private :videoid_get, :videoinfo_get, :videoinfo_extraction, :htmlpage_script_parse, :videoinfo_script_cleanup, :chatinfo_script_cleanup, :chat_body_get, :videoinfo_script_cleanup_p2 end