Add soco, pychromecast with gtts !

This commit is contained in:
Thomas Fransolet 2021-02-25 20:40:58 +01:00
parent 769be7e476
commit 5425e7aeee
21 changed files with 222 additions and 0 deletions

1
RPI Code/blinkpy Submodule

@ -0,0 +1 @@
Subproject commit 79ead7b826bb09b95fc0197113515a662ce76acb

Binary file not shown.

View File

@ -0,0 +1,22 @@
from gtts import gTTSimport time
import pychromecast
from gtts import gTTS
casts, browser = pychromecast.get_chromecasts()
pychromecast.discovery.stop_discovery(browser)
if len(casts) == 0:
print("No Devices Found")
sys.exit(1)
print("Found cast devices:")
for cast in casts:
print(
' "{}" on mDNS service {} with UUID:{}'.format(
cast.name, cast._services, cast.uuid # pylint: disable=protected-access
)
)
print(casts[0])
tts = gTTS(text='Coucou petit asticot' , lang='fr')
tts.save('./hello_world_ja.mp3')

View File

@ -0,0 +1,86 @@
import omitted
import os.path
from bottle import route, run, static_file, template
from os import path
from flask import request
import hashlib
from gtts import gTTS
import pychromecast
BASE_DIR = '/home/thomas/googleHome'
TALK_DIR = '/home/thomas/googleHome/var'
#app = bottle()
@route('/talks/<file_path:path>')
def get_talk_mp3(file_path):
print(file_path)
print(TALK_DIR)
return static_file(file_path, root=TALK_DIR)
@route('/form/<text>/<lang>', method='GET')
def get_talk_form(text, lang):
"" "form display" ""
content = BASE_DIR + '/template.html'
text = text or ''
lang = lang or 'ja'
return template(content.open().read(), langs=gTTS.LANGUAGES, lang=lang, text=text)
@route('/form/<text>/<lang>', method='POST')
def post_talk_form(text, lang):
"" "Save the talk text, playback instruction" ""
print('ceci est un tesssst')
print(text)
print(lang)
text_token = generate_talk(text, lang)
#because troublesome, beating decided to GoogleCast terminal attached to the eye
chromecasts, browser = pychromecast.get_listed_chromecasts(friendly_names=["Bureau Mini"])
#chromecasts = pychromecast.get_chromecasts()
chromecasts[0].wait()
print(chromecasts[0].device)
cast = chromecasts[0]
print(f"http://192.168.31.140:8080/talks/{text_token}")
mc = cast.media_controller
mc.play_media(f"http://192.168.31.140:8080/talks/{text_token}", 'audio/mp3')
mc.block_until_active()
print(mc.status)
return get_talk_form(text, lang)
def generate_talk(text, lang):
"" "languages to use the cache When not match the text" ""
text_token = hashlib.sha256((lang + text).encode()).hexdigest()
text_token+='.mp3'
talk_path = TALK_DIR+ '/' + text_token
if not path.exists(talk_path):
tts = gTTS(text=text, lang=lang)
tts.save(talk_path)
print(talk_path)
print(text_token)
return text_token
if __name__ == '__main__':
if not path.isdir(TALK_DIR):
os.mkdir(TALK_DIR)
run(host='0.0.0.0', port='8080', reloader=True)

63
RPI Code/soco/test.py Normal file
View File

@ -0,0 +1,63 @@
import soco
import cgi
from soco.discovery import by_name
from soco.music_services import MusicService
device = soco.discovery.any_soco()
albums = device.music_library.get_albums(search_term='Black')
for album in albums:
print('Added:', album.title)
# device.add_to_queue(album)
for zone in soco.discover():
print(zone.player_name)
zone_list = list(soco.discover())
for zoneList in zone_list:
print(zoneList)
print(zoneList.player_name)
print(zoneList.volume)
device = by_name('Bureau')
print(device)
device.volume = 5
device.play()
print(device.get_current_track_info())
print(device.get_queue())
#print(MusicService.get_subscribed_services_names())
#spotify = MusicService('Spotify')
#print(spotify.available_search_categories)
#print(MusicService.get_subscribed_services_names())
#device.volume += 10
#device.pause()
print(device.get_speaker_info())
print(device.get_current_transport_info())
#device.next()
print(device.player_name)
print(device.get_sonos_playlists())
#device.play_uri('https://lasonotheque.org/UPLOAD/mp3/2249.mp3')
#device.play()
print(device.get_current_track_info())
def tts(message):
key = "30b5e38e083d40b5b5637060cd3a9ef4" # replace with your real key
lang = "fr-fr" # or whatever language
freq = "44khz_8bit_mono" # Sonos cannot play the default, this one is working...
device.play_uri("x-rincon-mp3radio://api.voicerss.org/?key=%s&hl=%s&f=%s&src='%s'" %(key, lang, freq, message),title="Computer speaking - Test Thomas")
#tts('lol ctest un test')
device.volume = 25
#device.play_uri('https://lasonotheque.org/UPLOAD/mp3/2333.mp3')
#device.play_uri('x-sonos-spotify:spotify:track:6z2iUdxsGg0ASb4wQzwhuK?sid=9&amp;flags=0&amp;sn=1')
#device.play_uri('x-sonos-spotify:spotify:track:7rNjupJkjsc3ANGeW0RCYj?sid=9&amp;flags=0&amp;sn=1')
#device.play_uri('http://192.168.31.140:8080/talks/428bf747af1dd44311ea5f6632bf7bb82b212dc149f915a37489d4836e944ad4.mp3')
device.play_uri('http://192.168.31.140:8080/talks/0eed83ca6ac7741c98c4d60120ec30c305e4613149c5387c8a0eac27815df606.mp3')
device.play_uri('http://192.168.31.140:8080/talks/8d27aadcd9d3d22b50c2428841dbe814c25666a7be93c0a38976570b4241d8d8.mp3')

View File

@ -0,0 +1,50 @@
import soco
import cgi
from soco.discovery import by_name
from soco.music_services import MusicService
device = soco.discovery.any_soco()
albums = device.music_library.get_albums(search_term='Black')
for album in albums:
print('Added:', album.title)
# device.add_to_queue(album)
for zone in soco.discover():
print(zone.player_name)
zone_list = list(soco.discover())
for zoneList in zone_list:
print(zoneList)
print(zoneList.player_name)
print(zoneList.volume)
device = by_name('Bureau')
print(device)
device.volume = 5
device.play()
print(device.get_current_track_info())
print(device.get_queue())
#print(MusicService.get_subscribed_services_names())
#spotify = MusicService('Spotify')
#print(spotify.available_search_categories)
#print(MusicService.get_subscribed_services_names())
#device.volume += 10
#device.pause()
print(device.get_speaker_info())
print(device.get_current_transport_info())
#device.next()
print(device.player_name)
print(device.get_sonos_playlists())
#device.play_uri('https://lasonotheque.org/UPLOAD/mp3/2249.mp3')
#device.play()
print(device.get_current_track_info())
def tts(message):