-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube_comment_adapter.py
More file actions
35 lines (29 loc) · 1.05 KB
/
youtube_comment_adapter.py
File metadata and controls
35 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pytchat
import json
class YoutubeCommentAdapter:
def __init__(self, video_id) -> None:
self.chat = pytchat.create(video_id=video_id, interruptable=False)
def get_comment(self):
# コメントを一括で取得
comments = self.__get_comments()
if (comments is None):
return None
comment = comments[-1] # 最新のコメント
# コメント情報の中からコメントのみを取得
message = comment.get("message")
return message
def __get_comments(self):
if (not self.chat.is_alive()):
print("開始してません")
return None
comments = json.loads(self.chat.get().json())
if (comments == []):
print("コメントが取得できませんでした")
return None
return comments
if __name__ == "__main__":
import time
video_id = "youtube-video-id"
chat = YoutubeCommentAdapter(video_id)
time.sleep(1) # コメント取得のために少し待つ
print(chat.get_comment())