pytchat
=======
pytchat is a python library for fetching youtube live chat.
## Description
pytchat is a python library for fetching youtube live chat
without using Selenium or BeautifulSoup.
Other features:
+ Customizable [chat data processors](https://github.com/taizan-hokuto/pytchat/wiki/ChatProcessor) 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](https://github.com/taizan-hokuto/pytchat/wiki).
[JP wiki](https://github.com/taizan-hokuto/pytchat/wiki/Home_jp)
## Install
```python
pip install pytchat
```
## Examples
### CLI
One-liner command.
Save chat data to html with embedded custom emojis.
Show chat stream (--echo option) .
```bash
$ pytchat -v https://www.youtube.com/watch?v=ZJ6Q4U_Vg6s -o "c:/temp/"
# options:
# -v : Video ID or URL that includes ID
# -o : output directory (default path: './')
# --echo : Show chats.
# saved filename is [video_id].html
```
### on-demand mode with simple non-buffered.
```python
import pytchat
chat = pytchat.create(video_id="Zvp1pJpie4I")
while chat.is_alive():
chatdata = chat.get()
for c in chatdata.sync_items():
print(f"{c.datetime} [{c.author.name}]- {c.message}")
```
### callback mode with bufferd.
```python
from pytchat import LiveChat
import time
def main():
chat = LiveChat(video_id = "Zvp1pJpie4I", callback = disp)
while livechat.is_alive():
#other background operation.
time.sleep(1)
chat.terminate()
#callback function (automatically called)
def disp(chatdata):
for c in chatdata.sync_items():
print(f"{c.datetime} [{c.author.name}]- {c.message}")
if __name__ == '__main__':
main()
```
### asyncio context:
```python
from pytchat import LiveChatAsync
from concurrent.futures import CancelledError
import asyncio
async def main():
livechat = LiveChatAsync("Zvp1pJpie4I", callback = func)
while livechat.is_alive():
#other background operation.
await asyncio.sleep(3)
#callback function is automatically called.
async def func(chatdata):
async for c in chatdata.async_items():
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
if __name__ == '__main__':
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
except CancelledError:
pass
```
### youtube api compatible processor:
```python
from pytchat import LiveChat, CompatibleProcessor
import time
chat = LiveChat("Zvp1pJpie4I",
processor = CompatibleProcessor() )
while chat.is_alive():
try:
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']))
except KeyboardInterrupt:
chat.terminate()
```
### replay:
If specified video is not live,
automatically try to fetch archived chat data.
```python
import pytchat
chat = pytchat.get("ojes5ULOqhc", seektime = 60*30)
while chat.is_alive():
chatdata = chat.get()
for c in chatdata.sync_items():
print(f"{c.datetime} [{c.author.name}]- {c.message}")
```
### Extract archived chat data as [HTML](https://github.com/taizan-hokuto/pytchat/wiki/HTMLArchiver) or [tab separated values](https://github.com/taizan-hokuto/pytchat/wiki/TSVArchiver).
```python
from pytchat import HTMLArchiver, Extractor
video_id = "*******"
ex = Extractor(
video_id,
div=10,
processor=HTMLArchiver("c:/test.html")
)
ex.extract()
print("finished.")
```
## Structure of Default Processor
Each item can be got with `sync_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 dicts(id, txt, url). |
| 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
[](LICENSE)
## Contributes
Great thanks:
Most of source code of CLI refer to:
[PetterKraabol / Twitch-Chat-Downloader](https://github.com/PetterKraabol/Twitch-Chat-Downloader)
Progress bar in CLI is based on:
[vladignatyev/progress.py](https://gist.github.com/vladignatyev/06860ec2040cb497f0f3)
## Author
[taizan-hokuto](https://github.com/taizan-hokuto)
[twitter:@taizan205](https://twitter.com/taizan205)