<p>我不得不在Youtube上获取实时聊天,并选择使用<strong>pytchat</strong>而不是Youtube API。获取聊天记录没有问题,但速度有点慢</p>
<pre><code>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
</code></pre>
<p>下面显示了17649人观看的视频输出:(B到a需要5秒)</p>
<pre><code>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
</code></pre>
<p>下面显示了702观看的视频输出:(B到a至少需要10秒)</p>
<pre><code>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 ++++++++++++++++
</code></pre>
<p>我假设不同的观看量会影响时间?每次聊天都要迟到4到6秒,有可能解决吗?或者这就是Pytchat的工作原理</p>
<p>使用</p>
<pre><code>while chat.is_alive():
data = chat.get()
for c in data.sync_items():
#c can then be formatted for ur stuff
print("Formatting stuff")
</code></pre>
<p>sync_items()将为您提供适当的实时聊天移动</p>