from google.oauth2 import service_account import datetime import traceback import requests import json from utils import mLogger 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 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: # isGodrej = uid in whatsappSmsCompanies["Godrej"]["UIDs"] companyCode = userData['CompanyCode'] if 'CompanyCode' in userData else "Farmonaut" else: userData = {} companyCode = "Farmonaut" isGodrej = companyCode == "Godrej" orgSmsData = db.reference( f'OrganisationData/{companyCode}/ReportData').get() 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 orgSmsData.get("isWhatsappEnabled", False) print(['langg1s: ', fSmsLang, whatsappEnabled, userData, orgSmsData]) if whatsappEnabled: fSmsLang = fSmsLang or userData.get( "defaultSmsLang", None) or orgSmsData.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_TCI.png' % (uid, polygonID) ndvi_image = '%s/%s_NDVI.png' % (uid, polygonID) ndwi_image = '%s/%s_NDWI.png' % (uid, polygonID) rvi_image = '%s/%s_RVI.png' % (uid, polygonID) rsm_image = '%s/%s_RSM.png' % (uid, polygonID) ndwi_image_cmap2 = '%s/%s_NDWI_CMAP2.png' % (uid, polygonID) ndvi_image_cmap2 = '%s/%s_NDVI_CMAP2.png' % (uid, polygonID) rvi_image_cmap2 = '%s/%s_RVI_CMAP2.png' % (uid, polygonID) rsm_image_cmap2 = '%s/%s_RSM_CMAP2.png' % (uid, polygonID) recl_image = '%s/%s_RECL.png' % (uid, polygonID) recl_image_cmap2 = '%s/%s_RECL_CMAP2.png' % (uid, polygonID) dir_img = '%s/%s_dirImg.png' % (uid, polygonID) map_img = '%s/%s_mapImg.jpg' % (uid, polygonID) bdy_img = '%s/%s_bdyImg.jpg' % (uid, polygonID) ndre_image = '%s/%s_NDRE.png' % (uid, polygonID) ndre_image_cmap2 = '%s/%s_NDRE_CMAP2.png' % (uid, polygonID) else: etci_image = '%s/ETCI.png' % (uid) ndvi_image = '%s/ndvi.png' % (uid) ndwi_image = '%s/ndwi.png' % (uid) rvi_image = '%s/rvi.png' % (uid) rsm_image = '%s/rsm.png' % (uid) ndwi_image_cmap2 = '%s/ndwi_cmap2.png' % (uid) ndvi_image_cmap2 = '%s/ndvi_cmap2.png' % (uid) rvi_image_cmap2 = '%s/rvi_cmap2.png' % (uid) rsm_image_cmap2 = '%s/rsm_cmap2.png' % (uid) dir_img = '%s/dirImg.png' % (uid) map_img = '%s/mapImg.jpg' % (uid) bdy_img = '%s/bdyImg.jpg' % (uid) recl_image = '%s/recl.png' % (uid) recl_image_cmap2 = '%s/recl_cmap2.png' % (uid) ndre_image = '%s/ndre.png' % (uid) ndre_image_cmap2 = '%s/ndre_cmap2.png' % (uid) tci_image = '%s/TCI.png' % (uid) evi_image = '%s/evi.png' % (uid) savi_image = '%s/savi.png' % (uid) ndmi_image = '%s/ndmi.png' % (uid) hybrid_image = '%s/hybrid.png' % (uid) hybrid_blind_image = '%s/hybrid_blind.png' % ( uid) dem_image = '%s/dem.png' % (uid) soc_image = '%s/soc.png' % (uid) evapo_image = '%s/evapo.png' % (uid) evi_image_cmap2 = '%s/evi_cmap2.png' % (uid) savi_image_cmap2 = '%s/savi_cmap2.png' % (uid) soc_image_cmap2 = '%s/soc_cmap2.png' % (uid) ndmi_image_cmap2 = '%s/ndmi_cmap2.png' % (uid) 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 = getFieldMapImg(points, fieldLatLngs, map_img) isNDVIPresent = os.path.exists(ndvi_image) isCloudy = isCloudyImg( etci_image, ndvi_image) if isNDVIPresent else True isSmallPlant = ndviAvgVal < 0.3 and ndwiAvgVal < 0.3 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) # 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) # 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 = '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"] = sensed_year + \ '-'+sensed_month+'-'+sensed_day allReportValues["rgbTCIImg"] = tci_image allReportValues["rgbETCIImg"] = 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 createFieldDirImg(etci_image, dir_img, reportStrings, lang) dirImgStoragePath = "dir_img_%s.png" % lang if not checkIfPresent(uid, fieldid, "default", dirImgStoragePath, storage_client): upload_file(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[0], mapImgDimens[1]) allReportValues["fieldImgPaddings"] = ( mapImgDimens[2], mapImgDimens[3]) 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 # fieldBdyDimens = getFieldMapImg( # points, fieldLatLngs, bdy_img, weight=3) 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) allReportValues["weatherGraphImg"] = uid + \ "_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: create_linegraph(uid, 'ndvi', ndvi) create_linegraph(uid, 'evi', evi) create_linegraph(uid, 'savi', savi) create_linegraph(uid, 'ndre', ndre) create_linegraph(uid, 'ndwi', ndwi) create_linegraph(uid, 'ndmi', ndmi) create_linegraph(uid, 'soc', soc) allReportValues["eviGraphImg"] = uid+"/evi_linegraph.png" allReportValues["ndviGraphImg"] = uid+"/ndvi_linegraph.png" allReportValues["ndwiGraphImg"] = uid+"/ndwi_linegraph.png" allReportValues["ndmiGraphImg"] = uid+"/ndmi_linegraph.png" allReportValues["ndreGraphImg"] = uid+"/ndre_linegraph.png" allReportValues["saviGraphImg"] = uid+"/savi_linegraph.png" allReportValues["socGraphImg"] = uid+"/soc_linegraph.png" allReportValues["eviIndexImg"] = uid+"/evi_piechart.png" allReportValues["ndviIndexImg"] = uid+"/ndvi_piechart.png" allReportValues["ndwiIndexImg"] = uid+"/ndwi_piechart.png" allReportValues["ndmiIndexImg"] = uid+"/ndmi_piechart.png" allReportValues["ndreIndexImg"] = uid+"/ndre_piechart.png" allReportValues["saviIndexImg"] = uid+"/savi_piechart.png" allReportValues["socIndexImg"] = uid+"/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"])*100))) try: 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])) max_temps.append(temp_max_temp) except: min_temps.append('not available') max_temps.append('not available') try: rain_probs.append( str(round((float(single_w_data["preciprob"])*100), 2))) 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+'/' + whitelabel + (polygonID+"_" if isPolygon else "") + '_report_' + \ lang + '_'+str(imagedate)+'_'+removeSlash(field_address)+'.pdf' htmlfilename = uid + '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) try: storageReportName = '%sreport%s.pdf' % ( polygonID+"_" if isPolygon else "", "" if lang == "en" else lang) reportUrl = upload_file( uid, fieldid, imagedate, pdf_name, storageReportName, 9000) # send whatsapp msg; TODO: add name at calling place, remove upload pdf, check if dirMap already present 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) smsLang = lang if isSmsLangInReportLangs else ( fSmsLang or 'en') if smsLang != lang: reportStrings = db.reference( 'ReportStrings2022').child(smsLang).get() smsInstructions = getSmsFullInstructionsByZones( templateType, irriZones, cropZones, reportStrings, smsLang) storageDirImgName = "%sdir_on_map_%s.png" % ( polygonID+"_" if isPolygon else "", smsLang) dirMapUrl = getUrlIfPresent( uid, fieldid, "default", storageDirImgName, storage_client) if dirMapUrl == None: if lang != smsLang: createFieldDirImg(etci_image, dir_img, reportStrings, smsLang) dirImgStoragePath = "dir_img_%s.png" % smsLang if not checkIfPresent(uid, fieldid, "default", dirImgStoragePath, storage_client): upload_file(uid, fieldid, "default", dir_img, dirImgStoragePath) dirMapPath = "%s/dirMapImg%s.png" % (uid, smsLang) createDirOnMapImg(dir_img, map_img, mapImgDimens, dirMapPath) dirMapUrl = upload_file( uid, fieldid, "default", dirMapPath, storageDirImgName) dirMapShortUrl = createShortDynamicLink(dirMapUrl) reportShortUrl = createShortDynamicLink(reportUrl) # 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'] phoneNo = sanitiseNumber(phoneNo) userName = polygonData['Name'] else: # split_name_phone = temp_field_address.split(",") # try: # phoneNumber = split_name_phone[1] # except: # phoneNumber = "6366026267" # phoneNumber = phoneNumber.replace(" ", "") # if "+91" not in phoneNumber: # phoneNumber = "+91"+phoneNumber # if uid == "XSzwRsmzu40eCm8UvllGn7xsn0k1": # phoneNumber = "+919819984575" phoneNo = fieldOrPolyData.get( "Phone", None) or userData.get("Phone", None) phoneNo = sanitiseNumber(phoneNo) userName = fieldOrPolyData.get( "Name", None) or userData.get("Name", None) if phoneNo == None: # no phone number present return # send msg smsParams = [userName, temp_field_address, smsInstructions, reportShortUrl, ( (orgSmsData["smsCompanyName"].get(smsLang, None) or orgSmsData["smsCompanyName"].get("en", None)) if "smsCompanyName" in orgSmsData else None) or "Farmonaut"] send_whatsapp(dirMapShortUrl, smsParams, lang, uid, phoneNo) except: print(traceback.format_exc()) return { "isCloudy": isCloudy, "cropRedZones": cropZones, "irrigationRedZones": irriZones, } except: print(traceback.format_exc()) # compress(pdf_name,pdf_name) def sanitiseNumber(num): # expecting indian numbers only, otherwise returns None if num == None: return None 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 checkIfPresent(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 getUrlIfPresent(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 createShortDynamicLink(longLink): apiKey = "AIzaSyCdVSM4xjufjH7v01en4fh6TdomsdurmC8" info = { "dynamicLinkInfo": { "domainUriPrefix": "https://farmonaut.page.link", "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 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 # longUrl = "https://storage.googleapis.com/farmbase-b2f7e.appspot.com/PaidMonitoredFields/fSRlyKzCxxMJHJqDzpGhQImyyxf2/1669891376338/20221013/report.pdf?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=farmbase-b2f7e%40appspot.gserviceaccount.com%2F20221209%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20221209T131824Z&X-Goog-Expires=900&X-Goog-SignedHeaders=host&X-Goog-Signature=319ecd82f2015e6886788b82453319747d25ed2f56491b46d6700cecfe93358d95d3fe258a1aab5cfc89287af1770b83a3cf3f8550bb5f4c19e0bc37af48cc708f90e5675193b23c32b50bd707332a7de513b28e2beb6991d96c388e83325e66575a07096b39864e5de55f53efdbc4e00880b547608fed7d07873d6e0c9221acf8dc6722d46955d446d22c9b6f268ccbbf3b6d588b69ead0366c2ded7d681b94f5853d7c404c39c6cfcab2b682500ba1e349e46ebeaf2880ff17650296a9d90eef2688d8bb9456921224826c5ba3cdb9340f9f5700eb8357b9477c65e40d5d19269d788a887f0eabbdaf6b82e6cc7fa6ccf1f6d41188f57f3f620e9fded49baa" # shortUrl = createShortDynamicLink(longUrl) # print(shortUrl)