-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (48 loc) · 1.73 KB
/
main.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import boto3
import pygame.mixer
import urllib.parse
from contextlib import closing
from time import sleep
from youtube_live_streaming_api_client import YoutubeLiveStreamingApiClient
YOUTUBE_CLIENT_SECRETS_FILE = 'client_secrets.json'
YOUTUBE_CLIENT_SCOPES = [
'https://www.googleapis.com/auth/youtube.readonly']
DEST_AUDIO_FILE = 'message.mp3'
WAIT_SEC = 10
polly = boto3.client('polly')
pygame.mixer.init()
def convert_to_voice(text, path):
result = polly.synthesize_speech(Text=text, OutputFormat='mp3',
VoiceId='Joanna', Engine='neural')
with closing(result['AudioStream']) as stream:
with open(path, 'wb') as file:
file.write(stream.read())
def play_sound(path):
pygame.mixer.music.load(path)
pygame.mixer.music.play(1)
while pygame.mixer.music.get_busy():
sleep(0.1)
def main():
youtube = YoutubeLiveStreamingApiClient(
YOUTUBE_CLIENT_SECRETS_FILE, YOUTUBE_CLIENT_SCOPES)
url = input('YouTube Live URL: ')
live_id = urllib.parse.urlparse(url).path[1:]
live_chat_id = youtube.get_live_chat_id(live_id)
next_page_token = None
try:
while True:
print('get live chat messages ...')
live_chat_messages = youtube.get_live_chat_messages(
live_chat_id, next_page_token)
for message in live_chat_messages['messages']:
print(message)
convert_to_voice(message, DEST_AUDIO_FILE)
play_sound(DEST_AUDIO_FILE)
sleep(1)
next_page_token = live_chat_messages['next_page_token']
sleep(WAIT_SEC)
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()