Skip to main content

a python library for fetching youtube live chat.

Project description

pytchat

pytchat is a python library for fetching youtube live chat.

Description

pytchat is a python library for fetching youtube live chat without using youtube api, Selenium or BeautifulSoup.

Other features:

  • Customizable chat data processors including youtube api compatible one.
  • Available on asyncio context.
  • Quick fetching of initial chat data by generating continuation params instead of web scraping.

For more detailed information, see wiki.

Install

pip install pytchat

Demo

demo

Examples

on-demand mode

from pytchat import LiveChat

chat = LiveChat("rsHWP7IjMiw")
while chat.is_alive():
  data = chat.get()
  for c in data.items:
    print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
    data.tick()

callback mode

from pytchat import LiveChat
import time

#callback function is automatically called.
def display(data):
  for c in data.items:
    print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
    data.tick()

if __name__ == '__main__':
  chat = LiveChat("rsHWP7IjMiw", callback = display)
  while chat.is_alive():
    #other background operation.
    time.sleep(3)

asyncio context:

from pytchat import LiveChatAsync
from concurrent.futures import CancelledError
import asyncio

async def main():
  chat = LiveChatAsync("rsHWP7IjMiw", callback = func)
  while chat.is_alive():
    #other background operation.
    await asyncio.sleep(3)

#callback function is automatically called.
async def func(data):
  for c in data.items:
    print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
    await data.tick_async()

if __name__ == '__main__':
  try:
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
  except CancelledError:
    pass

youtube api compatible processor:

from pytchat import LiveChat, CompatibleProcessor
import time

chat = LiveChat("rsHWP7IjMiw", 
  processor = CompatibleProcessor() )

while chat.is_alive():
  data = chat.get()
  polling = data['pollingIntervalMillis']/1000
  for c in data['items']:
    if c.get('snippet'):
      print(f"[{c['authorDetails']['displayName']}]"
            f"-{c['snippet']['displayMessage']}")
      time.sleep(polling/len(data['items']))

replay:

If specified video is not live, automatically try to fetch archived chat data.

from pytchat import LiveChat

def main():
  #seektime (seconds): start position of chat.
  chat = ReplayChat("ojes5ULOqhc", seektime = 60*30)
  while chat.is_alive():
    data = chat.get()
    for c in data.items:
      print(f"{c.elapsedTime} [{c.author.name}]-{c.message} {c.amountString}")
      data.tick()

if __name__ == '__main__':
  main()

Structure of Default Processor

Each item can be got with items() function.

name type remarks
type str "superChat","textMessage","superSticker","newSponsor"
id str
message str emojis are represented by ":(shortcut text):"
messageEx str list of message texts and emoji URLs.
timestamp int unixtime milliseconds
datetime str e.g. "2019-10-10 12:34:56"
elapsedTime str elapsed time. (e.g. "1:02:27") *Replay Only.
amountValue float e.g. 1,234.0
amountString str e.g. "$ 1,234"
currency str ISO 4217 currency codes (e.g. "USD")
bgColor int RGB Int
author object see below

Structure of author object.

name type remarks
name str
channelId str *chatter's channel ID.
channelUrl str
imageUrl str
badgeUrl str
isVerified bool
isChatOwner bool
isChatSponsor bool
isChatModerator bool

Licence

MIT License

Author

taizan-hokuto

twitter:@taizan205

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytchat-0.0.4.4.tar.gz (31.3 kB view details)

Uploaded Source

File details

Details for the file pytchat-0.0.4.4.tar.gz.

File metadata

  • Download URL: pytchat-0.0.4.4.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.4

File hashes

Hashes for pytchat-0.0.4.4.tar.gz
Algorithm Hash digest
SHA256 7ea1617566096b813271c4bb32ce67534d28face38ff73a537c93ea35c1d1b5c
MD5 a91ee4bacd7cd7e65c258402b0c6a1d1
BLAKE2b-256 8fc10d0fff3d7b63147af132834ae1cb76a286f3ee0c152e654913b77a2a7abb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page