-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
75 lines (60 loc) · 2.53 KB
/
db.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cPickle,base64
import uuid
#Declate your database sqlite or mysql
#In Order to use mysql please uncomment the mysql line and comment the sqlite line or just change the sqlite name to mysql
db_type = "mysql"
#db_type = "sqlite"
__database__ = "siriserver"
__mysql_host__ = "siriserver.cxqenv0i7uek.us-east-1.rds.amazonaws.com"
__mysql_user__ = "siriserver"
__mysql_password__ = "siriserver"
__mysql_database__ = "siriserver"
if db_type!="mysql" and db_type!="mysql":
db_type!="sqlite"
if db_type == "mysql":
try:
import MySQLdb
except:
print "You must install MySQLdb to use MySQL with SiriServer. Try python-mysql or easy_install"
exit()
else:
try:
import sqlite3
except:
print "You must install SQLITE to use SQLITE with SiriServer. Try python-mysql or easy_install"
exit()
def setup():
conn = getConnection()
c = conn.cursor()
if db_type == "mysql":
c.execute("CREATE DATABASE IF NOT EXISTS googlesiri DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;")
c.execute("CREATE TABLE IF NOT EXISTS `assistants` (`id` int(255) unsigned NOT NULL AUTO_INCREMENT,`assistantId` text NOT NULL,`speechId` text NOT NULL,`censorSpeech` text NOT NULL,`timeZoneId` text NOT NULL,`language` text NOT NULL,`region` text NOT NULL,`firstName` text NOT NULL,`nickName` text NOT NULL,`date_created` datetime NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;")
else:
c.execute("create table if not exists assistants (assistantId text primary key, assistant assi)")
conn.commit()
c.close()
conn.close()
def getConnection():
if db_type == "mysql":
return MySQLdb.connect(__mysql_host__, __mysql_user__, __mysql_password__, __mysql_database__, use_unicode=True,charset='utf8')
else:
return sqlite3.connect(__database__, detect_types=sqlite3.PARSE_DECLTYPES)
class Assistant(object):
def __init__(self):
self.assistantId = None
self.speechId= None
self.censorSpeech = None
self.timeZoneId = None
self.language = None
self.region = None
self.nickName = u''
self.firstName=u''
if db_type == "sqlite":
def adaptAssistant(assistant):
return cPickle.dumps(assistant)
def convertAssistant(fromDB):
return cPickle.loads(fromDB)
sqlite3.register_adapter(Assistant, adaptAssistant)
sqlite3.register_converter("assi", convertAssistant)