from google.cloud import storage import time import firebase_admin from firebase_admin import credentials from firebase_admin import db import traceback import sys import json import requests import time from firebase_admin import firestore from send_moni_noti import send_moni_noti from create_interactive_html_report import make_interactive_html_report session_type = 0 try: session_type = sys.argv[1] except: print(traceback.format_exc()) cred = credentials.Certificate("servicekey.json") # os.system("rm -rf AwsData") try: app1 = firebase_admin.initialize_app( cred, {"databaseURL": "https://farmbase-b2f7e-31c0c.firebaseio.com/"}, app = 'app1' ) app2 = firebase_admin.initialize_app( cred, {"databaseURL": "https://farmbase-b2f7e-60c5a.firebaseio.com/"}, app = 'app2' ) except: qysy = 1 dbF = firestore.client(app=app1) storage_client = storage.Client(project='farmbase-b2f7e', app = app1) bucket_name = "farmbase-b2f7e.appspot.com" db_firestore = firestore.client(app=app1) disabledUIDs = [ "snQYQZqQx3SmVbRztmEqYn5Mkcz2", "KZQ7TZIYXnXN0b07OtrL1hlyYij1", "CeMGYvLXrGR5ZThxZ46iV7vY8sa2", "TCXcp5VIsfhHZrh0nm2VsgBtcGy2", "mFFHQdEtiSbn2hbYQAwwoIdYVi02" ] def upload_report(uid): bucket = storage_client.get_bucket(bucket_name) user_name = db.reference(uid,app=app2).child('MyProfile').child('Name').get() if user_name is None: user_name = '' blob = bucket.blob(("PaidMonitoredFields/" + uid + "/interactive_report.html")) blob.upload_from_filename((uid + "/interactive_report.html")) req_obj = { "UID": uid, "notification_body": "Hi " + user_name + ", the new consolidated report for your fields is now ready. Visit the \"Reports\" section of the app to view the details!", "notification_title": "Your New Consolidated Report is Ready!" } headers = {'Content-type': 'application/json'} url = 'https://us-central1-farmbase-b2f7e.cloudfunctions.net/send_fcm_notification' response = requests.post(url, data=json.dumps(req_obj), headers=headers) def instant_session(): #todo a = 1 #print(a) while a ==1: #print(a) report_requests_obj = db_firestore.collection(u'InteractiveReportRequests').where('Processed','==',0).get() #print(report_requests_obj) try: for temp_d in report_requests_obj: uid = temp_d.id uid = str(uid) print(uid) fields_arr = [] field_list_obj = db.reference('PaidMonitoredFields',app=app1).child('PMF').child(uid).get(False,False) for field_id, val in field_list_obj.items(): fields_arr.append(int(field_id)) latest_field = str(max(fields_arr)) sensed_days_obj = db.reference('PaidMonitoredFields', app=app1).child('PMF').child(uid).child(latest_field).child('SensedDays').get() if sensed_days_obj is not None: print(temp_d.id) print(temp_d.to_dict()) temp_main_obj = temp_d.to_dict() #uid = temp_main_obj.get("UID", None) make_interactive_html_report(uid) temp_main_obj["Processed"] = 1 db_firestore.collection(u'InteractiveReportRequests').document(uid).set(temp_main_obj) upload_report(uid) except: print(traceback.format_exc()) time.sleep(60) def daily_session(): while True: uid_list = db.reference('PaidMonitoredFields', app = app1).child('PMF').get(False, True) for uid, val in uid_list.items(): if uid not in disabledUIDs: try: make_interactive_html_report(uid) upload_report(uid) except: print(traceback.format_exc()) time.sleep((5*86400)) print(session_type) if session_type == '0': #print('ee') instant_session() elif session_type == '1': daily_session() else: a = 1