Update README

This commit is contained in:
taizan-hokuto
2020-01-13 13:09:51 +09:00
parent 260a2b35a9
commit 0272319fa6

View File

@@ -7,13 +7,16 @@ pytchat is a python library for fetching youtube live chat.
pytchat is a python library for fetching youtube live chat pytchat is a python library for fetching youtube live chat
without using youtube api, Selenium or BeautifulSoup. without using youtube api, Selenium or BeautifulSoup.
pytchatはAPIを使わずにYouTubeチャットを取得するための軽量pythonライブラリです。
Other features: Other features:
+ Customizable chat data processors including youtube api compatible one. + Customizable chat data processors including youtube api compatible one.
+ Available on asyncio context. + Available on asyncio context.
+ Quick fetching of initial chat data by generating continuation params + Quick fetching of initial chat data by generating continuation params
instead of web scraping. instead of web scraping.
For more detailed information, see [wiki](https://github.com/taizan-hokuto/pytchat/wiki). For more detailed information, see [wiki](https://github.com/taizan-hokuto/pytchat/wiki). <br>
より詳細な解説は[wiki](https://github.com/taizan-hokuto/pytchat/wiki/Home-:)を参照してください。
## Install ## Install
```python ```python
@@ -26,13 +29,17 @@ pip install pytchat
### on-demand mode ### on-demand mode
```python ```python
from pytchat import LiveChat from pytchat import LiveChat
livechat = LiveChat(video_id = "Zvp1pJpie4I")
chat = LiveChat("DSGyEsJ17cI") while livechat.is_alive():
while chat.is_alive(): try:
data = chat.get() chatdata = livechat.get()
for c in data.items: for c in chatdata.items:
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}") print(f"{c.datetime} [{c.author.name}]- {c.message}")
data.tick() chatdata.tick()
except KeyboardInterrupt:
livechat.terminate()
break
``` ```
### callback mode ### callback mode
@@ -40,17 +47,21 @@ while chat.is_alive():
from pytchat import LiveChat from pytchat import LiveChat
import time import time
#callback function is automatically called. def main():
def display(data): livechat = LiveChat(video_id = "Zvp1pJpie4I", callback = disp)
for c in data.items: while livechat.is_alive():
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}") #other background operation.
data.tick() time.sleep(1)
livechat.terminate()
#callback function (automatically called)
def disp(chatdata):
for c in chatdata.items:
print(f"{c.datetime} [{c.author.name}]- {c.message}")
chatdata.tick()
if __name__ == '__main__': if __name__ == '__main__':
chat = LiveChat("DSGyEsJ17cI", callback = display) main()
while chat.is_alive():
#other background operation.
time.sleep(3)
``` ```
@@ -61,16 +72,16 @@ from concurrent.futures import CancelledError
import asyncio import asyncio
async def main(): async def main():
chat = LiveChatAsync("DSGyEsJ17cI", callback = func) livechat = LiveChatAsync("Zvp1pJpie4I", callback = func)
while chat.is_alive(): while livechat.is_alive():
#other background operation. #other background operation.
await asyncio.sleep(3) await asyncio.sleep(3)
#callback function is automatically called. #callback function is automatically called.
async def func(data): async def func(chatdata):
for c in data.items: for c in chatdata.items:
print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}") print(f"{c.datetime} [{c.author.name}]-{c.message} {c.amountString}")
await data.tick_async() await chatdata.tick_async()
if __name__ == '__main__': if __name__ == '__main__':
try: try:
@@ -86,18 +97,20 @@ if __name__ == '__main__':
from pytchat import LiveChat, CompatibleProcessor from pytchat import LiveChat, CompatibleProcessor
import time import time
chat = LiveChat("DSGyEsJ17cI", chat = LiveChat("Zvp1pJpie4I",
processor = CompatibleProcessor() ) processor = CompatibleProcessor() )
while chat.is_alive(): while chat.is_alive():
data = chat.get() try:
polling = data['pollingIntervalMillis']/1000 data = chat.get()
for c in data['items']: polling = data['pollingIntervalMillis']/1000
if c.get('snippet'): for c in data['items']:
print(f"[{c['authorDetails']['displayName']}]" if c.get('snippet'):
f"-{c['snippet']['displayMessage']}") print(f"[{c['authorDetails']['displayName']}]"
time.sleep(polling/len(data['items'])) f"-{c['snippet']['displayMessage']}")
time.sleep(polling/len(data['items']))
except KeyboardInterrupt:
chat.terminate()
``` ```
### replay: ### replay:
If specified video is not live, If specified video is not live,
@@ -110,18 +123,21 @@ def main():
#seektime (seconds): start position of chat. #seektime (seconds): start position of chat.
chat = LiveChat("ojes5ULOqhc", seektime = 60*30) chat = LiveChat("ojes5ULOqhc", seektime = 60*30)
print('Replay from 30:00') print('Replay from 30:00')
while chat.is_alive(): try:
data = chat.get() while chat.is_alive():
for c in data.items: data = chat.get()
print(f"{c.elapsedTime} [{c.author.name}]-{c.message} {c.amountString}") for c in data.items:
data.tick() print(f"{c.elapsedTime} [{c.author.name}]-{c.message} {c.amountString}")
data.tick()
except KeyboardInterrupt:
chat.terminate()
if __name__ == '__main__': if __name__ == '__main__':
main() main()
``` ```
## Structure of Default Processor ## Structure of Default Processor
Each item can be got with items() function. Each item can be got with `items` function.
<table> <table>
<tr> <tr>
<th>name</th> <th>name</th>