from google.oauth2 import service_account import datetime import traceback import requests import json from utils import mLogger from gen_radar_hybrid import create_hybrid_image companyDomainNameToCodeName = { "linxag.ng": "linxag", } def gen_report_new( uid, field_address, center_lat, center_long, imagedate, fieldid, field_area, lang, failed, whitelabel, rangeLatLng, points, polygonID=None, ndviAvgVal=0, ndwiAvgVal=0, fieldOrPolyData=None, ): import pdfkit import io import codecs import report_html_filler as filler from create_bargraph import create_bargraph from create_linegraph import create_linegraph import firebase_admin from firebase_admin import credentials from firebase_admin import db from firebase_admin import messaging import time import datetime from google.cloud import storage from get_weather_data import get_weather_data from datetime import date from create_temperature_bargraph import create_temperature_bargraph from create_weather_line_graph import create_weather_line_graph import cv2 from PIL import Image from create_index_piechart import create_index_piechart from get_weather_forecast import get_weather_forecast from column_img import column_img import csv import os.path from upload_file import upload_file_to_storage from pyvirtualdisplay import Display from pdf_compressor import compress from geopy.geocoders import Nominatim from create_instructions import ( getRedZones, createFieldDirImg, getInstructionsByZones, isCloudyImg, getSmsFullInstructionsByZones, createDirOnMapImg, ) from getMapByCoordinates import getFieldMapImg from send_whatsapp import send_whatsapp # from docx_to_pdf import docx_to_pdf # Fetch the service account key JSON file contents try: cred = credentials.Certificate("servicekey.json") try: firebase_admin.initialize_app( cred, {"databaseURL": "https://farmbase-b2f7e-31c0c.firebaseio.com/"} ) except: print("fire running") try: fireApp2 = firebase_admin.initialize_app( cred, {"databaseURL": "https://farmbase-b2f7e-60c5a.firebaseio.com/"}, "fireApp2", ) except: fireApp2 = firebase_admin.get_app("fireApp2") # whatsappSmsCompanies = { # "ITC": { # # sehore1/2, # "UIDs": ["fSRlyKzCxxMJHJqDzpGhQImyyxf2", "rF4qo7DGoxd3AeEC1Ux4vBGbsbl1"], # "defaultSmsLang": "hi", # "isDefaultLangCompulsory": True, # "serveCompanyName": "आईटीसी मार्स", # }, # "RohaFarmsLLP": { # # rohafarmsllp@gmail.com, # "UIDs": ["XSzwRsmzu4OeCm8UvllGn7xsnOk1"], # "defaultSmsLang": "en", # "isDefaultLangCompulsory": True, # "fieldIDs": [ # "1663590354626", # "1663868427256", # "1663868432667", # "1663868434413", # "1663868438991", # "1663868440996", # "1663868442297", # "1663868443515", # "1663868444633", # "1663868445670", # ], # }, # "Godrej":{ # #yaswanth.tippireddy@godrejagrovet.com # "UIDs": ["TCXcp5VIsfhHZrh0nm2VsgBtcGy2"], # "defaultSmsLang": "en", # "isDefaultLangCompulsory": True, # } # } # whatsappSmsCompany = None # for key, company in whatsappSmsCompanies.items(): # if uid in company["UIDs"]: # whatsappSmsCompany = company # break whatsappEnabled = fieldOrPolyData.get("isWhatsappEnabled", None) fSmsLang = fieldOrPolyData.get("smsLang", None) userData = db.reference(f"{uid}/MyProfile", fireApp2).get() if userData is not None: # user data present in DB # isGodrej = uid in whatsappSmsCompanies["Godrej"]["UIDs"] companyCode = ( userData["CompanyCode"] if "CompanyCode" in userData else "Farmonaut" ) else: # no user data present; check by whitelabel name whitelabelCodeName = companyDomainNameToCodeName.get(whitelabel) userData = {} companyCode = whitelabelCodeName or "Farmonaut" isGodrej = companyCode == "Godrej" orgReportData = db.reference(f"OrganisationData/{companyCode}/ReportData").get() addLogo = orgReportData.get("addLogo", True) dynamicLinkDomain = orgReportData.get("dynamicLinkDomain", "https://farmonaut.page.link") expertNumber = orgReportData.get("expertNumber", "6366026267") isPolygon = polygonID != None # if isPolygon: # orgSmsData = { # "defaultSmsLang": "en", # } # geolocator = Nominatim(user_agent="geoapiExercises") # location = geolocator.reverse(str(center_lat)+"," + str(center_long)) # address_obj = location.raw['address'] # temp_field_address = address_obj.get('suburb','') + ", " + address_obj.get('city','') + ", " + address_obj.get('state','') + ", " + address_obj.get('country','') temp_field_address = field_address storage_client = storage.Client() bucket_name = "farmbase-b2f7e.appspot.com" all_langs = lang.split(",") # hasDefaultSmsLang = None if orgSmsData == None else orgSmsData[ # "defaultSmsLang"] in all_langs if whatsappEnabled == None: whatsappEnabled = userData.get( "isWhatsappEnabled", False ) or orgReportData.get("isWhatsappEnabled", False) print(["langg1s: ", fSmsLang, whatsappEnabled, userData, orgReportData]) if whatsappEnabled: fSmsLang = ( fSmsLang or userData.get("defaultSmsLang", None) or orgReportData.get("defaultSmsLang", None) ) print(["langgs: ", fSmsLang]) isSmsLangInReportLangs = fSmsLang in all_langs today = date.today() date_d = today.strftime("%Y%m%d") year = date_d[0:4] month = date_d[4:6] day = date_d[6:] report_date = year + "-" + month + "-" + day + " (in yyyy-mm-dd)" sensed_year = imagedate[0:4] sensed_month = imagedate[4:6] sensed_day = imagedate[6:] sensed_imagedate = ( sensed_year + "-" + sensed_month + "-" + sensed_day + " (in yyyy-mm-dd)" ) sensed_day = int(sensed_day) sensed_month = int(sensed_month) print("report_date") print(report_date) print(sensed_imagedate) if isPolygon: etci_image = "%s/%s/%s_TCI.png" % (uid, fieldid, polygonID) ndvi_image = "%s/%s/%s_NDVI.png" % (uid, fieldid, polygonID) ndwi_image = "%s/%s/%s_NDWI.png" % (uid, fieldid, polygonID) rvi_image = "%s/%s/%s_RVI.png" % (uid, fieldid, polygonID) rsm_image = "%s/%s/%s_RSM.png" % (uid, fieldid, polygonID) ndwi_image_cmap2 = "%s/%s/%s_NDWI_CMAP2.png" % (uid, fieldid, polygonID) ndvi_image_cmap2 = "%s/%s/%s_NDVI_CMAP2.png" % (uid, fieldid, polygonID) # rvi_image_cmap2 = "%s/%s/%s_RVI_CMAP2.png" % (uid, fieldid, polygonID) # rsm_image_cmap2 = "%s/%s/%s_RSM_CMAP2.png" % (uid, fieldid, polygonID) recl_image = "%s/%s/%s_RECL.png" % (uid, fieldid, polygonID) recl_image_cmap2 = "%s/%s/%s_RECL_CMAP2.png" % (uid, fieldid, polygonID) dir_img = "%s/%s/%s_dirImg.png" % (uid, fieldid, polygonID) map_img = "%s/%s/%s_mapImg.jpg" % (uid, fieldid, polygonID) bdy_img = "%s/%s/%s_bdyImg.jpg" % (uid, fieldid, polygonID) ndre_image = "%s/%s/%s_NDRE.png" % (uid, fieldid, polygonID) ndre_image_cmap2 = "%s/%s/%s_NDRE_CMAP2.png" % (uid, fieldid, polygonID) else: etci_image = "ETCI.png" ndvi_image = "ndvi.png" ndwi_image = "ndwi.png" rvi_image = "rvi.png" rsm_image = "rsm.png" ndwi_image_cmap2 = "ndwi_cmap2.png" ndvi_image_cmap2 = "ndvi_cmap2.png" # rvi_image_cmap2 = "%s/%s/rvi_cmap2.png" # rsm_image_cmap2 = "%s/%s/rsm_cmap2.png" dir_img = "dirImg.png" map_img = "mapImg.jpg" bdy_img = "bdyImg.jpg" recl_image = "recl.png" recl_image_cmap2 = "recl_cmap2.png" ndre_image = "ndre.png" ndre_image_cmap2 = "ndre_cmap2.png" tci_image = "TCI.png" evi_image = "evi.png" savi_image = "savi.png" ndmi_image = "ndmi.png" hybrid_image = "hybrid.png" hybrid_blind_image = "hybrid_blind.png" dem_image = "dem.png" soc_image = "soc.png" evapo_image = "evapo.png" evi_image_cmap2 = "evi_cmap2.png" savi_image_cmap2 = "savi_cmap2.png" soc_image_cmap2 = "soc_cmap2.png" ndmi_image_cmap2 = "ndmi_cmap2.png" fieldLatLngs = { "minLat": rangeLatLng[0], "minLng": rangeLatLng[1], "maxLat": rangeLatLng[2], "maxLng": rangeLatLng[3], "centerLat": center_lat, "centerLng": center_long, } print(["pp1", points, fieldLatLngs, map_img]) mapImgDimens = db.reference(f"PaidMonitoredFields/PMF/{uid}/{fieldid}/MapDimensions").get() mapImgPresentInStorage = checkIfStorageFilePresent(uid, fieldid, "default", "mapImg.jpg", storage_client) createMapImg = mapImgDimens == None or (not mapImgPresentInStorage) if not createMapImg: # Field Map Img is already present outPath = downloadStorageFileToPath(uid, fieldid, "default", "mapImg.jpg", storage_client, map_img) createMapImg = outPath == None if createMapImg: # Field Map Img is not present; create and upload mapImgDimens = getFieldMapImg(points, fieldLatLngs, map_img) db.reference(f"PaidMonitoredFields/PMF/{uid}/{fieldid}/MapDimensions").set(mapImgDimens) upload_file_to_storage(uid, fieldid, "default", map_img, "mapImg.jpg") isNDVIPresent = os.path.exists(ndvi_image) try: isCloudy = isCloudyImg(tci_image, ndvi_image) if isNDVIPresent else True # create_hybrid_image(ndvi_image, ndwi_image, (uid + '/' + fieldid + '/hybrid.png')) except: isCloudy = True # create_hybrid_image(rvi_image, rsm_image, (uid + '/' + fieldid + '/hybrid.png')) if isCloudy == True: create_hybrid_image((uid + '/' + fieldid + '/rvi.png'), (uid + '/' + fieldid + '/rsm.png'), (uid + '/' + fieldid + '/hybrid.png')) else: create_hybrid_image((uid + '/' + fieldid + '/ndvi.png'), (uid + '/' + fieldid + '/ndwi.png'), (uid + '/' + fieldid + '/hybrid.png')) isSmallPlant = ndviAvgVal < 0.4 and ndwiAvgVal < 0.4 isRVIPresent = os.path.exists(rvi_image) if isRVIPresent: rviRedZones = getRedZones(rvi_image) rsmRedZones = getRedZones(rsm_image) # rvi2RedZones = getRedZones(rvi_image_cmap2) # rsm2RedZones = getRedZones(rsm_image_cmap2) if isNDVIPresent: ndviRedZones = getRedZones(ndvi_image) ndvi2RedZones = getRedZones(ndvi_image_cmap2, True) ndwiRedZones = getRedZones(ndwi_image) ndwi2RedZones = getRedZones(ndwi_image_cmap2, True) socRedZones = getRedZones(soc_image) soc2RedZones = getRedZones(soc_image_cmap2, True) # save data to DB irriZones = ( ( rsmRedZones # if not isSmallPlant else rsm2RedZones) if isRVIPresent else None ) if isCloudy else ( (ndwiRedZones if not isSmallPlant else ndwi2RedZones) if isNDVIPresent else None ) ) cropZones = ( ( rviRedZones # if not isSmallPlant else rvi2RedZones) if isRVIPresent else None ) if isCloudy else ( (ndviRedZones if not isSmallPlant else ndvi2RedZones) if isNDVIPresent else None ) ) soilZones = ( (socRedZones if not isSmallPlant else soc2RedZones) if isNDVIPresent else None ) # Initialize the app with a service account, granting admin privileges # whitelabel = "nothing" health = ( db.reference("PaidMonitoredFields") .child("PMF") .child(uid) .child(fieldid) .child("Health") .get() ) ndvi = try_index(health, "ndvi") evi = try_index(health, "evi") ndwi = try_index(health, "ndwi") ndre = try_index(health, "ndre") ndmi = try_index(health, "ndmi") savi = try_index(health, "savi") soc = try_index(health, "soc") # if lang == 'ar': # alignmentType = WD_ALIGN_PARAGRAPH.RIGHT # else: # alignmentType = WD_ALIGN_PARAGRAPH.LEFT header_img_url = "APIOrganizations/" + uid + ".jpeg" if os.path.isfile(header_img_url): header_img_url = "https://farmonaut.com/Images/" + uid + ".jpeg" else: header_img_url = "APIOrganizations/" + whitelabel + ".jpg" if os.path.isfile(header_img_url): header_img_url = orgReportData.get("logoUrl", "https://farmonaut.com/Images/" + whitelabel + ".jpg") else: header_img_url = "https://farmonaut.com/Images/Farmonaut_Logo_1.jpg" f_lat = str(center_lat) f_lat = f_lat[0:6] f_long = str(center_long) f_long = f_long[0:6] cur_date = imagedate cur_weather_data = ( db.reference("PaidMonitoredFields") .child("PMF") .child(uid) .child(fieldid) .child("Weather") .child(cur_date) .get() ) try: min_temp = cur_weather_data["min_temp"] except: min_temp = None past_weather_url = "https://farmonaut.com/Images/data_not_found.jpg" # min_temp = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('min_temp').get() if min_temp is None: print("Weather data not available") min_temp = "not available" max_temp = "not available" humidity = "not available" pressure = "not available" station = "not available" wind_speed = "not available" wind_deg = "not available" cloud_cover = "not available" else: min_temp = float(min_temp) - 273 min_temp = str(min_temp) min_temp = min_temp[0:5] max_temp = cur_weather_data["max_temp"] # max_temp = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('max_temp').get() max_temp = float(max_temp) - 273 max_temp = str(max_temp) max_temp = max_temp[0:5] humidity = cur_weather_data["humidity"] # humidity = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('humidity').get() pressure = cur_weather_data["pressure"] # pressure = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('pressure').get() station = cur_weather_data["station"] # station = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('station').get() wind_speed = cur_weather_data["wind_speed"] # wind_speed = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('wind_speed').get() try: wind_deg = cur_weather_data["wind_deg"] except: wind_deg = "Not Available" # wind_deg = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(fieldid).child('Weather').child(cur_date).child('wind_deg').get() try: cloud_cover = cur_weather_data["cloud_cover"] except: cloud_cover = "Not Available" weather_data = ( db.reference("PaidMonitoredFields") .child("PMF") .child(uid) .child(fieldid) .child("Weather") .get() ) for langIndex in range(len(all_langs)): lang = all_langs[langIndex] lang = lang.replace(" ", "") lang_num = 0 allReportValues = { "addressValueH6": temp_field_address, "fieldAreaValue": field_area, } all_strings = [] row_num = 0 print(lang) all_strings.append(lang) try: reportStrings = db.reference("ReportStrings2022").child(lang).get() except: reportStrings = db.reference("ReportStrings2022").child("en").get() # print(reportStrings) for i in reportStrings: all_strings.append(i) # print(all_strings) print(all_strings[5]) allReportValues["generationDateValueH6"] = year + "-" + month + "-" + day allReportValues["visitDateValueH6"] = ( str(sensed_year) + "-" + str(sensed_month) + "-" + str(sensed_day) ) allReportValues["rgbTCIImg"] = tci_image allReportValues["rgbETCIImg"] = uid + '/' + fieldid + '/' + etci_image allReportValues["colorBlindImg"] = hybrid_blind_image allReportValues["evapoImg"] = evapo_image allReportValues["decisionTreeBasicImg"] = hybrid_image allReportValues["ndviSmallHeightImg"] = ndvi_image_cmap2 allReportValues["eviGoodHeightImg"] = evi_image allReportValues["eviSmallHeightImg"] = evi_image_cmap2 allReportValues["ndwiGoodHeightImg"] = ndwi_image allReportValues["ndwiSmallHeightImg"] = ndwi_image_cmap2 allReportValues["ndreGoodHeightImg"] = ndre_image allReportValues["ndreSmallHeightImg"] = ndre_image_cmap2 allReportValues["saviGoodHeightImg"] = savi_image allReportValues["saviSmallHeightImg"] = savi_image_cmap2 allReportValues["ndmiGoodHeightImg"] = ndmi_image allReportValues["ndmiSmallHeightImg"] = ndmi_image_cmap2 if uid == "M53J9SImW9My4vghlCaaWLYxsc22": lang = "hi" # Create and save the field dir img in language lang to storage createFieldDirImg((uid + '/' + fieldid + '/'+ etci_image), dir_img, reportStrings, lang) dirImgStoragePath = "dir_img_%s.png" % lang if not checkIfStorageFilePresent( uid, fieldid, "default", dirImgStoragePath, storage_client ): upload_file_to_storage(uid, fieldid, "default", dir_img, dirImgStoragePath) allReportValues["decisionTreeFieldDirImg"] = dir_img allReportValues["isPolygon"] = isPolygon allReportValues["isCloudy"] = isCloudy allReportValues["isSmallPlant"] = isSmallPlant if mapImgDimens != None: allReportValues["mapImgSize"] = (mapImgDimens.get("width"), mapImgDimens.get("height")) allReportValues["fieldImgPaddings"] = (mapImgDimens.get("horiPadPer"), mapImgDimens.get("verPadPer")) allReportValues["mapImg"] = map_img allReportValues["demGoodHeightImg"] = dem_image # allReportValues["demSmallHeightImg"] = dem_image_cmap2 allReportValues["socGoodHeightImg"] = soc_image allReportValues["socSmallHeightImg"] = soc_image_cmap2 if isRVIPresent: allReportValues["rviImg"] = rvi_image allReportValues["soilMoistImg"] = rsm_image allReportValues["decisionTreeCloudyIrriP"] = getInstructionsByZones( rsmRedZones, reportStrings, True, lang ) allReportValues["decisionTreeCloudyCropP"] = getInstructionsByZones( rviRedZones, reportStrings, False, lang ) if isNDVIPresent: allReportValues["ndviGoodHeightImg"] = ndvi_image allReportValues["decisionTreeLaterIrriP"] = getInstructionsByZones( ndwiRedZones, reportStrings, True, lang ) allReportValues["decisionTreeEarlyIrriP"] = getInstructionsByZones( ndwi2RedZones, reportStrings, True, lang ) allReportValues["decisionTreeLaterCropP"] = getInstructionsByZones( ndviRedZones, reportStrings, False, lang ) allReportValues["decisionTreeEarlyCropP"] = getInstructionsByZones( ndvi2RedZones, reportStrings, False, lang ) if isGodrej: isRECLPresent = os.path.exists(recl_image) if isRECLPresent: allReportValues["reclGoodHeightImg"] = recl_image allReportValues["reclSmallHeightImg"] = recl_image_cmap2 allReportValues["isOilPalm"] = True allReportValues["fieldBdyImg"] = bdy_img allReportValues["companyLogoImg"] = header_img_url allReportValues["latValue"] = f_lat allReportValues["longValue"] = f_long # weather allReportValues["weatherValueStn"] = station allReportValues["weatherValuePressure"] = pressure allReportValues["weatherValueCloud"] = cloud_cover allReportValues["weatherValueTempMin"] = min_temp allReportValues["weatherValueTempMax"] = max_temp allReportValues["weatherValueWind"] = wind_speed allReportValues["weatherValueWindDir"] = wind_deg allReportValues["weatherValueHumidity"] = humidity if weather_data is None: print("weather data not available yet") else: cloud_array = [] humidity_array = [] max_temp_array = [] min_temp_array = [] pressure_array = [] wind_deg_array = [] wind_speed_array = [] dates_array = [] for key, val in weather_data.items(): weather_date = key date_data = weather_data[weather_date] dates_array.append(weather_date) dates_array.sort() for key in dates_array: weather_date = key date_data = weather_data[weather_date] try: cloud_array.append(date_data["cloud_cover"]) except: cloud_array.append("not availabe") try: humidity_array.append(date_data["humidity"]) except: humidity_array.append("not available") try: t_max_temp = date_data["max_temp"] - 273 except: t_max_temp = "not available" try: t_min_temp = date_data["min_temp"] - 273 except: t_min_temp = "not available" max_temp_array.append(t_max_temp) min_temp_array.append(t_min_temp) try: pressure_array.append(date_data["pressure"]) except: pressure_array.append("not available") try: wind_deg_array.append(date_data["wind_deg"]) except: wind_deg_array.append("not available") try: wind_speed_array.append(date_data["wind_speed"]) except: wind_speed_array.append("not available") if len(dates_array) > 1: try: line_types = [ "min_temp", "max_temp", "cloud_cover", "humidity", "pressure", "wind_speed", ] weather_array = [ min_temp_array, max_temp_array, cloud_array, humidity_array, pressure_array, wind_speed_array, ] create_weather_line_graph( uid, line_types, weather_array, dates_array, fieldid ) allReportValues["weatherGraphImg"] = ( "weather_linegraph.png" ) # past_weather_url = upload_file(uid, fieldid, imagedate,(uid+'_weather_linegraph.png'),'past_weather.png') except: past_weather_url = ( "https://farmonaut.com/Images/data_not_found.jpg" ) if len(ndvi) > 1: print("came to linegraph") create_linegraph(uid, "ndvi", ndvi, fieldid) create_linegraph(uid, "evi", evi, fieldid) create_linegraph(uid, "savi", savi, fieldid) create_linegraph(uid, "ndre", ndre, fieldid) create_linegraph(uid, "ndwi", ndwi, fieldid) create_linegraph(uid, "ndmi", ndmi, fieldid) create_linegraph(uid, "soc", soc, fieldid) allReportValues["eviGraphImg"] = ( "evi_linegraph.png" ) allReportValues["ndviGraphImg"] = ( "ndvi_linegraph.png" ) allReportValues["ndwiGraphImg"] = ( "ndwi_linegraph.png" ) allReportValues["ndmiGraphImg"] = ( "ndmi_linegraph.png" ) allReportValues["ndreGraphImg"] = ( "ndre_linegraph.png" ) allReportValues["saviGraphImg"] = ( "savi_linegraph.png" ) allReportValues["socGraphImg"] = ( "soc_linegraph.png" ) allReportValues["eviIndexImg"] = "evi_piechart.png" allReportValues["ndviIndexImg"] = "ndvi_piechart.png" allReportValues["ndwiIndexImg"] = "ndwi_piechart.png" allReportValues["ndmiIndexImg"] = "ndmi_piechart.png" allReportValues["ndreIndexImg"] = "ndre_piechart.png" allReportValues["saviIndexImg"] = "savi_piechart.png" allReportValues["socIndexImg"] = "soc_piechart.png" all_weather_data = get_weather_forecast(center_lat, center_long) all_days = [] min_temps = [] max_temps = [] rain_probs = [] max_preps = [] cloud_covers = [] final_days = [] weather_infos = [] for key, val in all_weather_data.items(): all_days.append(key) all_days.sort() all_days = all_days[1:] for key in all_days: # print('kk') # print(key) single_w_data = all_weather_data[key] w_date = datetime.datetime.fromtimestamp(int(key)) final_days.append(str(w_date)[0:10]) weather_infos.append(single_w_data["summary"]) cloud_covers.append(str(int(float(single_w_data["cloudcover"])))) try: temp_min_temp = str(single_w_data["templow"]) # temp_min_temp = ( # str(round(((float(single_w_data["templow"])-32)*(5/9)), 2))) # # temp_min_temp = temp_min_temp + \ # # ((' at '+str(datetime.datetime.fromtimestamp( # # int(single_w_data["templowtime"])))[11:16])) min_temps.append(temp_min_temp) # temp_max_temp = ( # str(round(((float(single_w_data["temphigh"])-32)*(5/9)), 2))) # temp_max_temp = temp_max_temp + \ # ((' at '+str(datetime.datetime.fromtimestamp( # int(single_w_data["temphightime"])))[11:16])) temp_max_temp = str(single_w_data["temphigh"]) max_temps.append(temp_max_temp) except: min_temps.append("not available") max_temps.append("not available") try: rain_probs.append(single_w_data["preciprob"]) temp_prep = str((single_w_data["precimax"])) # temp_prep = temp_prep + \ # ((' at '+str(datetime.datetime.fromtimestamp( # int(single_w_data["precimaxtime"])))[11:16])) max_preps.append(temp_prep) except: rain_probs.append("not available") max_preps.append("not available") allReportValues[ "weather" + str(len(max_preps)) + "MaxPrecipitationTd" ] = max_preps[-1] allReportValues[ "weather" + str(len(max_temps)) + "MaxTempTd" ] = max_temps[-1] allReportValues[ "weather" + str(len(min_temps)) + "MinTempTd" ] = min_temps[-1] allReportValues[ "weather" + str(len(weather_infos)) + "SummaryTd" ] = weather_infos[-1] allReportValues[ "weather" + str(len(final_days)) + "DateTd" ] = final_days[-1] allReportValues[ "weather" + str(len(rain_probs)) + "RainTd" ] = rain_probs[-1] allReportValues[ "weather" + str(len(cloud_covers)) + "CloudCoverTd" ] = cloud_covers[-1] # document.add_page_break() # try: # present_ndvi = float(ndvi[imagedate])/100 # except: # present_ndvi = 0 # try: # present_ndwi = float(ndwi[imagedate])/100 # except: # present_ndwi = 0 # try: # present_ndre = float(ndre[imagedate])/100 # except: # present_ndre = 0 # try: # present_savi = float(savi[imagedate])/100 # except: # present_savi = 0 # try: # present_evi = float(evi[imagedate])/100 # except: # present_evi = 0 # try: # present_ndmi = float(ndmi[imagedate])/100 # except: # present_ndmi = float(0) # try: # present_soc = float(soc[imagedate])/100 # except: # present_soc = 0 # print(html_message) for key, value in allReportValues.items(): allReportValues[key] = str(value) html_str = filler.populateData(lang, reportStrings, allReportValues) field_address = field_address.replace(" ", "_") field_address = field_address.replace(",", "_") pdf_name = ( uid + "/" + fieldid + "/" + whitelabel + (polygonID + "_" if isPolygon else "") + "_report_" + lang + "_" + str(imagedate) + "_" + removeSlash(field_address) + ".pdf" ) htmlfilename = uid + "/" + str(fieldid) + "/report.html" destination_blob_name = ( "PaidMonitoredFields/" + str(uid) + "/" + str(fieldid) + "/" + str(imagedate) + "/report.html" ) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) blob.upload_from_string(html_str) saveToTmpHtml(html_str, htmlfilename) # blob = bucket.blob(destination_blob_name) # blob.download_to_filename(htmlfilename) # with codecs.open('marathi.txt', 'w', encoding='utf-8') as file: # file.write(all_strings[80]) # with codecs.open(htmlfilename, 'w', encoding='utf-8') as file: # file.write(html_message) # htmlfile = open(htmlfilename,"w") # htmlfile.write(html_message) # htmlfile.close() options = { "enable-local-file-access": "", "page-height": "192mm", "page-width": "136mm", "margin-top": "6mm", "margin-bottom": "6mm", "margin-left": "8mm", "margin-right": "8mm" # "font": "report_fonts/Noto_Sans/NotoSans-Medium.ttf", # 'page-size': 'A4', # 'margin-top': '0.75in', } # path_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' # config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf) # with Display(): # pdfkit.from_url(htmlfilename, pdf_name) pdfkit.from_file(htmlfilename, pdf_name, options=options) ## whatsapp sms part try: storageReportName = "%sreport%s.pdf" % ( polygonID + "_" if isPolygon else "", "" if lang == "en" else lang, ) reportUrl = upload_file_to_storage( uid, fieldid, imagedate, pdf_name, storageReportName, 9000 ) ## send whatsapp msg if whatsappEnabled and ( (not isSmsLangInReportLangs and langIndex == 0) or (lang == fSmsLang) ): print( isCloudy, isRVIPresent, isNDVIPresent, [rsmRedZones, rviRedZones] if isRVIPresent else [], [ndwiRedZones, ndviRedZones] if isNDVIPresent else [], ) ## it's expected that irriZones and cropZones are not None try: templateType = "%s%s" % ( 0 if len(irriZones) != 0 else 1, 0 if len(cropZones) != 0 else 1, ) except: print(traceback.format_exc()) mLogger.error( f"isCloudy: {isCloudy}, isRVIPresent: {isRVIPresent}, isNDVIPresent: {isNDVIPresent}, irriZones: {irriZones}, cropZones: {cropZones}, rvi_image: {rvi_image}, ndvi_image: {ndvi_image}", exc_info=True, ) # smsCropInstructions = getSmsInstructionsByZones( # cropZones, reportStrings, lang) # smsIrriInstructions = getSmsInstructionsByZones( # irriZones, reportStrings, lang) ## set smsLang & get instructions & dirImg smsLang = lang if isSmsLangInReportLangs else (fSmsLang or "en") if uid == "M53J9SImW9My4vghlCaaWLYxsc22": smsLang = "hi" if smsLang != lang: reportStrings = ( db.reference("ReportStrings2022").child(smsLang).get() ) smsInstructions = getSmsFullInstructionsByZones( templateType, irriZones, cropZones, reportStrings, smsLang ) ## check dirMapImg in storage & upload if not present storageDirMapImgName = "%sdir_on_map_%s.png" % ( polygonID + "_" if isPolygon else "", smsLang, ) dirMapUrl = getStorageFileUrl( uid, fieldid, "default", storageDirMapImgName, storage_client ) if dirMapUrl == None: if lang != smsLang: createFieldDirImg( (uid + '/' + fieldid + '/'+ etci_image), dir_img, reportStrings, smsLang ) dirImgStoragePath = "dir_img_%s.png" % smsLang if not checkIfStorageFilePresent( uid, fieldid, "default", dirImgStoragePath, storage_client, ): upload_file_to_storage( uid, fieldid, "default", dir_img, dirImgStoragePath ) dirMapPath = "%s/%s/%sdirMapImg%s.png" % ( uid, fieldid, polygonID + "_" if isPolygon else "", smsLang, ) createDirOnMapImg(dir_img, map_img, mapImgDimens, dirMapPath, addLogo=addLogo) dirMapUrl = upload_file_to_storage( uid, fieldid, "default", dirMapPath, storageDirMapImgName ) dirMapShortUrl = createShortLink(dirMapUrl, defaultUrl='https://farmonaut.com/wp-content/uploads/2024/01/thumbnail-Whatsapp-Advisory--1024x576.jpg') reportShortUrl = createShortLink(reportUrl) print(dirMapShortUrl, dirMapUrl) ## get name, phoneNo if isPolygon: polygonData = ( fieldOrPolyData or db.reference("PaidMonitoredFields") .child("PMF") .child(uid) .child(fieldid) .child("Polygons") .child(polygonID) .get() ) phoneNo = ( polygonData["PhoneNumber"] if "PhoneNumber" in polygonData else polygonData["FarmerInfoData"]["whatsAppNumber"] ) userName = polygonData["Name"] else: phoneNo = ( fieldOrPolyData.get("whatsAppNumber", None) or fieldOrPolyData.get("Phone", None) or userData.get("Phone", None) ) userName = fieldOrPolyData.get("Name", " ") or userData.get( "Name", " " ) ## for DCM if uid == "M53J9SImW9My4vghlCaaWLYxsc22": orgData = fieldOrPolyData["OrgData"] if userName == None: userName = orgData.get("OrgRyotName", "Farmer") if phoneNo == None: phoneNo = orgData.get( "OrgWhatsappNo", orgData.get("OrgMobileNo", None) ) phoneNo = sanitiseNumber(phoneNo) ## send msg orgCompanyData = orgReportData.get("smsCompanyName", {"en":"Farmonaut"}) orgName = orgCompanyData.get(smsLang, orgCompanyData.get("en", "Farmonaut")) smsParams = [ userName, orgName, temp_field_address, smsInstructions, reportShortUrl, expertNumber, orgName, ] send_whatsapp(dirMapUrl, smsParams, lang, uid, phoneNo, whitelabel) except: # whatsapp part error print(traceback.format_exc()) return { "isCloudy": isCloudy, "cropRedZones": "empty" if len(cropZones) == 0 else cropZones, "irrigationRedZones": "empty" if len(irriZones) == 0 else irriZones, "socRedZones": None if isNDVIPresent else ("empty" if len(soilZones) == 0 else soilZones), } except: print(traceback.format_exc()) return "yes" def sanitiseNumber(num): # expecting indian numbers only, otherwise returns None if num == None: return None num = str(num) num = num.replace(" ", "") if len(num) == 10: return num if num[:3] == "+91": return num[-10:] return None def removeSlash(str): return str.replace("/", "") def checkIfStorageFilePresent(uid, fieldId, imgDate, fileName, storage_client): bucket_name = "farmbase-b2f7e.appspot.com" destination_blob_name = ( "PaidMonitoredFields/" + str(uid) + "/" + str(fieldId) + "/" + str(imgDate) + "/" + fileName ) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) return blob.exists() def getStorageFileUrl(uid, fieldId, imgDate, fileName, storage_client): bucket_name = "farmbase-b2f7e.appspot.com" destination_blob_name = ( "PaidMonitoredFields/" + str(uid) + "/" + str(fieldId) + "/" + str(imgDate) + "/" + fileName ) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) cred = service_account.Credentials.from_service_account_file("servicekey.json") try: url = blob.generate_signed_url( version="v4", expiration=datetime.timedelta(minute=10000), method="GET", credentials=cred, ) return url except: return None def downloadStorageFileToPath(uid, fieldId, imgDate, fileName, storage_client, outPath): """Downloads field file and save to outPath RETURNS - outPath if successful""" bucket_name = "farmbase-b2f7e.appspot.com" destination_blob_name = ( "PaidMonitoredFields/" + str(uid) + "/" + str(fieldId) + "/" + str(imgDate) + "/" + fileName ) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name) cred = service_account.Credentials.from_service_account_file("servicekey.json") try: blob.download_to_filename(outPath) return outPath except: return None def createShortDynamicLink(longLink, domainPrefix="https://farmonaut.page.link"): apiKey = "AIzaSyCdVSM4xjufjH7v01en4fh6TdomsdurmC8" info = { "dynamicLinkInfo": { "domainUriPrefix": domainPrefix, "link": longLink, }, "suffix": {"option": "SHORT"}, } response = requests.post( "https://firebasedynamiclinks.googleapis.com/v1/shortLinks", params={"key": apiKey}, data=json.dumps(info), ) if response.status_code == 200: return json.loads(response.text)["shortLink"] else: return None def createShortLink(longLink, domainPrefix="https://farmonaut.com", defaultUrl=""): apiKey = "eyJhbGciOiJIUzI1NiIsInR5cGUiOiJKV1QifQ.eyJpYXQiOjE3MTkzMDUzMTgsImlkZW50aWZpZXIiOiJmYXJtb25hdXRfYWRtaW4ifQ.VdTMA95b0zzePCnnlMsxl7TiOYhW9PEQ1EszL7gRQnY" info = {"url": longLink, "domain": domainPrefix} response = requests.post( "https://us-central1-farmbase-b2f7e.cloudfunctions.net/create_short_url", headers={"Authorization": "Bearer " + apiKey, "Content-Type": "application/json"}, data=json.dumps(info), ) if response.status_code == 200: return json.loads(response.text)["data"]["short_url"] else: return defaultUrl def saveToTmpHtml(htmlStr, htmlFileName): with open(htmlFileName, "w", encoding="utf8") as f: f.write(htmlStr) def try_index(health, index): new_index = {} try: new_index = health[index] except: new_index = {} return new_index ######### Test Code ######### def test(): print("start") url = "https://farmonaut.com/wp-content/uploads/2024/01/thumbnail-Whatsapp-Advisory--1024x576.jpg" short_url = createShortLink(url) print(short_url) print("done") # test()