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_jeevanai_report_server import create_interactive_report from datetime import date 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/"}, name="app1", ) app2 = firebase_admin.initialize_app( cred, {"databaseURL": "https://farmbase-b2f7e-60c5a.firebaseio.com/"}, name="app2", ) except: print(traceback.format_exc()) dbF = firestore.client(app1) storage_client = storage.Client(project="farmbase-b2f7e") bucket_name = "farmbase-b2f7e.appspot.com" db_firestore = firestore.client(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) temp_main_obj = temp_d.to_dict() temp_main_obj["Processed"] = 1 print(uid) is_admin = ( db.reference("SalesAssociatesPromo", app=app1) .child("AdminUIDs") .child(uid) .get() ) if (is_admin) or (uid in disabledUIDs): print("admin") # Read the content of the HTML file template_file_name = "demo_report_jeevanai.html" output_file_name = uid + "/interactive_report.html" with open(template_file_name, "r", encoding="utf-8") as file: content_str = file.read() # Save the final report with open(output_file_name, "w") as f: f.write(content_str) # upload report upload_report(uid) # Change flag db_firestore.collection("InteractiveReportRequests").document( uid ).set(temp_main_obj) else: print("normal") 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(False, False) ) if sensed_days_obj is not None: print(temp_d.id) print(temp_d.to_dict()) # uid = temp_main_obj.get("UID", None) create_interactive_report(uid) upload_report(uid) db_firestore.collection("InteractiveReportRequests").document( uid ).set(temp_main_obj) 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: create_interactive_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