-
Notifications
You must be signed in to change notification settings - Fork 6
/
sailfish_hw.py
executable file
·917 lines (833 loc) · 31.5 KB
/
sailfish_hw.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
#! /usr/bin/env python
import pyotherside
import os, sys
import email
import subprocess
import sqlite3
from datetime import datetime, timedelta
import pyjulius
import queue as Queue
import time
import random
import threading
import espeak2julius
import json
from streetnames import get_street_names
import alsaaudio, audioop
import re
from ID3 import ID3
try:
import urllib.parse as parse
except:
import urllib as parse
import guessing
import base64
import hmac
import hashlib
import http.client as httplib
import urllib.request as urllib2
from collections import namedtuple
import shutil
Message = namedtuple("Message", ("id", "sender", "sender_id", "message", "account", "date"))
import ast # for safely parsing timed stuff
global app
app = None
settings_path = os.getenv('HOME')+'/.config/saera/setting.json'
class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)
def load_config():
settings = {
"use_gps":True,
"imperial":True,
"read_texts":False,
"internet_voice":False,
"internet_voice_engine":"Wit", # Options: Wit, Google, Houndify
}
if os.path.exists(settings_path):
with open(settings_path) as settings_file:
settings.update(json.load(settings_file))
else:
os.makedirs(settings_path[:settings_path.rindex('/')], exist_ok=True)
with open(settings_path, 'w') as settings_file:
json.dump(settings, settings_file)
return Struct(**settings)
config = load_config()
class MicroMock(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
memory_path = os.getenv('HOME')+'/.saera_memory.db'
if not os.path.exists(memory_path):
conn = sqlite3.connect(memory_path)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute('CREATE TABLE Variables (Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, VarName TEXT NOT NULL, Value TEXT NOT NULL, UpdateTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP)')
cur.execute('CREATE TABLE Locations (Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, LocName TEXT NOT NULL COLLATE NOCASE, Zip TEXT, Latitude REAL, Longitude REAL, Timezone REAL, UpdateTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP)')
cur.execute('CREATE TABLE People (Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL COLLATE NOCASE, Description TEXT, Born DATE, Died DATE, Gender TEXT, Profession TEXT)')
cur.execute('CREATE TABLE LocationLogs (Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Latitude REAL, Longitude REAL, UpdateTime TIMESTAMP DEFAULT CURRENT_TIMESTAMP)')
cur.execute('INSERT INTO Locations (LocName, Zip, Latitude, Longitude, Timezone) VALUES ("Tokyo", "", 35.6833, 139.6833, 9)')
cur.execute('INSERT INTO Locations (LocName, Zip, Latitude, Longitude, Timezone) VALUES ("Shanghai", "", 31.2, 121.5, 8)')
cur.execute('INSERT INTO Locations (LocName, Zip, Latitude, Longitude, Timezone) VALUES ("New York", "10001", 40.7127, -74.0059, -5)')
cur.execute('INSERT INTO Locations (LocName, Zip, Latitude, Longitude, Timezone) VALUES ("Mexico City", "08619", 19.433, -99.133, -6)')
cur.execute('INSERT INTO Locations (LocName, Zip, Latitude, Longitude, Timezone) VALUES ("Moscow", "101", 55.75, 37.6167, 3)')
cur.execute('INSERT INTO Locations (LocName, Zip, Latitude, Longitude, Timezone) VALUES ("Los Angeles", "90001", 34.05, -118.25, -8)')
conn.commit()
else:
conn = sqlite3.connect(memory_path)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
try:
mailconn = sqlite3.connect('/home/nemo/.qmf/database/qmailstore.db')
mailcur = mailconn.cursor()
has_mail = True
except sqlite3.OperationalError:
has_mail = False
f = __file__.split('sailfish_hw.py')[0]
# terminate any pre-existing julius processes
p = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
out, err = p.communicate()
for line in out.decode('UTF-8').splitlines():
if 'julius.arm' in line:
pid = int(line.split(None, 1)[0])
os.kill(pid, 9)
activeMediaPlayer = "jolla-mediaplayer"
song_title_map = {}
lst = []
def regen_music():
regex = re.compile('[^a-zA-Z ]')
# files = subprocess.Popen("find /home/nemo/Music/ -type f -name \*.mp3", shell=True, stdout=subprocess.PIPE).communicate()[0].splitlines()[:40]
# for file in files:
# id3info = ID3(file)
# if id3info.has_tag:
# lst.append(id3info.title.decode('utf-8'))
# song_title_map[id3info.title.decode('utf-8').lower()] = file
# else:
# name = os.path.split(file)[1].decode('utf-8').split('.')[0]
# if name.count(' - ')==1:
# artist, name = name.split(' - ')
# name = regex.sub('', name).strip()
# if name:
# lst.append(name)
# song_title_map[name.lower()] = file
# continue
tlist = subprocess.Popen('tracker-sparql -q "SELECT ?title ?artist ?url \
WHERE { ?song a nmm:MusicPiece . ?song nie:title ?title . ?song nmm:performer ?aName . ?aName nmm:artistName ?artist . \
?song nie:url ?url . }"', shell=True, stdout=subprocess.PIPE).communicate()[0].splitlines()[1:]
for line in tlist:
if not line: continue
try:
l = line.decode('utf-8').split(", file://")
file = l[-1]
# tracker-sparql uses commas in results and doesn't escape them. As there
# seems to be no workaround, we assume that all URLs are file:// urls and
# no artists contain commas.
index_of_last_comma = l[0].rindex(',')
except: # If there are no songs on device, tracker-sparql will return 'None' which does not contain commas
lst.append('music')
break
artist = l[0][index_of_last_comma+1:]
title = l[0][:index_of_last_comma]
if title.count(' - ')==1:
name_artist, title = title.split(' - ')
title = regex.sub('', title).strip()
lst.append(title)
# print (title)
song_title_map[title.lower()] = file
contacts = {}
firstnames = {}
streetnames = []
volume = 65536
def regen_contacts():
firsts = []
fulls = []
ccon = sqlite3.connect('/home/nemo/.local/share/system/Contacts/qtcontacts-sqlite/contacts.db')
cur = ccon.cursor()
cur.execute('SELECT lowerFirstName, lowerLastName, hasPhoneNumber, contactId from Contacts')
rows = cur.fetchall()
for first, last, hasPhoneNumber, contactId in rows:
if first is not None and first.isalpha():
firsts.append(first)
guessing.variables['contact'].keywords.append(first)
if last is not None and last.isalpha():
fulls.append(first+' '+last)
contacts[first+' '+last] = {'hasPhoneNumber':hasPhoneNumber, 'contactId':contactId}
firstnames[first] = first+' '+last
guessing.variables['contact'].keywords.append(last)
# try:
# print (fulls[-1])
# except UnicodeEncodeError:
# print ("Name contains Unicode string")
else:
contacts[first] = {'hasPhoneNumber':hasPhoneNumber, 'contactId':contactId}
firstnames[first] = first
# try:
# print (firsts[-1])
# except UnicodeEncodeError:
# print ("Name contains Unicode string")
cur.execute('SELECT Contacts.contactId, PhoneNumbers.phoneNumber from Contacts, PhoneNumbers where Contacts.contactId = PhoneNumbers.contactId')
rows = cur.fetchall()
for contactId, phoneNumber in rows:
if contactId in [contacts[i]['contactId'] for i in contacts]:
for i in contacts:
if contacts[i]['contactId'] == contactId:
contacts[i]['phoneNumber'] = phoneNumber
break
def regen_streetnames():
global streetnames
cur.execute("SELECT * FROM Variables WHERE VarName='here'")
here = cur.fetchone()
if here:
cur.execute("SELECT * FROM Locations WHERE Id="+str(here[2]))
here = cur.fetchone()
else:
cur.execute("SELECT * FROM Variables WHERE VarName='home'")
here = cur.fetchone()
if here:
cur.execute("SELECT * FROM Locations WHERE Id="+str(here[2]))
here = cur.fetchone()
else:
streetnames = [("main","st"),
("first","ave"),
("washington","blvd")]
return
stn = get_street_names(here)
for streettype in stn:
for streetname in stn[streettype]:
streetnames.append((streetname,streettype.lower()))
print (streetnames)
# espeak2julius.create_grammar(streetnames, 'addresses', 'addresses')
pyotherside.send('load_msg','Loading music titles...')
if not os.path.exists('/home/nemo/.cache/saera/musictitles.dfa'):
if not os.path.exists('/home/nemo/.cache/saera'):
os.mkdir('/home/nemo/.cache/saera')
regen_music()
espeak2julius.create_grammar(lst, 'musictitles', 'songtitles')
else:
regen_music()
pyotherside.send('load_msg','Loading contacts...')
if not os.path.exists('/home/nemo/.cache/saera/contacts.dfa'):
if not os.path.exists('/home/nemo/.cache/saera'):
os.mkdir('/home/nemo/.cache/saera')
regen_contacts()
espeak2julius.create_grammar(list(contacts) if contacts else ['John Smith'], 'contacts', 'contacts')
else:
regen_contacts()
pyotherside.send('load_msg','Loading street names...')
if not os.path.exists('/home/nemo/.cache/saera/addresses.dfa'):
if not os.path.exists('/home/nemo/.cache/saera'):
os.mkdir('/home/nemo/.cache/saera')
regen_streetnames()
pyotherside.send('load_msg','Loading street names\n(this may take a while)...')
espeak2julius.create_grammar(streetnames, 'addresses', 'addresses')
else:
pass # We don't do anything with streetnames here so no point to load them
pyotherside.send('load_msg','Initializing speech recognition...')
if not os.path.exists('/tmp/saera'):
os.mkdir('/tmp/saera')
jproc = subprocess.Popen([f+'julius/julius.jolla','-module', '-record', '/tmp/saera/', '-gram',f+'julius/saera', '-gram', '/home/nemo/.cache/saera/musictitles', '-gram', '/home/nemo/.cache/saera/contacts', '-gram', '/home/nemo/.cache/saera/addresses','-h',f+'julius/hmmdefs','-hlist',f+'julius/tiedlist','-input','mic','-tailmargin','800','-rejectshort','600'],stdout=subprocess.PIPE)
# jproc = subprocess.Popen([f+'julius/julius.arm','-module','-gram','/tmp/saera/musictitles','-h',f+'julius/hmmdefs','-hlist',f+'julius/tiedlist','-input','mic','-tailmargin','800','-rejectshort','600'],stdout=subprocess.PIPE)
client = pyjulius.Client('localhost',10500)
print ('Connecting to pyjulius server')
while True:
try:
client.connect()
break
except pyjulius.ConnectionError:
sys.stdout.write('.')
time.sleep(2)
sys.stdout.write('..Connected\n')
client.start()
client.send("TERMINATE\n")
detected = False
daemons_running = True
listening = False
active_listening = False
# These should really go in a utilities file
def post_multipart(host, selector, fields, files):
content_type, body = encode_multipart_formdata(fields, files)
while True:
try:
h = httplib.HTTPConnection(host)
h.putrequest('POST', selector)
h.putheader('content-type', content_type)
h.putheader('content-length', str(len(body)))
h.endheaders()
h.send(body)
break
except InterruptedError:
continue
# errcode, errmsg, headers = h.getreply()
response = h.getresponse()
# return h.file.read()
return response.read().decode('utf-8')
def encode_multipart_formdata(fields, files):
boundary = b"fhajlhafjdhjkfadsjhkfhajsfdhjfdhajkhjsfdakl"
CRLF = b'\r\n'
L = []
for (key, value) in fields.items():
L.append(b'--' + boundary)
L.append(('Content-Disposition: form-data; name="%s"' % key).encode('utf-8'))
L.append(b'')
L.append(value)
for (key, value) in files.items():
L.append(b'--' + boundary)
L.append(('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, key)).encode('utf-8'))
L.append(b'Content-Type: application/octet-stream')
L.append(b'')
L.append(value)
L.append(b'--' + boundary + b'--')
L.append(b'')
body = CRLF.join(L)
content_type = ('multipart/form-data; boundary=%s' % boundary.decode('utf-8')).encode('utf-8')
return content_type, body
access_key = "e46ce64507373c1d4e18a5e927efe7e0"
access_secret = "XGjCnOU0U4Dysum3kGmPDG0YH8gKvoMQUZY1hzox"
def pause_daemons():
global daemons_running
daemons_running = False
stop_active_listening()
def resume_daemons():
global daemons_running
daemons_running = True
e.set()
e2.set()
start_active_listening()
def watch_proximity(e):
global detected
while True:
prox_detect = open("/sys/devices/virtual/input/input10/prx_detect").read()
if bool(int(prox_detect)) and not detected:
detected = True
print ("Detected proximity input")
pyotherside.send('start')
if not daemons_running:
print ('Application unfocused.')
e.wait()
e.clear()
print ('Application focused.')
time.sleep(1)
def watch_headset_btn():
dbproc = subprocess.Popen(["dbus-monitor --system \"type='signal',sender='org.bluez',interface='org.bluez.Headset',member='AnswerRequested'\""],shell=True,stdout=subprocess.PIPE)
while True:
res = dbproc.stdout.readline()
if res.endswith(b'AnswerRequested\n'):
pyotherside.send('start')
def watch_mic(e):
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)
inp.setchannels(1)
inp.setrate(8000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(160)
while True:
l,data = inp.read()
if not daemons_running:
inp.pause()
e.wait()
e.clear()
inp.pause(False)
while True:
tl,tdata = inp.read()
if tl:
l,data = tl, tdata
else:
break
if l:
# Return the maximum of the absolute value of all samples in a fragment.
# print (audioop.max(data, 2))
pyotherside.send('set_vol',audioop.max(data,2))
time.sleep(.04)
e = threading.Event()
e2 = threading.Event()
e3 = threading.Event()
prox_thread = threading.Thread(target=watch_proximity, args=(e,))
mic_thread = threading.Thread(target=watch_mic, args=(e2,))
headset_thread = threading.Thread(target=watch_headset_btn)
prox_thread.start()
mic_thread.start()
headset_thread.start()
qgvdial_msgs = []
def load_qgvdial_messages():
qconn = sqlite3.connect('/home/nemo/.qgvdial/qgvdial.sqlite.db')
qcur = qconn.cursor()
rows = qcur.execute('SELECT * from gvinbox where flags & 1 != 0 and type=5')
for row in rows:
qgvdial_msgs.append([row[4],row[6]])
def check_qgvdial_messages():
qconn = sqlite3.connect('/home/nemo/.qgvdial/qgvdial.sqlite.db')
qcur = qconn.cursor()
qcur.execute('SELECT * from gvinbox where flags & 1 != 0 and type=5')
rows = qcur.fetchall()
new_msgs = []
for row in rows:
conversation = row[6].split('<br>')
for message in conversation:
if not message: continue
sender, msg = message.split(':</b> ')
sender = sender.split('<b>')[-1]
msg, msg_time = msg.split(' <i>')
msg_time = msg_time.split('</i>')[0]
msg_date = datetime.fromtimestamp(row[2])
hour, minute = msg_time.split(':')
minute, am_pm = minute.split(' ')
hour, minute = int(hour), int(minute)
if 'p' in am_pm.lower():
hour += 12
hour %= 24
msg_date = msg_date.replace(hour=hour, minute=minute)
msg_id = row[0]+hashlib.md5(message.encode('utf-8')).hexdigest()
msg = Message(id=msg_id, sender=sender, sender_id=row[4], message=msg, account='qgvdial', date=time.mktime(msg_date.timetuple()))
if msg not in qgvdial_msgs and sender!='Me':
new_msgs.append(msg)
qgvdial_msgs.append(msg)
return new_msgs
def check_messages():
def parse_txt_date(date):
return time.mktime(datetime.strptime(date.split(" GMT")[0], '%a %b %d %H:%M:%S %Y').timetuple())
rproc = subprocess.Popen(["commhistory-tool", "listgroups"],stdout=subprocess.PIPE)
res = rproc.communicate()[0]
new_msgs = []
groups = []
for i in res.splitlines():
i = i.decode('utf-8')
if i.startswith("Group"):
groups.append([])
v = i[i.index('('):i.index(')')]
if not v.startswith('0'):
# Yay! New messages!
pass
else:
msg = i.split('|')
if len(msg)<13:
continue
if msg[6]=='0':
# new_msgs.append([msg[10],msg[12]])
new_msgs.append(Message(id=msg[0], sender=msg[10], sender_id=msg[10], message=msg[12], account=msg[9], date=parse_txt_date(msg[2])))
return new_msgs
print ("Messages:")
for i in check_messages():
print ("%s: %s" % (i.sender, i.message))
print ("QGVDial:")
for i in check_qgvdial_messages():
print ("%s: %s" % (i.sender, i.message))
# pass
def watch_texts():
global detected
while True:
unread_msgs = check_qgvdial_messages()
if unread_msgs:
pyotherside.send('sayRich', '%s says: %s' % (unread_msgs[0].sender, unread_msgs[0].message), None, None, None)
if not daemons_running:
e.wait()
e.clear()
time.sleep(20)
text_thread = threading.Thread(target=watch_headset_btn)
#julius/julius.jolla -module -gram /usr/share/harbour-saera/qml/pages/julius/saera -gram /home/nemo/.cache/saera/musictitles -gram /home/nemo/.cache/saera/contacts -gram /home/nemo/.cache/saera/addresses -h /usr/share/harbour-saera/qml/pages/julius/hmmdefs -hlist /usr/share/harbour-saera/qml/pages/julius/tiedlist -input mic -tailmargin 800 -rejectshort 600
def listen():
l = threading.Thread(target=listen_thread)
l.start()
def listen_thread():
print ("Listening...")
global volume
volume_getter = subprocess.Popen(["pactl list sinks | grep \"Volume: front-left\" | awk '{print $3}'"], shell=True, stdout=subprocess.PIPE)
result, err = volume_getter.communicate()
# if config.internet_voice:
# if os.path.exists('/tmp/saera/outfile.ogg'):
# os.remove('/tmp/saera/outfile.ogg')
# audiorecorder = subprocess.Popen(["gst-launch-0.10 alsasrc ! audioconvert ! audioresample ! vorbisenc ! oggmux ! filesink location=/tmp/saera/outfile.ogg"], shell=True)
volume = int(result.split(b'\n')[1])
target_volume = int(volume/4)
subprocess.Popen(['pactl', 'set-sink-volume', '1', str(target_volume)])
# purge message queue
time.sleep(0.7)
client.send("RESUME\n")
global listening
listening = True
while 1:
try:
client.results.get(False)
except Queue.Empty:
break
print ("Message queue Empty")
while 1:
try:
result = client.results.get(False)
if isinstance(result,pyjulius.Sentence):
print ("SENTENCE")
print (dir(result), " ".join([i.word for i in result.words]), result.score)
break
elif result.tag=="RECOGFAIL":
# result.words = ['*mumble*']
result = MicroMock(words=[MicroMock(word='*mumble*')])
break
elif not listening:
return
except Queue.Empty:
continue
numbers = {'zero':'0','oh':'0','one':'1','two':'2','three':'3','four':'4','five':'5','six':'6','seven':'7','eight':'8','nine':'9'}
words = [i.word for i in result.words]
num_str = ''
for i, word in enumerate(words):
if len(words)>i-1:
if word in numbers:
num_str += numbers[word]
else:
if len(num_str)>1:
words[i-(len(num_str))] = num_str
words[i-(len(num_str))+1:i] = ['']*(len(num_str)-1)
num_str = ''
words = [i for i in words if i]
if words[0] in ['what','where','when','why','how','who','is','will','are','do','should','can','would','does']:
punct = '?'
else:
punct = '.'
res = " ".join(words)+punct
res = res[0].upper()+res[1:]
client.send("TERMINATE\n")
if config.internet_voice:
ifconfig_proc = subprocess.Popen(['/sbin/ifconfig'], stdout=subprocess.PIPE)
output, err = ifconfig_proc.communicate()
# Only send voice to server if we are on wifi
if b'wlan0' in output:
pyotherside.send('goBusy')
tmpfile = max(os.listdir('/tmp/saera'))
data = open('/tmp/saera/%s' % tmpfile, 'rb').read()
if config.internet_voice_engine=='Wit':
req = urllib2.Request('https://api.wit.ai/speech?v=20141022', data)
req.add_header('Content-Length', '%d' % len(data))
req.add_header('Authorization', "Bearer CH4ZMQO2X7VGXBCLERMDJFFO4RYQWFCK")
req.add_header('Content-Type', 'audio/wav')
rem_res = urllib2.urlopen(req)
out = rem_res.read()
j = json.loads(out.decode('utf-8'))
print (j)
if '_text' in j and j['_text']:
res = j['_text'][0].upper() + j['_text'][1:]
pyotherside.send('process_spoken_text',res)
def getTrigger():
while active_listening:
try:
print(".", end="")
result = client.results.get(True, 0.5)
if isinstance(result,pyjulius.Sentence) and len(result.words)==1 and result.words[0].word=="Saera" and result.words[0].confidence>0.7:
# getSpeech()
print ("Got trigger!")
pyotherside.send('trigger')
return
except Queue.Empty:
time.sleep(0.2)
continue
def start_active_listening():
global active_listening
if not active_listening:
active_listening = True
l = threading.Thread(target=getTrigger)
l.start()
def stop_active_listening():
global active_listening
active_listening = False
subprocess.Popen(['pactl', 'set-sink-volume', '1', str(volume)])
def cancel_listening():
client.send("TERMINATE\n")
global listening
global active_listening
listening = False
active_listening = False
subprocess.Popen(['pactl', 'set-sink-volume', '1', str(volume)])
class timed:
alarms = []
def check():
if os.path.exists(os.getenv('HOME')+'/.config/saera/alarms'):
alarm_list = open(os.getenv('HOME')+'/.config/saera/alarms').read().splitlines()
else:
if not os.path.exists(os.getenv('HOME')+'/.config/saera'):
os.mkdir(os.getenv('HOME')+'/.config/saera')
alarm_list = []
result = subprocess.Popen(["timedclient-qt5", "--info"], stdout=subprocess.PIPE).communicate()
rawvals = result[0].decode("UTF-8").split("Cookie ")
cookie = None
for val in rawvals:
alm = {}
for line in val.split('\n'):
line = line.strip()
# TODO: recurrence0 determines whether alarm is active if it comes from clock app
if '=' in line:
sections = [i.strip() for i in line.split('=')]
alm[sections[0]] = ast.literal_eval(sections[-1])
else:
# TODO: add alarm to list of alarms if set by us
# First, remove any alarms whose time has passed
try:
cookie = str(int(line))
except ValueError:
pass
if cookie in alarm_list:
hour = int(alm['timeOfDay']) // 60
minute = int(alm['timeOfDay']) % 60
now = datetime.now()
created_date = datetime.fromtimestamp(int(alm['createdDate']))
alarm_past = False
if (now-created_date).days > 1:
alarm_past = True
elif now.date()>created_date.date():
if now.hour>hour or (now.hour == hour and now.minute > minute):
alarm_past = True
elif hour>created_date.hour or (hour == created_date.hour and minute>created_date.minute):
if now.hour>hour or (now.hour == hour and now.minute > minute):
alarm_past = True
if alarm_past:
result = subprocess.Popen(["timedclient-qt5 --cancel-event=%s" % cookie], shell=True, stdout=subprocess.PIPE).communicate()
alarm_list.remove(cookie)
cookie = None
timed.alarms.append(alm)
with open(os.getenv('HOME')+'/.config/saera/alarms', 'w') as alarm_list_file:
alarm_list_file.write('\n'.join(alarm_list))
def set_alarm(time,message):
result = subprocess.Popen(["timedclient-qt5 -r'hour="+str(time.hour)+";minute="+str(time.minute)+";everyDayOfWeek;everyDayOfMonth;everyMonth;' -e'APPLICATION=nemoalarms;TITLE=Alarm;createdDate="+str(int(datetime.now().timestamp()))+";timeOfDay="+str(time.hour*60+time.minute)+";type=clock;alarm;reminder;boot;keepAlive;singleShot;time="+time.strftime("%Y-%m-%d %H:%M")+";'"], shell=True, stdout=subprocess.PIPE).communicate()
res_str = result[0].splitlines()
print (res_str)
cookie = res_str[-1].decode('utf-8').split('cookie is')[-1].strip()
with open(os.getenv('HOME')+'/.config/saera/alarms', 'a') as alarm_list_file:
alarm_list_file.write('\n'+cookie)
# timed.check()
def set_reminder(time,message,location=None):
result = subprocess.Popen(["timedclient-qt5 -b'TITLE=button0' -e'APPLICATION=saera;TITLE="+message+(";location="+location if location else "")+";time="+time.strftime("%Y-%m-%d %H:%M")+";'"], shell=True, stdout=subprocess.PIPE).communicate()
print (result)
timed.check()
timed.check()
def set_alarm(time, message = "alarm"):
timed.set_alarm(time,message)
def set_reminder(time,message,location=None):
return timed.set_reminder(time,message,location)
def run_text(t):
return app.execute_text(t)
def run_app(s):
global app
app = s
def is_playing():
global activeMediaPlayer
result = subprocess.Popen(["gdbus",
"call",
"-e",
"-d",
"org.mpris.MediaPlayer2.jolla-mediaplayer",
"-o",
"/org/mpris/MediaPlayer2",
"-m",
"org.freedesktop.DBus.Properties.Get",
"org.mpris.MediaPlayer2.Player",
"PlaybackStatus"], stdout=subprocess.PIPE).communicate()
spotresult = subprocess.Popen(["gdbus",
"call",
"-e",
"-d",
"org.mpris.MediaPlayer2.CuteSpot",
"-o",
"/org/mpris/MediaPlayer2",
"-m",
"org.freedesktop.DBus.Properties.Get",
"org.mpris.MediaPlayer2.Player",
"PlaybackStatus"], stdout=subprocess.PIPE).communicate()
if not result[0]:
activeMediaPlayer = "CuteSpot"
return "Playing" if "Playing" in spotresult[0].decode("UTF-8") else "Paused" if "Paused" in spotresult[0].decode("UTF-8") else "Off" if not spotresult[0] else "Stopped"
else:
activeMediaPlayer = "jolla-mediaplayer"
return "Playing" if "Playing" in result[0].decode("UTF-8") else "Paused" if "Paused" in result[0].decode("UTF-8") else "Stopped"
def pause():
result = subprocess.Popen(["gdbus",
"call",
"-e",
"-d",
"org.mpris.MediaPlayer2."+activeMediaPlayer,
"-o",
"/org/mpris/MediaPlayer2",
"-m",
"org.mpris.MediaPlayer2.Player.Pause"], stdout=subprocess.PIPE).communicate()
print (result)
def play(song=None):
if is_playing() in ("Playing", "Paused") and not song:
result = subprocess.Popen(["gdbus",
"call",
"-e",
"-d",
"org.mpris.MediaPlayer2."+activeMediaPlayer,
"-o",
"/org/mpris/MediaPlayer2",
"-m",
"org.mpris.MediaPlayer2.Player.Play"], stdout=subprocess.PIPE).communicate()
else:
if is_playing() == "Off":
os.system("jolla-mediaplayer &")
time.sleep(8) # for the media player to finish launching
print(song)
if song is not None and song in song_title_map:
f = song_title_map[song]
else:
files = subprocess.Popen("find /home/nemo/Music/ -type f -name \*.mp3", shell=True, stdout=subprocess.PIPE).communicate()[0].splitlines()
f = parse.quote(random.choice(files).decode('utf-8'))
print ("Playing file://"+f)
result = subprocess.Popen(["gdbus",
"call",
"-e",
"-d",
"org.mpris.MediaPlayer2.jolla-mediaplayer",
"-o",
"/org/mpris/MediaPlayer2",
"-m",
"org.mpris.MediaPlayer2.Player.OpenUri",
"file://"+f], stdout=subprocess.PIPE).communicate()
subprocess.Popen(["gdbus",
"call",
"-e",
"-d",
"org.mpris.MediaPlayer2.jolla-mediaplayer",
"-o",
"/org/mpris/MediaPlayer2",
"-m",
"org.mpris.MediaPlayer2.Player.Play"], stdout=subprocess.PIPE).communicate()
print (result)
def identify_song():
if os.path.exists('/tmp/rec.ogg'):
os.remove('/tmp/rec.ogg')
gproc = subprocess.Popen(['gst-launch-0.10 autoaudiosrc ! vorbisenc ! oggmux ! filesink location=/tmp/rec.ogg'], shell=True)
time.sleep(11)
gproc.terminate()
f = open('/tmp/rec.ogg', "rb")
sample_bytes = os.path.getsize('/tmp/rec.ogg')
content = f.read()
f.close()
http_method = "POST"
http_uri = "/v1/identify"
data_type = "audio"
signature_version = "1"
timestamp = time.time()
string_to_sign = http_method+"\n"+http_uri+"\n"+access_key+"\n"+data_type+"\n"+signature_version+"\n"+str(timestamp)
sign = base64.b64encode(hmac.new(access_secret.encode('utf-8'), string_to_sign.encode('utf-8'), digestmod=hashlib.sha1).digest())
fields = {'access_key':access_key.encode('utf-8'),
'sample_bytes':str(sample_bytes).encode('utf-8'),
'timestamp':str(timestamp).encode('utf-8'),
'signature':sign,
'data_type':data_type.encode('utf-8'),
"signature_version":signature_version.encode('utf-8')}
res = post_multipart("ap-southeast-1.api.acrcloud.com", "/v1/identify", fields, {"sample":content})
print (res)
result = json.loads(res)
if result['status']['code']==0: # Success!
title = result['metadata']['music'][0]['title']
artists = [i['name'] for i in result['metadata']['music'][0]['artists']]
if len(artists) == 1:
artists_string = artists[0]
else:
artists = ", ".join(artists[:-1])+" and "+artists[-1]
if "spotify" in result['metadata']['music'][0]['external_metadata']:
pv_url = json.loads(urllib2.urlopen("https://api.spotify.com/v1/tracks/"+result['metadata']['music'][0]['external_metadata']['spotify']['track']['id']).read().decode("utf-8"))["preview_url"]
return "It sounds like "+title+", by "+artists_string+".|"+"spot_preview|"+pv_url
elif "itunes" in result['metadata']['music'][0]['external_metadata']:
pv_url = json.loads(urllib2.urlopen("https://itunes.apple.com/lookup?id="+result['metadata']['music'][0]['external_metadata']['itunes']['track']['id']).read().decode("utf-8"))["results"][0]["previewUrl"]
else:
return "It sounds like "+title+", by "+artists_string+"."
elif result['status']['code']==1001:
return "I don't recognize it."
else:
return "I can't find out, the server gave me a "+str(result['status']['code'])+" error."
def play_url(url):
g = subprocess.Popen(['gst-launch-0.10 playbin2 uri='+url], shell=True)
g.wait()
return
def call_phone(num):
result = subprocess.Popen(["gdbus",
"call",
"-e",
"-d",
"com.jolla.voicecall.ui",
"-o",
"/",
"-m",
"com.jolla.voicecall.ui.dial",
"'"+num+"'"], stdout=subprocess.PIPE).communicate()
return "true" in result[0].decode("UTF-8")
def check_contact(contact):
if contact.lower() in contacts:
c = contacts[contact.lower()]
elif contact.lower() in firstnames:
c = contacts[firstnames[contact.lower()]]
else:
raise NameError
if c['hasPhoneNumber']:
return True
else:
raise AttributeError
def call_contact(contact):
if contact.lower() in contacts:
c = contacts[contact.lower()]
elif contact.lower() in firstnames:
c = contacts[firstnames[contact.lower()]]
else:
raise NameError
if c['hasPhoneNumber']:
print ("Calling "+c['phoneNumber'])
result = subprocess.Popen(["gdbus",
"call",
"-e",
"-d",
"com.jolla.voicecall.ui",
"-o",
"/",
"-m",
"com.jolla.voicecall.ui.dial",
"'"+c['phoneNumber']+"'"], stdout=subprocess.PIPE).communicate()
return "true" in result[0].decode("UTF-8")
else:
raise AttributeError
def get_unread_email():
if has_mail:
mailconn.execute("VACUUM") # to move messages from the WAL into the main database
mailcur.execute("SELECT * FROM mailmessages WHERE stamp>'"+(datetime.now()+timedelta(days=-1)).strftime("%Y-%m-%dT%H:%M:%S.000")+"'")
rows = mailcur.fetchall()
messages = []
for row in rows:
if bin(row[8])[2:][-8]=='0' and bin(row[8])[2:][-10]=='0': # one of those two bits is the read flag
messages.append({'type':'email','to':row[9],'from':row[4].split(" <")[0].split(" (")[0].replace('"',''),'subject':row[6].split(' [')[0],'content':row[22]})
return messages
else:
return []
class MailFolder:
def __init__(self):
self.messages = {}
def check(self):
for i in os.listdir(os.getenv("HOME")+"/.qmf/mail/"):
if not i in self.messages and not "part" in i:
self.messages[i] = email.message_from_file(open(os.getenv("HOME")+"/.qmf/mail/"+i))
def speak(string):
global detected
try:
is_string = isinstance(string,basestring)
except NameError:
is_string = isinstance(string,str)
if is_string:
spoken_str = string.split('|')[0]
else:
spoken_str = '\n'.join([i[0] for i in string])
if not os.path.exists("/tmp/espeak_lock"):
os.system('pactl set-sink-volume 1 %i' % (volume/2))
os.system('touch /tmp/espeak_lock && espeak --stdout -v +f2 "' + spoken_str.replace(":00"," o'clock").replace("\n",". ").replace(":", " ") + '" |'
' gst-launch-0.10 -q fdsrc ! wavparse ! audioconvert ! volume volume=4.0 ! alsasink && rm /tmp/espeak_lock && pactl set-sink-volume 1 %i &' % volume)
detected = False
return string
def enablePTP():
pyotherside.send('enablePTP')
def disablePTP():
pyotherside.send('disablePTP')
def sayRich(spokenMessage, message, img, lat=0, lon=0):
pyotherside.send('sayRich',message, img, lat, lon)
speak(spokenMessage)
def check_can_listen():
return not os.path.exists("/tmp/espeak_lock")
def quit():
conn.close()
client.stop()
client.disconnect()
client.join()
jproc.terminate()
if os.path.exists('/tmp/saera'):
shutil.rmtree('/tmp/saera')