-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase_db.py
111 lines (91 loc) · 2.79 KB
/
firebase_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
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
import csv
import numpy as np
import random
import pyrebase
import time
from firebase_admin import auth
import firebase_admin
from flask import Flask
from flask import request
from flask import render_template
from selenium import webdriver
import webbrowser
# firebase configuration
config = {
"apiKey": "XXXX",
"authDomain": "XXXX",
"databaseURL": "https://zeoco-XXXX.firebaseio.com",
"storageBucket": "zeoco-XXXXX.appspot.com",
"serviceAccount": "zeoco.json"
}
# init firebase objects
firebase = pyrebase.initialize_app(config) #pyrebase
default_app = firebase_admin.initialize_app() #firebase
db = firebase.database()
# files
with open('/home/summerhacks/zeoco/zeoco_db.csv', 'r') as f:
products = list(csv.reader(f, delimiter=','))
# working variables
products=np.array(products)
suggested_products = []
data={}
users = []
final = []
carbon_indices = []
index = 0
# log userbase
for user in auth.list_users().iterate_all():
users.append(user.uid)
db.child("users").child(user.uid).update({"test-suggested_productsey" : "test-val"})
# helper functions
def send(data,carbon_index):
app = zeoco_suggested_products(__name__, template_folder='template')
@app.route('/', methods=['GET', 'POST'])
def index():
browser = webdriver.Firefox()
browser.get('http://127.0.0.1:5000/shutdown')
browser.close()
return render_template('test.html',len=len(data),data = data)
def flaskThread():
app.run()
def shutdown_server():
print("ok shut up")
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug ')
func()
@app.route('/shutdown', methods=['GET','POST'])
def shutdown():
shutdown_server()
return "Shutting down server"
#return 'Server shutting down...'
if __name__ == "__main__":
#thread.start_new_thread(flaskThread(),)
app.run()
while True:
for j in range(len(users)):
carbon_indices = []
print(users[j])
if(list(db.child('users').child(users[j]).shallow().get().val())[0]=="Barcode"):
print("Entered user")
barcode=str(db.child('users').child(users[j]).child('Barcode').get().val())
suggested_products=[]
for i in range (41):
if (barcode==products[i,2]):
index=i
break
product=products[index][0]
less=float(products[index][3])
for i in range(41):
if products[i][0]==product:
carbon_index=float(products[i][3])
if (carbon_index<less):
print(products[i][1], "would be a better option :)")
suggested_products.append(products[i][1])
carbon_indices.append(carbon_index)
# print(suggested_products)
data={"Alternative suggestions":suggested_products}
final.append(data)
db.child("users").child(users[j]).set(data)
db.child("users").child(users[j]).update({"Carbon Indices":carbon_indices})
send(final,carbon_index)