def workload(vid_id): data = [] vid_path = ROOT.joinpath(vid_id + ".json") if vid_path.exists(): logger.critical("File already exists for stream {}", vid_id) return logger.debug("task {} started", vid_id) def callback(chat_data: Chatdata): logger.debug("Processing Data") for chat in chat_data.items: chat: Chat json_data: dict = json.loads(chat.json()) logger.debug(f"S:[{json_data['author']['name']}][{json_data['timestamp']}][{json_data['message']}]") data.append(json_data) try: # live_chat = LiveChatAsync(vid_id, callback=callback) live_chat = LiveChat(vid_id, callback=callback, force_replay=True) except Exception: raise else: while live_chat.is_alive(): time.sleep(3) try: live_chat.raise_for_status() except ChatDataFinished: logger.info("Chat data finished.") except Exception: raise if data: new_dict = {idx: chat_json for idx, chat_json in enumerate(data)} vid_path.write_text(json.dumps(new_dict, indent=2), encoding="utf8") logger.info("Written {} chats for {}", len(data), vid_id) else: logger.warning("No data to write")
from pytchat import LiveChat import time import json import pprint import pathlib # chat_id = "LLw58K_VVwE" chat_id = input("Stream ID:") members = set() def callback(chat_data): for chat in chat_data.items: json_data: dict = json.loads(chat.json()) members.add(json_data["author"]["name"]) pprint.pprint(members) livechat = LiveChat(chat_id, callback=callback, direct_mode=True) while livechat.is_alive(): time.sleep(3) livechat.raise_for_status() if members: save_file = pathlib.Path(__file__).parent.joinpath(chat_id + ".json") save_file.write_text(json.dumps(members, indent=2), "utf8")