Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

PytchatCore

taizan-hokuto edited this page Apr 25, 2021 · 15 revisions
  • Simple non-buffered object for fetching live chat.

Get with pytchat.create() function.

Example code

import pytchat
import time

chat = pytchat.create(video_id="uIx8l2xlYVY")

while chat.is_alive():
    chatdata = chat.get()
    print(chatdata.json())
    time.sleep(5)

try:
    chat.raise_for_status()
except pytchat.ChatdataFinished:
    print("chat data finished")
except Exception as e:
    print(type(e), str(e))

Display chats at appropriate intervals.

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}")

constructor params

name type required remarks default value
video_id str * ID of youtube video, or youtube URL that includes ID. -
processor ChatProcessor DefaultProcessor
interruptable bool Allows keyboard interrupts. Set this parameter to False if your own threading program causes the problem. True
seektime int start position of fetching chat (seconds). This option is valid for archived chat only. If negative value, fetches chatdata which is posted before start broadcasting. This parameter is not working well. We'll deal with it as soon as possible. 0
force_replay bool force to fetch archived chat data, even if specified video is live. False
topchat_only bool If True, get only top chat. False
hold_exception bool If True, when exceptions occur, the exception is held internally, and can be raised by raise_for_status(). True
logger logging.Logger any Logger object internal logger(set NullHandler)
replay_continuation str continuation parameter(archived chat only) None

continuation

The continuation parameter of recent chat data.
This parameter can be used for retrieving chat data of any timing by specifying in the constructor as replay_continuation.
(This parameter is valid only archived chat data.)

Example code

import pytchat
import time
stream = pytchat.create(video_id = "uIx8l2xlYVY")
i = 0
while stream.is_alive():
  data = stream.get()
  items = data.items
  for c in items:
      print(f"{c.datetime} [{c.author.name}]- {c.message}")
  time.sleep(3)
  i += 1
  if i == 3:
    # get the continuation parameter
    continuation = stream.continuation
    stream.terminate()
    break

# retrieve chatdata from the continuation.
stream = pytchat.create(video_id = "uIx8l2xlYVY", replay_continuation=continuation)
data = stream.get()
items = data.items
for c in items:
    print(f"{c.datetime} [{c.author.name}]- {c.message}")
stream.terminate()

get()

description return value
Get processed chat data. processed chat data

is_alive()

description return value
Check if livechat stream is alive. bool

is_replay()

description return value
Check if replaying archived chat. bool

terminate()

description return value
Terminate fetching chat. -

raise_for_status()

Raise internal exception after is_alive() becomes False.

By this function, you can check the reason for the termination.

*This function is valid only when hold_exception option is True.