diff --git a/backend.py b/backend.py new file mode 100644 index 0000000..c8b6f8f --- /dev/null +++ b/backend.py @@ -0,0 +1,79 @@ +from firebase import firebase +firebase = firebase.FirebaseApplication('https://learntogether-a250b.firebaseio.com/') + + +def fileReader(firebase, file, user): + unitFile = open(file, 'r') + unitList = [] + + for line in unitFile: + unitList.append(line) + + unitFile.close() + unitStored = [] + + for i in range(1,len(unitList)): + unitLine = unitList[i].strip().split("\t") + unitName = unitLine[0].split("_") + unitLine[0]=unitName[0] + unitStrip = [] + unitStrip.append(unitLine[0]) + unitStrip.append(unitLine[2]+unitLine[3]) + unitStrip.append(unitLine[4]+unitLine[5]) + unitStrip.append(unitLine[7].replace("/","-")) + unitStrip.append(unitLine[2]) + unitStrip.append(unitLine[3]) + if unitLine[9][1] == ".": + unitLine[9] = unitLine[9][:3] + else: + unitLine[9] = unitLine[9][0] + unitStrip.append(unitLine[9]) + unitStored.append(unitStrip) + + unitOut = unitStored + unitSimple = [] + unitSimpleTemp = [] + for unitRef in unitOut: + unitStore = unitRef[0]+"*"+unitRef[1]+"*"+unitRef[2]+"*"+unitRef[3] + unitStoreTemp = unitRef[0]+"*"+unitRef[1]+"*"+unitRef[2]+"*"+unitRef[3]+ "*" +unitRef[6] + unitSimple.append(unitStore) + unitSimpleTemp.append(unitStoreTemp) + unit = unitSimple + unitTemp = unitSimpleTemp + + firebase.put("", "users/" + str(user[0]) + "/classes", unitTemp) + + newUnit = [] + for p in range(len(unit)): + newUnit.append([]) + + for x in range(len(unit)): + newUnit[x].append(unitOut[x][0]) + newUnit[x].append(unitOut[x][4]) + newUnit[x].append(unitOut[x][5]+unitOut[x][2]) + + + oldUsers = [] + for n in range(len(unit)): + oldUsers.append([]) + temp = [] + + for x in range(len(unit)): + temp.append(firebase.get('','/classes/'+unit[x]+'/users/')) + if temp[x] not in oldUsers[x] and temp[x] is not None: + oldUsers[x].extend(temp[x]) + + if user[0] not in oldUsers[x]: + oldUsers[x].extend(user) + + for i in range(len(unit)): + dictionary = {'location': unitOut[i][3], 'time': unitOut[i][2], 'unitID': unitOut[i][0], 'group':unitOut[i][1],'users':oldUsers[i]} + firebase.put("","/classes/"+str(unit[i]),dictionary) + + #End of function + + +### Main +file = 'timetable-10001000.txt' +user = ["Ishan"] +fileReader(firebase, file, user) diff --git a/backend_optimized.py b/backend_optimized.py new file mode 100644 index 0000000..061f855 --- /dev/null +++ b/backend_optimized.py @@ -0,0 +1,179 @@ +import datetime +import json +import os.path + +import pyrebase +""" +TODO: +""" +#TODO add file check (is legit text file) +#TODO only add semester 1 + + +from firebase import firebase +firebase = firebase.FirebaseApplication('https://learntogether-a250b.firebaseio.com/') + +config = { + 'apiKey': "AIzaSyBLA6sDqXwnMX6tJayM4dmYCPRzt5CW91M", + 'authDomain': "learntogether-a250b.firebaseapp.com", + 'databaseURL': "https://learntogether-a250b.firebaseio.com", + 'storageBucket': "learntogether-a250b.appspot.com", +} + + +pybase = pyrebase.initialize_app(config) + + +allclasses = None +if os.path.exists("classes.json"): + j = open("classes.json", 'r+') + allclasses = json.load(j) + j.close() +else: + allclasses = firebase.get("","classes/") + j = open("classes.json", 'w') + json.dump(allclasses, j) + j.close() + +print(allclasses) + + + +storage = pybase.storage() + +def fileReader(firebase, file, user): + try: + unitFile = open(file, 'r') + unitList = [] + + for line in unitFile: + unitList.append(line) + + unitFile.close() + unitStored = [] + + for i in range(1,len(unitList)): + unitLine = unitList[i].strip().split("\t") + unitName = unitLine[0].split("_") + if(unitName[2]!="S1"): + unitLine[0]=unitName[0] + unitStrip = [] + unitStrip.append(unitLine[0]) + unitStrip.append(unitLine[2]+unitLine[3]) + unitStrip.append(unitLine[4]+unitLine[5]) + unitStrip.append(unitLine[7].replace("/","-")) + unitStrip.append(unitLine[2]) + unitStrip.append(unitLine[3]) + if unitLine[9][1] == ".": + unitLine[9] = unitLine[9][:3] + else: + unitLine[9] = unitLine[9][0] + unitStrip.append(unitLine[9]) + unitStored.append(unitStrip) + + + unitOut = unitStored + + unitSimple = [] + unitSimpleTemp = [] + for unitRef in unitOut: + unitStore = unitRef[0]+"*"+unitRef[1]+"*"+unitRef[2][0:3]+"*"+unitRef[2][3:]+"*"+unitRef[3]+"*"+unitRef[-1].replace(".", "~") + unitStoreTemp = unitRef[0]+"*"+unitRef[1]+"*"+unitRef[2][0:3]+"*"+unitRef[2][3:]+"*"+unitRef[3]+"*"+unitRef[-1].replace(".", "~") + + # print(unitStore, unitStoreTemp) + + unitSimple.append(unitStore) + unitSimpleTemp.append(unitStoreTemp) + unit = unitSimple + #print(unit) + #print() + unitTemp = unitSimpleTemp + + + firebase.put("", "users/" + str(user[0]) + "/classes", unitTemp) + + newUnit = [] + for p in range(len(unit)): + newUnit.append([]) + + for x in range(len(unit)): + newUnit[x].append(unitOut[x][0]) + newUnit[x].append(unitOut[x][4]) + newUnit[x].append(unitOut[x][5]+unitOut[x][2]) + + #print(unit, newUnit) + + oldUsers = [] + for n in range(len(unit)): + oldUsers.append([]) + temp = [] + + for x in range(len(unit)): + try: + #a = firebase.get('','classes/'+unit[x]+'/users/') + #print(unit[x]) + + a = allclasses[unit[x]]['users'] + + temp.append(a) + + if temp[x] not in oldUsers[x] and temp[x] is not None: + oldUsers[x].extend(temp[x]) + + if user[0] not in oldUsers[x]: + oldUsers[x].extend(user) + except Exception as e: + firebase.put('/', 'requests/' + str(user[0]) + "/status", 'ERR'+str(e)) + + for i in range(len(unit)): + dictionary = {'users':oldUsers[i]} + firebase.put("","classes/"+str(unit[i]),dictionary) + allclasses[str(unit[i])] = dictionary + + j = open("classes.json", 'w') + json.dump(allclasses, j) + j.close() + + + firebase.put('/', 'requests/' + str(user[0]) + "/status", 'SUCCESS') + #End of function + except Exception as e: + firebase.put('/', 'requests/' + str(user[0]) + "/status", 'ERROR\t' + str(e)) + + + ### Main + + +requests = firebase.get('/','requests/') + + +#print(requests) +#Download all the files + +a = datetime.datetime.now() +print(a) +for request in requests: + if(requests[request]['status'] == "UPLOAD"): + storage.child("usertimetable/"+request).download("user_files/"+request) + firebase.put('/','requests/'+request+"/status", 'DOWNLOADED') +b = datetime.datetime.now() +print(b, b-a) + +for request in requests: + if(requests[request]['status']!="SUCCESS"): + user = request + fileReader(firebase, "user_files/"+user, [user]) + +c = datetime.datetime.now() +print(c-b) +# +# file = 'timetable-10001000.txt' +# user = ["Ishan"] +# fileReader(firebase, file, user) + + + + + + +#storage.child("hey/28809033.txt").download("288090331.txt") diff --git a/classes.json b/classes.json new file mode 100644 index 0000000..ce3089c --- /dev/null +++ b/classes.json @@ -0,0 +1 @@ +{"FIT1043*Lecture01*Wed*10:00*CL_21Col-E2*2*S2!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1045*Laboratory32*Thu*13:00*CL_23Col-G47*2*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1045*Lecture03-P1*Mon*17:00*CL_20Chn-H5*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1045*Lecture03-P2*Tue*16:00*CL_21Col-E1*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1045*PASSWorkshop04*Fri*15:00*CL_20Chn-E353*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1045*Tutorial12*Wed*09:00*CL_14Rnf-G16*2*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1047*Laboratory18*Thu*18:00*CL_14Rnf-G11A*2*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1047*Lecture02-P2*Wed*16:00*-*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1047*Lecture02-P1*Tue*14:00*-*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1047*PASSWorkshop02*Thu*11:00*CL_7Inn-CG22*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1050*Laboratory03*Wed*17:00*CL_22All-103*3*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1050*Lecture01*Mon*10:00*CL_21Col-E1*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "MAT1830*Lecture01-P2*Thu*12:00*CL_25Exh-C1*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A", "swhhZrPWz6bT8y117abMEoPcZ7E2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "MAT1830*Lecture01-P3*Fri*16:00*CL_25Exh-C1*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A", "swhhZrPWz6bT8y117abMEoPcZ7E2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "MAT1830*Lecture01-P1*Wed*12:00*CL_25Exh-C1*1*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A", "swhhZrPWz6bT8y117abMEoPcZ7E2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "MAT1830*Support-Class12*Tue*17:30*CL_7Inn-CG20*1~5*S1!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "MAT1841*Lecture01-P1*Mon*11:00*CL_16Rnf-S4*1*S2!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "MAT1841*Lecture01-P2*Wed*14:00*CL_15Rnf-S5*1*S2!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "MAT1841*Lecture01-P3*Thu*11:00*CL_15Rnf-S5*1*S2!!2017": {"users": {"QPjoAO7nrOVH4YQMP6WcLEVwhOW2": "A", "TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A", "uaBUroQsbWRiRBboI3iDct7HY892": "A"}}, "FIT1041*Debate01*Mon*08:00*CL_20Chn-H8 (24-7-18-9, 2-10-16-10), CL_10Chn-CG60 (24-7-18-9, 2-10-16-10)*1*S2!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "FIT1045*Laboratory25*Wed*18:00*CL_14Rnf-G11A*2*S1!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "FIT1045*Lecture02-P1*Mon*15:00*CL_21Col-E3*1*S1!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "FIT1045*Lecture02-P2*Tue*13:00*CL_21Col-E3*1*S1!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "FIT1045*Tutorial14*Tue*15:00*CL_14Rnf-G16*2*S1!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "FIT1047*Laboratory17*Thu*18:00*CL_14Rnf-147*2*S1!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "FIT1047*Lecture01-P1*Tue*14:00*CL_43Rnf-STH1*1*S1!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "FIT1047*Lecture01-P2*Wed*16:00*CL_43Rnf-STH1*1*S1!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "MAT1830*Support-Class10*Mon*16:30*CL_7Inn-CG11*1~5*S1!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "MGF1010*Workshop34*Mon*08:00*CA_F-F432*2*S1!!2017": {"users": {"TCAEsKIxqvSaI0FQzupsSNmjaIc2": "A"}}, "ACC1200*Seminar04*Wed*15:00*CL_7Inn-BG15*3*S1!!2017": {"users": {"swhhZrPWz6bT8y117abMEoPcZ7E2": "A"}}, "ETC1000*Comp-Lab09*Fri*08:00*CL_20Chn-E163*1~5*S1!!2017": {"users": {"swhhZrPWz6bT8y117abMEoPcZ7E2": "A"}}, "ETC1000*Lecture02*Fri*11:30*CL_25Exh-C1*1~5*S1!!2017": {"users": {"swhhZrPWz6bT8y117abMEoPcZ7E2": "A"}}, "FIT1045*Laboratory20*Fri*13:00*CL_23Col-G45*2*S1!!2017": {"users": {"swhhZrPWz6bT8y117abMEoPcZ7E2": "A"}}, "FIT1045*Lecture01-P1*Mon*13:00*CL_21Col-E3*1*S1!!2017": {"users": {"swhhZrPWz6bT8y117abMEoPcZ7E2": "A"}}, "FIT1045*Lecture01-P2*Tue*15:00*CL_21Col-E3*1*S1!!2017": {"users": {"swhhZrPWz6bT8y117abMEoPcZ7E2": "A"}}, "FIT1045*Tutorial16*Mon*14:00*CL_23Col-G46*2*S1!!2017": {"users": {"swhhZrPWz6bT8y117abMEoPcZ7E2": "A"}}, "MAT1830*Support-Class08*Mon*10:00*CL_7Inn-CG10*1~5*S1!!2017": {"users": {"swhhZrPWz6bT8y117abMEoPcZ7E2": "A"}}} \ No newline at end of file diff --git a/new_db_structure_firebase.py b/new_db_structure_firebase.py new file mode 100644 index 0000000..d8667a2 --- /dev/null +++ b/new_db_structure_firebase.py @@ -0,0 +1,272 @@ +import datetime +import json +import os.path + +import pyrebase + + + +""" +TODO: +""" +#TODO add file check (is legit text file) +#TODO only add semester 1 + + +from firebase import firebase +firebase = firebase.FirebaseApplication('https://learntogether-a250b.firebaseio.com/') + +config = { + 'apiKey': "AIzaSyBLA6sDqXwnMX6tJayM4dmYCPRzt5CW91M", + 'authDomain': "learntogether-a250b.firebaseapp.com", + 'databaseURL': "https://learntogether-a250b.firebaseio.com", + 'storageBucket': "learntogether-a250b.appspot.com", +} + + +pybase = pyrebase.initialize_app(config) + +auth = pybase.auth() + +# Log the user in + + +# Get a reference to the database service +db = pybase.database() + +if(user!=None): + + + allclasses = {} + if os.path.exists("classes.json"): + j = open("classes.json", 'r+') + allclasses = json.load(j) + if(allclasses == None): + allclasses = {} + j.close() + else: + allclasses = db.child("classes/").get(user['idToken']).val() + if (allclasses == None): + allclasses = {} + j = open("classes.json", 'w') + json.dump(allclasses, j) + j.close() + + + + storage = pybase.storage() + + def fileReader(firebase, file, curruservalue): + # print(curruservalue, "Hello excuse me") + try: + unitFile = open(file, 'r') + unitList = [] + + for line in unitFile: + unitList.append(line) + + unitFile.close() + unitStored = [] + + for i in range(1,len(unitList)): + unitLine = unitList[i].strip().split("\t") + unitName = unitLine[0].split("_") + if(unitName[2]!="S1" or unitName[2]=="S1"): + # if(True): + unitLine[0]=unitName[0] + unitStrip = [] + unitStrip.append(unitLine[0]) + unitStrip.append(unitLine[2]+unitLine[3]) + unitStrip.append(unitLine[4]+unitLine[5]) + unitStrip.append(unitLine[7].replace("/","-")) + unitStrip.append(unitLine[2]) + unitStrip.append(unitLine[3]) + if unitLine[9][1] == ".": + unitLine[9] = unitLine[9][:3] + else: + unitLine[9] = unitLine[9][0] + unitStrip.append(unitLine[9]) + unitStrip.append(unitName[2]+"!!2017") + unitStored.append(unitStrip) + + + unitOut = unitStored + + unitSimple = [] + unitSimpleTemp = [] + for unitRef in unitOut: + unitStore = unitRef[0]+"*"+unitRef[1]+"*"+unitRef[2][0:3]+"*"+unitRef[2][3:]+"*"+unitRef[3]+"*"+unitRef[-2].replace(".", "~") + "*" + unitRef[-1] + unitStoreTemp = unitRef[0]+"*"+unitRef[1]+"*"+unitRef[2][0:3]+"*"+unitRef[2][3:]+"*"+unitRef[3]+"*"+unitRef[-2].replace(".", "~") + "*" + unitRef[-1] + + # print(unitStore, unitStoreTemp) + + unitSimple.append(unitStore) + unitSimpleTemp.append(unitStoreTemp) + unit = unitSimple + + unitTemp = unitSimpleTemp + + + c = {} + for each in unitTemp: + c[each] = "S" + # print(c,"\n", user) + db.child("/users/").child(curruservalue[0]).child('classes').set(c, user['idToken']) + # firebase.put("", "users/" + str(user[0]) + "/classes", c) + + newUnit = [] + for p in range(len(unit)): + newUnit.append([]) + + for x in range(len(unit)): + newUnit[x].append(unitOut[x][0]) + newUnit[x].append(unitOut[x][4]) + newUnit[x].append(unitOut[x][5]+unitOut[x][2]) + + print(unit, newUnit) + + oldUsers = [] + for n in range(len(unit)): + oldUsers.append([]) + print(oldUsers, "\toldusers") + temp = [] + + + for x in range(len(unit)): + try: + # a = firebase.get('','classes/'+unit[x]+'/users/') + # print(unit[x]) + + + + if(allclasses.get(unit[x]) is None): + print("trueee") + allclasses[unit[x]] = {} + allclasses[unit[x]]['users'] = [] + print(allclasses) + + a = allclasses[unit[x]]['users'] + + print(a, "a") + + + temp.append(a) + + if temp[x] not in oldUsers[x] and temp[x] is not None: + oldUsers[x].extend(temp[x]) + + print(oldUsers, "2") + + if curruservalue[0] not in oldUsers[x]: + oldUsers[x].extend(curruservalue) + except Exception as e: + db.child("/requests/").child(str(curruservalue[0])).child('status').set('ERR' + str(e), + user['idToken']) + + for i in range(len(unit)): + print(oldUsers, "3") + sqwer = {} + for each in oldUsers[i]: + sqwer[each] = "A" + dictionary = {'users': sqwer} + db.child("classes/").child(str(unit[i])).set(dictionary, user['idToken']) + allclasses[str(unit[i])] = dictionary + # try: + # #a = firebase.get('','classes/'+unit[x]+'/users/') + # #print(unit[x]) + # + # + # ap = allclasses[unit[x]]['users'] + # a = [] + # if(ap is not None): + # for each in ap: + # a.append(each) + # + # + # + # temp.append(a) + # + # print("a\t", a, "\n", temp, " ", x, "\n", oldUsers) + # print() + # + # if temp[x] not in oldUsers[x] and temp[x] is not None: + # oldUsers[x].extend(temp[x]) + # + # if curruservalue[0] not in oldUsers[x]: + # oldUsers[x].extend(curruservalue) + # + # + # except Exception as e: + # pass + # # print(e) + # #db.child("/requests/").child(str(curruservalue[0])).child('status').set('ERR'+str(e), user['idToken']) + # # firebase.put('/', 'requests/' + str(user[0]) + "/status", 'ERR'+str(e)) + # + # for i in range(len(unit)): + # qc = {} + # print(oldUsers[i]) + # for each in oldUsers[i]: + # qc[each] = "P" + # dictionary = {'users':qc} + # print(dictionary) + # try: + # db.child("classes/").child(str(unit[i])).set(dictionary, user['idToken']) + # except Exception as e: + # print(e) + # # firebase.put("","classes/"+str(unit[i]),dictionary) + # allclasses[str(unit[i])] = dictionary + + j = open("classes.json", 'w') + json.dump(allclasses, j) + j.close() + + db.child("/requests/").child(str(curruservalue[0])).child('status').set('SUCCESS', user['idToken']) + # firebase.put('/', 'requests/' + str(user[0]) + "/status", 'SUCCESS') + #End of function + except Exception as e: + print(e) + db.child("/requests/").child(str(curruservalue[0])).child('status').set('ERR' + str(e), user['idToken']) + # firebase.put('/', 'requests/' + str(user[0]) + "/status", 'ERROR\t' + str(e)) + + + ### Main + + + requests = db.child("/requests/").get(user['idToken']).val() + + + #print(requests) + #Download all the files + + a = datetime.datetime.now() + print(a) + for request in requests: + if(requests[request]['status'] == "UPLOAD"): + storage.child("usertimetable/"+request).download("user_files/"+request) + db.child("/requests/").child(request).child('status').set('DOWNLOADED', user['idToken']) + # firebase.put('/','requests/'+request+"/status", 'DOWNLOADED') + b = datetime.datetime.now() + print(b, b-a) + + for request in requests: + if(requests[request]['status']!="SUCCESS"): + userp = request + fileReader(firebase, "user_files/"+userp, [userp]) + + # userp = "TCAEsKIxqvSaI0FQzupsSNmjaIc2" + # fileReader(firebase, "user_files/" + userp, [userp]) + c = datetime.datetime.now() + # print(c-b) +else: + print("Auth failed.") +# +# file = 'timetable-10001000.txt' +# user = ["Ishan"] +# fileReader(firebase, file, user) + + + + + + +#storage.child("hey/28809033.txt").download("288090331.txt") diff --git a/newbackend.py b/newbackend.py new file mode 100644 index 0000000..c5a3135 --- /dev/null +++ b/newbackend.py @@ -0,0 +1,139 @@ +#use this updated version +import pyrebase +""" +TODO: +""" +#TODO add file check (is legit text file) +#TODO only add semester 1 + + +from firebase import firebase +firebase = firebase.FirebaseApplication('https://learntogether-a250b.firebaseio.com/') + +config = { + 'apiKey': "AIzaSyBLA6sDqXwnMX6tJayM4dmYCPRzt5CW91M", + 'authDomain': "learntogether-a250b.firebaseapp.com", + 'databaseURL': "https://learntogether-a250b.firebaseio.com", + 'storageBucket': "learntogether-a250b.appspot.com", +} + + +pybase = pyrebase.initialize_app(config) + + +storage = pybase.storage() + +def fileReader(firebase, file, user): + unitFile = open(file, 'r') + unitList = [] + + for line in unitFile: + unitList.append(line) + + unitFile.close() + unitStored = [] + + for i in range(1,len(unitList)): + unitLine = unitList[i].strip().split("\t") + unitName = unitLine[0].split("_") + unitLine[0]=unitName[0] + unitStrip = [] + unitStrip.append(unitLine[0]) + unitStrip.append(unitLine[2]+unitLine[3]) + unitStrip.append(unitLine[4]+unitLine[5]) + unitStrip.append(unitLine[7].replace("/","-")) + unitStrip.append(unitLine[2]) + unitStrip.append(unitLine[3]) + if unitLine[9][1] == ".": + unitLine[9] = unitLine[9][:3] + else: + unitLine[9] = unitLine[9][0] + unitStrip.append(unitLine[9]) + unitStored.append(unitStrip) + + + unitOut = unitStored + + unitSimple = [] + unitSimpleTemp = [] + for unitRef in unitOut: + unitStore = unitRef[0]+"*"+unitRef[1]+"*"+unitRef[2][0:3]+"*"+unitRef[2][3:]+"*"+unitRef[3]+"*"+unitRef[-1].replace(".", "~") + unitStoreTemp = unitRef[0]+"*"+unitRef[1]+"*"+unitRef[2][0:3]+"*"+unitRef[2][3:]+"*"+unitRef[3]+"*"+unitRef[-1].replace(".", "~") + + # print(unitStore, unitStoreTemp) + + unitSimple.append(unitStore) + unitSimpleTemp.append(unitStoreTemp) + unit = unitSimple + #print(unit) + #print() + unitTemp = unitSimpleTemp + + + firebase.put("", "users/" + str(user[0]) + "/classes", unitTemp) + + newUnit = [] + for p in range(len(unit)): + newUnit.append([]) + + for x in range(len(unit)): + newUnit[x].append(unitOut[x][0]) + newUnit[x].append(unitOut[x][4]) + newUnit[x].append(unitOut[x][5]+unitOut[x][2]) + + #print(unit, newUnit) + + oldUsers = [] + for n in range(len(unit)): + oldUsers.append([]) + temp = [] + + for x in range(len(unit)): + try: + a = firebase.get('','classes/'+unit[x]+'/users/') + #print(unit[x]) + + temp.append(a) + + if temp[x] not in oldUsers[x] and temp[x] is not None: + oldUsers[x].extend(temp[x]) + + if user[0] not in oldUsers[x]: + oldUsers[x].extend(user) + except Exception as e: + firebase.put('/', 'requests/' + str(user[0]) + "/status", 'ERR'+str(e)) + + for i in range(len(unit)): + dictionary = {'location': unitOut[i][3], 'time': unitOut[i][2], 'unitID': unitOut[i][0], 'group':unitOut[i][1],'users':oldUsers[i]} + firebase.put("","classes/"+str(unit[i]),dictionary) + + firebase.put('/', 'requests/' + str(user[0]) + "/status", 'SUCCESS') + #End of function + + +### Main + + +requests = firebase.get('/','requests/') +#print(requests) +#Download all the files +for request in requests: + if(requests[request]['status'] == "UPLOAD"): + storage.child("usertimetable/"+request).download("user_files/"+request) + firebase.put('/','requests/'+request+"/status", 'DOWNLOADED') + +for request in requests: + if(requests[request]['status']!="SUCCESS"): + user = request + fileReader(firebase, "user_files/"+user, [user]) +# +# file = 'timetable-10001000.txt' +# user = ["Ishan"] +# fileReader(firebase, file, user) + + + + + + +#storage.child("hey/28809033.txt").download("288090331.txt") diff --git a/seperate_unit_types.py b/seperate_unit_types.py new file mode 100644 index 0000000..3da7367 --- /dev/null +++ b/seperate_unit_types.py @@ -0,0 +1,49 @@ +import json +classes = json.load(open("classes.json")) + +upload = {} + +def GetTextOnly(s): + return ''.join([i for i in s if not i.isdigit()]) + + +for cl in classes: + cll = cl.split("*") + #check if the unit is present + unit = cll[0] + if(unit not in upload): + upload[unit] = {} + type = GetTextOnly(cll[1]) + if(type not in upload[unit]): + upload[unit][type] = [] + upload[unit][type].append(cl) + +pwd = "ishan4899qwert123" +import pyrebase + +config = { + 'apiKey': "AIzaSyBLA6sDqXwnMX6tJayM4dmYCPRzt5CW91M", + 'authDomain': "learntogether-a250b.firebaseapp.com", + 'databaseURL': "https://learntogether-a250b.firebaseio.com", + 'storageBucket': "learntogether-a250b.appspot.com", +} + +firebase = pyrebase.initialize_app(config) + + +auth = firebase.auth() + +# Log the user in +user = auth.sign_in_with_email_and_password("ishan@ishanjoshi.me", pwd) +print(user) + +# Get a reference to the database service +db = firebase.database() + +db.child('seperate_unit_list/').set(upload, user['idToken']) + + + + + + diff --git a/swap.py b/swap.py new file mode 100644 index 0000000..6b96d57 --- /dev/null +++ b/swap.py @@ -0,0 +1,48 @@ +import networkx as nx +from firebase import firebase + +firebase = firebase.FirebaseApplication('https://learntogether-a250b.firebaseio.com/') +swaps = firebase.get("", "/swaps/unitcode/typeofactivity/") + +edgesindex = {} +listofgraphtuple = [] + +for each in swaps: + edgekeys = [] + edgekey = "" + currentedge = swaps[each][0] + for i in range(1,len(swaps[each])): + edgekey = currentedge + swaps[each][i] + listofgraphtuple.append((currentedge, swaps[each][i])) + edgekeys.append(edgekey) + edgekey = "" + for key in edgekeys: + edgesindex[key] = each + #print (edgekeys) + +print(edgesindex) +print(listofgraphtuple) + +G = nx.DiGraph(listofgraphtuple) +solution = list(nx.simple_cycles(G)) + +for x in solution: + x+=[x[0]] +print(solution) + +useraction = [] +for solutionz in solution: + actions = [] + for i in range(len(solutionz) - 1): + actions.append(edgesindex[solutionz[i]+solutionz[i+1]]) + useraction.append(actions) +print(useraction) + +""" +OUTPUT: +{'8pm4pm': 'uid', '4pm8pm': 'uid1', '4pm6pm': 'uid2', '6pm8pm': 'uid3', '6pm4pm': 'uid3', '6pm10am': 'uid3'} +[('8pm', '4pm'), ('4pm', '8pm'), ('4pm', '6pm'), ('6pm', '8pm'), ('6pm', '4pm'), ('6pm', '10am')] +[['6pm', '4pm', '6pm'], ['6pm', '8pm', '4pm', '6pm'], ['4pm', '8pm', '4pm']] +[['uid3', 'uid2'], ['uid3', 'uid', 'uid2'], ['uid1', 'uid']] +""" +