import json import firebase_admin from firebase_admin import credentials, db import traceback def getDictFromFile(path): with open(path, "r", encoding="utf-8") as f: return json.loads(f.read()) def saveJsonDataToFile(path, data): with open(path, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=4) def initFirebaseApp(): credPath = "mess.json" dbUrl = "https://farmbase-b2f7e-31c0c.firebaseio.com/" cred = credentials.Certificate(credPath) firebase_admin.initialize_app(cred, {"databaseURL": dbUrl}) def get_only_keys_frompath(path): ref = db.reference(path) data = ref.get(False, True) return data def get_data_from_path(path): ref = db.reference(path) data = ref.get() return data ## Connet to Firebase initFirebaseApp() all_polygons_fnIds_path = "allPolygonFnIds.json" PolygonfnIDs_list = getDictFromFile(all_polygons_fnIds_path) all_visit_dates = {} empty_polygon_fnIDs = [] i = 0 for fieldfnID, PolygonfnIDs in PolygonfnIDs_list.items(): for polygonfnID in PolygonfnIDs: visit_dates_path = ( "PaidMonitoredFields/PMF/TCXcp5VIsfhHZrh0nm2VsgBtcGy2/" + fieldfnID + "/Polygons/" + polygonfnID + "/Health" ) #data = get_only_keys_frompath(visit_dates_path) indices_data = get_data_from_path(visit_dates_path) # print(indices_data) if indices_data: # for indices_day, indices_values in indices_data.items(): if fieldfnID not in all_visit_dates: all_visit_dates[fieldfnID] = {} if polygonfnID not in all_visit_dates[fieldfnID]: all_visit_dates[fieldfnID][polygonfnID] = [] # print(fieldfnID,polygonfnID,indices_data) all_visit_dates[fieldfnID][polygonfnID] = indices_data else: empty_polygon_fnIDs.append(polygonfnID) print("No visit Data for", polygonfnID) # if data: # if fieldfnID not in all_visit_dates: # all_visit_dates[fieldfnID] = {} # if polygonfnID not in all_visit_dates[fieldfnID]: # all_visit_dates[fieldfnID][polygonfnID] = [] # all_visit_dates[fieldfnID][polygonfnID].extend(list(data.keys())) # else: # empty_polygon_fnIDs.append(polygonfnID) # print("No visit Data for", polygonfnID) if i % 500 == 0: print(i) # Save the data to the JSON file # all_polygons_visit_dates_path = "allvisitdates.json" # saveJsonDataToFile(all_polygons_visit_dates_path, all_visit_dates) i += 1 # Save the remaining data to the JSON file all_polygons_visit_dates_path = "allvisitdates.json" saveJsonDataToFile(all_polygons_visit_dates_path, all_visit_dates)