-
Notifications
You must be signed in to change notification settings - Fork 6
/
harmattan_hw.py
493 lines (444 loc) · 17.4 KB
/
harmattan_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
#! /usr/bin/env python
import os, sys
import email
import subprocess
import sqlite3
import dbus
from datetime import datetime, timedelta
import pyjulius
import Queue
import time
import random
import threading
import espeak2julius
from streetnames import get_street_names
import re
from ID3 import ID3
try:
import urllib.parse as parse
except:
import urllib as parse
import guessing
import ast # for safely parsing timed stuff
global app
app = None
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()
if os.path.exists('/tmp/espeak_lock'):
os.remove('/tmp/espeak_lock')
# WE NEED SQLITES3 AND PYTHON-DBUS
mailconn = sqlite3.connect('/home/user/.qmf/database/qmailstore.db')
mailcur = mailconn.cursor()
f = __file__.split('harmattan_hw.py')[0]
log = open('/home/user/debug.log','w')
log.write(f+'\n')
log.flush()
# 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)
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
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(',')
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 = []
def regen_contacts():
firsts = []
fulls = []
return # Contacts are borked for now
ccon = sqlite3.connect('/home/user/.cache/tracker/meta.db')
cur = ccon.cursor()
# THIS IS A TERRIBLE HACK
cur.execute('select "nco:PersonContact"."nco:nameGiven", "nco:PersonContact"."nco:nameFamily", "nco:PhoneNumber"."nco:phoneNumber", "nco:PersonContact"."ID" from "nco:PersonContact", "nco:PhoneNumber", "nco:Role_nco:hasPhoneNumber" where "nco:Role_nco:hasPhoneNumber"."ID"="nco:PersonContact"."ID"+1 and "nco:Role_nco:hasPhoneNumber"."nco:hasPhoneNumber"="nco:PhoneNumber"."ID"')
rows = cur.fetchall()
for first, last, phoneNumber, 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':True, 'contactId':contactId, 'phoneNumber':phoneNumber}
firstnames[first] = first+' '+last
guessing.variables['contact'].keywords.append(last)
print (fulls[-1])
else:
contacts[first] = {'hasPhoneNumber':True, 'contactId':contactId, 'phoneNumber':phoneNumber}
firstnames[first] = first
print (firsts[-1])
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')
'''if not os.path.exists('/home/user/.cache/saera/musictitles.dfa'):
if not os.path.exists('/home/user/.cache/saera'):
os.mkdir('/home/user/.cache/saera')
regen_music()
espeak2julius.create_grammar(lst, 'musictitles', 'songtitles')
else:
regen_music()
if not os.path.exists('/home/user/.cache/saera/contacts.dfa'):
if not os.path.exists('/home/user/.cache/saera'):
os.mkdir('/home/user/.cache/saera')
regen_contacts()
espeak2julius.create_grammar(list(contacts), 'contacts', 'contacts')
else:
regen_contacts()
if not os.path.exists('/home/user/.cache/saera/addresses.dfa'):
if not os.path.exists('/home/user/.cache/saera'):
os.mkdir('/home/user/.cache/saera')
regen_streetnames()
espeak2julius.create_grammar(streetnames, 'addresses', 'addresses')
else:
pass # We don't do anything with streetnames here so no point to load them'''
print ' '.join([f+'julius/julius-harmattan','-module','-gram',f+'julius/saera', '-gram', '/home/user/.cache/saera/musictitles', '-gram', '/home/user/.cache/saera/contacts', '-gram', '/home/user/.cache/saera/addresses','-h',f+'julius/hmmdefs','-hlist',f+'julius/tiedlist','-input','mic','-tailmargin','800','-rejectshort','600'])
# jproc = subprocess.Popen([f+'julius/julius-harmattan','-module','-gram',f+'julius/saera', '-gram', '/home/user/.cache/saera/musictitles', '-gram', '/home/user/.cache/saera/contacts', '-gram', '/home/user/.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-harmattan','-module','-gram',f+'julius/saera','-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
def pause_daemons():
global daemons_running
daemons_running = False
def resume_daemons():
global daemons_running
daemons_running = True
e.set()
# TODO: handle this in QML
# 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)
#
# e = threading.Event()
# prox_thread = threading.Thread(target=watch_proximity, args=(e,))
# prox_thread.start()
def listen():
print ("Listening...")
# purge message queue
time.sleep(0.6)
client.send("RESUME\n")
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
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.lower() 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]
res = " ".join(words)
res = res[0].upper()+res[1:]
client.send("TERMINATE\n")
return res
################## PAST HERE UNTESTED ###########################
class timed:
alarms = []
def check():
time_obj = bus.get_object('com.nokia.time', '/com/nokia/time')
time_intf = dbus.Interface(time_obj, 'com.nokia.time')
alarm_obj = bus.get_object('com.nokia.time', '/org/maemo/contextkit/Alarm/Trigger')
alarms = alarm_obj.Get(dbus_interface='org.maemo.contextkit.Property')[0]
cookies = alarms[0].keys()
timed.alarms = []
for cookie in cookies:
timestamp = alarms[0][cookie]
attributes = time_intf.query_attributes(cookie)
timed.alarms.append(attributes)
alarms_list.sort()
# result = subprocess.Popen(["timedclient-qt5", "--info"], stdout=subprocess.PIPE).communicate()
# rawvals = result[0].decode("UTF-8").split("Cookie ")
# 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:
# pass
# timed.alarms.append(alm)
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()
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()
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():
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()
return "Playing" if "Playing" in result[0].decode("UTF-8") else "Paused" if "Paused" in result[0].decode("UTF-8") else "Off" if not result[0] else "Stopped"
def pause():
result = subprocess.Popen(["gdbus",
"call",
"-e",
"-d",
"org.mpris.MediaPlayer2.jolla-mediaplayer",
"-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 song is None:
result = 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()
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 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 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():
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
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
else:
spoken_str = '\n'.join([i[0] for i in string])
if not os.path.exists("/tmp/espeak_lock"):
os.system('touch /tmp/espeak_lock && espeak -v +f2 "' + spoken_str.replace(":00"," o'clock").replace("\n",". ") + '" && rm /tmp/espeak_lock &')
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 quit():
conn.close()
client.stop()
client.disconnect()
client.join()
jproc.terminate()