YouTube live and premiere comments, delivered fresh and daily!

YouTube live and premiere comments, delivered fresh and daily!

As a content creator you may be posting videos to premier or publish videos live. When doing so, would you like a record of the chat? This would allow you to analyze content and look for positive or negative reactions. If this interests you then pytchat is going to be a big help.

First, the docs provide minimal help, the code they provide either gives you raw json format *****puke***** or requires you to wait eons to see the data. Just try this on any video:

import pytchat
chat = pytchat.create(video_id="uIx8l2xlYVY")
while chat.is_alive():
    for c in chat.get().sync_items():
        print(f"{c.datetime} [{c.author.name}]- {c.message}")

I’ll wait…and so will you.

Let’s take a step back though. Before we proceed how do we get the fundamental item necessary for this type of comment encoding? We need the video ID. Actually, it is really easy to grab.

Go to any video you wish to grab the comments from and find the share button. This is next to the “thumbs up” or “thumbs down” buttons.

Here the last part of the address is your video ID for the rest of the pytchat code.

Now that we have this we will want to work in the json format. It will generate results a lot faster, and give us more flexibility. So start your code with the following:

import pytchat
import json

Now we will set up a few simple loops to get what we want. Loop #1: a raw output of the data.

f=open('q3alUrfh6BI.txt', 'w')
chat = pytchat.create(video_id="q3alUrfh6BI")
while chat.is_alive():
    for c in chat.get().items:
        obj = c.json()
        obj2 = json.loads(obj)
        f.write(str("{}: {}".format(obj2['author']['name'],obj2['message']).encode('utf8')))
        f.write("\n")
f.close()

First, open a file that you want to write the data to. This will be a standard python function so I won’t cover it here, but it is the first line in the code above. Then we call to the pytchat to get our video data with “chat”, and activate this for encoding. Looping through this we need to set the obj to c.json() so we can pass this to json.loads(), otherwise you will get an error message. Then we write this using standard json and .write() syntax.

The output? It’s very nice, ignore the “\xe2\x9d\xa4\xef\xb8\x8f\xf0\x9f\x91\xbb” items, these are emojis. We encoded this for utf-8 (necessary for the .txt format). With this encoding the emojis are not their usual happy selves.

What if you want to search the comments? Well, I have just the thing for you.

chat = pytchat.create(video_id='q3alUrfh6BI')
while chat.is_alive():
    for c in chat.get().items:
        if 'enter any text here' in c.json():
            obj = c.json()
            obj2 = json.loads(obj)
            print(obj2['author']['name']+': '+obj2['message'])

Here the only difference is we are printing the name of the user and message based on the “IN” statement being true. In the “enter any text here” you can look for users or specific message text. This will give you a clean output where you can analyze the comments more thoroughly.

Notice on line 4 we search for “lmao”. This provide any output with that in the text. Now lets look for a user named “Mogollon Monster”.

We put this search criteria in the same area. We are not searching specific sub-content, but using the json output as a string. This allows use to use the “IN” function effectively to search for any term name, date, or other metadata encoded in the comment.

I hope you found this helpful! Post comments or suggestions for future content!

Comments are closed.