-
Notifications
You must be signed in to change notification settings - Fork 18
/
ufirebase.py
251 lines (233 loc) · 8.77 KB
/
ufirebase.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
import ujson
import usocket
import ussl
import _thread
import time
class FIREBASE_GLOBAL_VAR:
GLOBAL_URL=None
GLOBAL_URL_ADINFO=None
SLIST={}
class INTERNAL:
def connect(id):
LOCAL_ADINFO=usocket.getaddrinfo(FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["host"], FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["port"], 0, usocket.SOCK_STREAM)[0]
FIREBASE_GLOBAL_VAR.SLIST["S"+id] = usocket.socket(LOCAL_ADINFO[0], LOCAL_ADINFO[1], LOCAL_ADINFO[2])
FIREBASE_GLOBAL_VAR.SLIST["S"+id].connect(LOCAL_ADINFO[-1])
if FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["proto"] == "https:":
try:
FIREBASE_GLOBAL_VAR.SLIST["SS"+id] = ussl.wrap_socket(FIREBASE_GLOBAL_VAR.SLIST["S"+id], server_hostname=FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["host"])
except:
print("ENOMEM, try to restart. If you make to many id's (sokets) simultaneously (bg=1 and id=x), try to use less or use a board with more ram!\nSome emulation software limits the RAM.")
FIREBASE_GLOBAL_VAR.SLIST["S"+id].close()
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=None
FIREBASE_GLOBAL_VAR.SLIST["S"+id]=None
raise MemoryError
else:
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=FIREBASE_GLOBAL_VAR.SLIST["S"+id]
def disconnect(id):
FIREBASE_GLOBAL_VAR.SLIST["SS"+id].close()
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=None
FIREBASE_GLOBAL_VAR.SLIST["S"+id]=None
def put(PATH, DATA, id, cb):
try:
while FIREBASE_GLOBAL_VAR.SLIST["SS"+id]:
time.sleep(2)
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
except:
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
INTERNAL.connect(id)
LOCAL_SS=FIREBASE_GLOBAL_VAR.SLIST["SS"+id]
LOCAL_SS.write(b"PUT /"+PATH+b".json HTTP/1.0\r\n")
LOCAL_SS.write(b"Host: "+FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["host"]+b"\r\n")
LOCAL_SS.write(b"Content-Length: "+str(len(DATA))+"\r\n\r\n")
LOCAL_SS.write(DATA)
LOCAL_DUMMY=LOCAL_SS.read()
del LOCAL_DUMMY
INTERNAL.disconnect(id)
if cb:
try:
cb[0](*cb[1])
except:
try:
cb[0](cb[1])
except:
raise OSError("Callback function could not be executed. Try the function without ufirebase.py callback.")
def patch(PATH, DATATAG, id, cb):
try:
while FIREBASE_GLOBAL_VAR.SLIST["SS"+id]:
time.sleep(1)
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
except:
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
INTERNAL.connect(id)
LOCAL_SS=FIREBASE_GLOBAL_VAR.SLIST["SS"+id]
LOCAL_SS.write(b"PATCH /"+PATH+b".json HTTP/1.0\r\n")
LOCAL_SS.write(b"Host: "+FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["host"]+b"\r\n")
LOCAL_SS.write(b"Content-Length: "+str(len(DATATAG))+"\r\n\r\n")
LOCAL_SS.write(DATATAG)
LOCAL_DUMMY=LOCAL_SS.read()
del LOCAL_DUMMY
INTERNAL.disconnect(id)
if cb:
try:
cb[0](*cb[1])
except:
try:
cb[0](cb[1])
except:
raise OSError("Callback function could not be executed. Try the function without ufirebase.py callback.")
def get(PATH, DUMP, id, cb, limit):
try:
while FIREBASE_GLOBAL_VAR.SLIST["SS"+id]:
time.sleep(1)
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
except:
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
INTERNAL.connect(id)
LOCAL_SS=FIREBASE_GLOBAL_VAR.SLIST["SS"+id]
LOCAL_SS.write(b"GET /"+PATH+b".json?shallow="+ujson.dumps(limit)+b" HTTP/1.0\r\n")
LOCAL_SS.write(b"Host: "+FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["host"]+b"\r\n\r\n")
LOCAL_OUTPUT=ujson.loads(LOCAL_SS.read().splitlines()[-1])
INTERNAL.disconnect(id)
globals()[DUMP]=LOCAL_OUTPUT
if cb:
try:
cb[0](*cb[1])
except:
try:
cb[0](cb[1])
except:
raise OSError("Callback function could not be executed. Try the function without ufirebase.py callback.")
def getfile(PATH, FILE, bg, id, cb, limit):
try:
while FIREBASE_GLOBAL_VAR.SLIST["SS"+id]:
time.sleep(1)
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
except:
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
INTERNAL.connect(id)
LOCAL_SS=FIREBASE_GLOBAL_VAR.SLIST["SS"+id]
LOCAL_SS.write(b"GET /"+PATH+b".json?shallow="+ujson.dumps(limit)+b" HTTP/1.0\r\n")
LOCAL_SS.write(b"Host: "+FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["host"]+b"\r\n\r\n")
while not LOCAL_SS.readline()==b"\r\n":
pass
LOCAL_FILE=open(FILE, "wb")
if bg:
while True:
LOCAL_LINE=LOCAL_SS.read(1024)
if LOCAL_LINE==b"":
break
LOCAL_FILE.write(LOCAL_LINE)
time.sleep_ms(1)
else:
while True:
LOCAL_LINE=LOCAL_SS.read(1024)
if LOCAL_LINE==b"":
break
LOCAL_FILE.write(LOCAL_LINE)
LOCAL_FILE.close()
LOCAL_DUMMY=LOCAL_SS.read()
del LOCAL_DUMMY
INTERNAL.disconnect(id)
if cb:
try:
cb[0](*cb[1])
except:
try:
cb[0](cb[1])
except:
raise OSError("Callback function could not be executed. Try the function without ufirebase.py callback.")
def delete(PATH, id, cb):
try:
while FIREBASE_GLOBAL_VAR.SLIST["SS"+id]:
time.sleep(1)
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
except:
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
INTERNAL.connect(id)
LOCAL_SS=FIREBASE_GLOBAL_VAR.SLIST["SS"+id]
LOCAL_SS.write(b"DELETE /"+PATH+b".json HTTP/1.0\r\n")
LOCAL_SS.write(b"Host: "+FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["host"]+b"\r\n\r\n")
LOCAL_DUMMY=LOCAL_SS.read()
del LOCAL_DUMMY
INTERNAL.disconnect(id)
if cb:
try:
cb[0](*cb[1])
except:
try:
cb[0](cb[1])
except:
raise OSError("Callback function could not be executed. Try the function without ufirebase.py callback.")
def addto(PATH, DATA, DUMP, id, cb):
try:
while FIREBASE_GLOBAL_VAR.SLIST["SS"+id]:
time.sleep(1)
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
except:
FIREBASE_GLOBAL_VAR.SLIST["SS"+id]=True
INTERNAL.connect(id)
LOCAL_SS=FIREBASE_GLOBAL_VAR.SLIST["SS"+id]
LOCAL_SS.write(b"POST /"+PATH+b".json HTTP/1.0\r\n")
LOCAL_SS.write(b"Host: "+FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO["host"]+b"\r\n")
LOCAL_SS.write(b"Content-Length: "+str(len(DATA))+"\r\n\r\n")
LOCAL_SS.write(DATA)
LOCAL_OUTPUT=ujson.loads(LOCAL_SS.read().splitlines()[-1])
INTERNAL.disconnect(id)
if DUMP:
globals()[DUMP]=LOCAL_OUTPUT["name"]
if cb:
try:
cb[0](*cb[1])
except:
try:
cb[0](cb[1])
except:
raise OSError("Callback function could not be executed. Try the function without ufirebase.py callback.")
def setURL(url):
FIREBASE_GLOBAL_VAR.GLOBAL_URL=url
try:
proto, dummy, host, path = url.split("/", 3)
except ValueError:
proto, dummy, host = url.split("/", 2)
path = ""
if proto == "http:":
port = 80
elif proto == "https:":
import ussl
port = 443
else:
raise ValueError("Unsupported protocol: " + proto)
if ":" in host:
host, port = host.split(":", 1)
port = int(port)
FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO={"proto": proto, "host": host, "port": port}
def put(PATH, DATA, bg=True, id=0, cb=None):
if bg:
_thread.start_new_thread(INTERNAL.put, [PATH, ujson.dumps(DATA), str(id), cb])
else:
INTERNAL.put(PATH, ujson.dumps(DATA), str(id), cb)
def patch(PATH, DATATAG, bg=True, id=0, cb=None):
if bg:
_thread.start_new_thread(INTERNAL.patch, [PATH, ujson.dumps(DATATAG), str(id), cb])
else:
INTERNAL.patch(PATH, ujson.dumps(DATATAG), str(id), cb)
def getfile(PATH, FILE, bg=False, id=0, cb=None, limit=False):
if bg:
_thread.start_new_thread(INTERNAL.getfile, [PATH, FILE, bg, str(id), cb, limit])
else:
INTERNAL.getfile(PATH, FILE, bg, str(id), cb, limit)
def get(PATH, DUMP, bg=False, cb=None, id=0, limit=False):
if bg:
_thread.start_new_thread(INTERNAL.get, [PATH, DUMP, str(id), cb, limit])
else:
INTERNAL.get(PATH, DUMP, str(id), cb, limit)
def delete(PATH, bg=True, id=0, cb=None):
if bg:
_thread.start_new_thread(INTERNAL.delete, [PATH, str(id), cb])
else:
INTERNAL.delete(PATH, str(id), cb)
def addto(PATH, DATA, DUMP=None, bg=True, id=0, cb=None):
if bg:
_thread.start_new_thread(INTERNAL.addto, [PATH, ujson.dumps(DATA), DUMP, str(id), cb])
else:
INTERNAL.addto(PATH, ujson.dumps(DATA), DUMP, str(id), cb)