Lara19
Lara19

Reputation: 699

Python: fetching live chat but too slow

I had to fetch live chat on Youtube and chose to use pytchat instead of Youtube API. There's no problem to fetch the chat, but it was a bit slow.

from pytchat import LiveChat
from datetime import datetime

chat = LiveChat(video_id = "36YnV9STBqc")
k=[]

while chat.is_alive():
    now2 = datetime.now()
    current_time2 = now2.strftime("%H:%M:%S")
    print("Current Time =", current_time2,"==========")  #A
    try:
        data = chat.get()
        now3 = datetime.now()
        current_time3 = now3.strftime("%H:%M:%S")
        print("Current Time =", current_time3,"~~~~~~~~~~~~~")  #B
        for c in data.items:
            comment=f"[{c.datetime}-{c.message}]"
            k.append(comment)   #I need to use k later
            now = datetime.now()
            current_time = now.strftime("%H:%M:%S")
            print(comment,"Current Time =", current_time,"++++++++++++++++")  
            data.tick()
    except KeyboardInterrupt:
        chat.terminate()
        break

Below shows the output of a video with 17,649 watching: (B to A took 5 secs)

Current Time = 18:49:33 ==========   #A
Current Time = 18:49:33 ~~~~~~~~~~~~~  #B
[2020-05-30 18:49:29-hi] Current Time = 18:49:33 ++++++++++++++++  #4 seconds late
[2020-05-30 18:49:32-how are you] Current Time = 18:49:36 ++++++++++++++++
Current Time = 18:49:38 ==========  #A
Current Time = 18:49:38 ~~~~~~~~~~~~~  #B
[2020-05-30 18:49:32-so good] Current Time = 18:49:38 ++++++++++++++++   #6 seconds late

Below shows the output of a video with 702 watching: (B to A took at least 10 secs)

Current Time = 18:49:09 ==========   #A
Current Time = 18:49:10 ~~~~~~~~~~~~~  #B
[2020-05-30 18:49:06-hellp] Current Time = 18:49:10 ++++++++++++++++
[2020-05-30 18:49:07-love the music] Current Time = 18:49:15 ++++++++++++++++
Current Time = 18:49:20 ==========   #A
Current Time = 18:49:20 ~~~~~~~~~~~~~   #B
[2020-05-30 18:49:15-???] Current Time = 18:49:20 ++++++++++++++++

I assume that different watching amounts will effect the time? It's also 4 to 6 secs late to fetch every chat, is it possible to solve it? Or it's just how Pytchat works?

Upvotes: 0

Views: 798

Answers (2)

Polaroid
Polaroid

Reputation: 1

Use

while chat.is_alive():
  data = chat.get()
  for c in data.sync_items():
    #c can then be formatted for ur stuff
    print("Formatting stuff")

sync_items() will give you appropriate realtime chat movement.

Upvotes: 0

taizan-hokuto
taizan-hokuto

Reputation: 1

This is a specification.

Pytchat gets the chat in exactly the same way as the browser. If your browser displays the time of the chat and the current time down to the second, you'll get the same results.

The response of the YouTube server is presumably affected by the number of people watching the chat and the number of people posting the chat at any given time.

It needs to be verified, but as you pointed out, I'm guessing that if a lot of chat posts are made, it's taking longer for the YouTube server to process them and return the chats that are retrieved.

(If you comment out the data.tick(), you might get a little better results.)

Upvotes: 0

Related Questions