Skip to content

Instantly share code, notes, and snippets.

@chfour
Last active August 28, 2021 20:54
Show Gist options
  • Save chfour/3a307fef98c4d16a691a134c327be52e to your computer and use it in GitHub Desktop.
Save chfour/3a307fef98c4d16a691a134c327be52e to your computer and use it in GitHub Desktop.
yt live chat downloader
import pytchat, argparse
parser = argparse.ArgumentParser()
parser.add_argument("vid_id", type=str,
help="video ID")
parser.add_argument("output", type=str,
help="output file")
args = parser.parse_args()
chat = pytchat.create(video_id=args.vid_id, processor=pytchat.JsonfileArchiver(args.output))
while chat.is_alive():
print(f"{chat.get()['total_lines']} lines so far")
import json, fileinput, sys, traceback
def parse_runs(runs):
content = ""
for run in runs:
if "emoji" in run.keys():
if "shortcuts" in run["emoji"]:
content += run["emoji"]["shortcuts"][0]
else:
content += "{{" + repr(run["emoji"]["emojiId"]) + "}}"
else:
content += run["text"]
return content
try:
for l in fileinput.input():
data = json.loads(l)
chat_data = data["addChatItemAction"]["item"]
if "liveChatTextMessageRenderer" in chat_data:
chat_data = chat_data["liveChatTextMessageRenderer"]
content = parse_runs(chat_data["message"]["runs"])
author = chat_data["authorName"]["simpleText"]
elif "liveChatModeChangeMessageRenderer" in chat_data:
chat_data = chat_data["liveChatModeChangeMessageRenderer"]
content = parse_runs(chat_data["text"]["runs"]) + " | " + parse_runs(chat_data["subtext"]["runs"])
author = "* SYSTEM *"
timestamp = chat_data["timestampText"]["simpleText"]
print(f"{timestamp} <{author}> {content}")
except Exception as e:
print(traceback.format_exc(), file=sys.stderr)
print(chat_data, file=sys.stderr)
@chfour
Copy link
Author

chfour commented Aug 28, 2021

alright, I updated the json2txt script, should work now (failed on some emoji, possibly the transgender flag in some cases)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment