Skip to content

Instantly share code, notes, and snippets.

@chrisboyle
Last active December 1, 2019 09:27
Show Gist options
  • Save chrisboyle/7af5da4c52a91c705139b7db5f31e1f6 to your computer and use it in GitHub Desktop.
Save chrisboyle/7af5da4c52a91c705139b7db5f31e1f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import bgtunnel
import requests
import subprocess
import sys
if sys.version_info >= (3, 0):
from urllib.parse import urlsplit, urlunsplit
else:
from urlparse import urlsplit, urlunsplit
if len(sys.argv) < 2:
sys.exit('Usage: assist "query"')
cmd = ' '.join(sys.argv[1:])
fwd = bgtunnel.open(ssh_user='pi', ssh_address='pi', ssh_port=22, host_port=3000, silent=True)
r = requests.post('http://localhost:%d/assistant' % fwd.bind_port, json={'command': cmd})
body = r.json()
if r.status_code != 200 or body['success'] != True or ('response' not in body):
sys.exit(r.status_code + '\n' + body)
if body['response'] != '':
print(body['response'])
if 'audio' in body:
audio = urlsplit(body['audio'])
audio = urlunsplit((audio[0], 'localhost:%d' % fwd.bind_port) + audio[2:5])
subprocess.check_output(['vlc', '-qI', 'dummy', audio, 'vlc://pause:1', 'vlc://quit'], stderr=subprocess.STDOUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment