-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccesDB.py
140 lines (131 loc) · 4.93 KB
/
AccesDB.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
# coding: UTF-8
import pyrebase
import time
class AccessToDataBase:
#ジャンル トピック プロパティー プレディケイと
def __init__(self):
self.config = {
"apiKey": "AIzaSyCA9NmzoNn5wC63wzdKuwCwhrPL4Ccuzew",
"authDomain": "dialogsystem-7767f.firebaseapp.com",
"databaseURL": "https://dialogsystem-7767f.firebaseio.com",
"storageBucket": ""
}
self.firebase = pyrebase.initialize_app(self.config)
self.db = self.firebase.database()
self.input_text = ""
def updateDB(self,data):
#do something
element = self.db.child(data[0]).child(data[1]).child(data[2]).get().val()
if isinstance(element, str):
element = [element,data[3]]
elif isinstance(element, list):
element.append(data[3])
else:
element = data[3]
self.db.child(data[0]).child(data[1]).child(data[2]).set(element)
def getData(self, arg):
_a = self.db.child(arg).get()
return _a
def stream_handler(self,message):
if self.saved_text != message["data"]:
self.input_text = message["data"]
self.saved_text = self.input_text
def listen(self):
self.saved_text = self.db.child("message-top").remove()
self.input_text = ""
time.sleep(1)
self.my_stream = self.db.child("message-top").stream(self.stream_handler)
while self.input_text == "":
time.sleep(.5)
self.my_stream.close
return self.input_text
def getTopiclist(self,genre):
if genre == '':
pass
else:
result = self.db.child('君の名は。').get()
lis = []
for i in result.val():
lis.append(i)
return lis
# do something
#def getData(self,genre,topic):
# result = self.db.child(genre).child(topic).get()
# return result
def searchDB(self,genre,topic,proper,predicate):
#DBを検索する.ジャンルは必須,
#トピックがない場合には,プロパティーとプレディケイとが必要
#ジャンルとトピックだけだと所得したデータの全てを返す
#全てが埋まっていたら,確認用に使用可能
#DB構成図
# root - NewGame! |-aoba_suzukaze |-eye |-blue
# | | | |
# | | | |-small
# | | |
# | | |-hear |- long
# | | . |- twin
# | | . |- blue
# | | .
# | |
# | |-kou_yagami ---|-eye |-yellow
# | | |
# | | |-small
# | |
# | |-hear |- long
# . . |- strate
# . . |- gold
# . .
#
# print(searchDB(NewGame!,aoba_suzukaze,eye,'')) $ [eye:blue,small]
# print(searchDB(NewGame!,'',eye,blue)) $ [aoba_suzukaze,hihumi,....] 所得に失敗した時 $ [eye:None]
# print(searchDB(NewGame!,'kou_yagami','','')) $ [eye:[blue,small],]
# print(searchDB('','aoba_suzukaze',eye,blue)) $ []
# print(searchDB('NewGame!','','',blue)) $ []
# print(searchDB('NewGame!','aoba_suzukaze','eye',blue)) $ [eye:blue,small]
# print(searchDB('NewGame!','aoba_suzukaze','eye',red)) $ [eye:blue]
#
try:
if not genre:
return []
if not topic:
result = []
if proper and predicate:
recivedData = self.db.child(genre).get()
for i in recivedData.val():
for j in recivedData.val()[i].keys():
if j == proper:
if predicate in recivedData.val()[i][j]:
result.append(i)
return result
else:
return []
else:
trueResult = []
if predicate and not proper:
result = self.db.child(genre).child(topic).get()
for k,v in result.val().items():
if type(v) is list and predicate in v:
trueResult.append(k)
elif type(v) is str and predicate == v:
trueResult.append(k)
return trueResult
else:
print(genre, topic, proper)
result = self.db.child(genre).child(topic).child(proper).get()
print("search result", result.val())
if type(result.val()) is str:
return [result.val()]
for i in result.val():
trueResult.append(i)
return trueResult
except:
return []
#ac = AccessToDataBase()
#valll = ac.db.child('NewGame!').get().val()
#print(valll)
#ac.db.child('NEWGAME!').set(valll)
#print('s')
#print(ac.searchDB('NewGame','涼風青葉','髪',''))# $ [eye:blue,small]
#ac.updateDB(('NewGame','八神コウ','耳','緑'))
#ac.getData('test')
#ac.listen()