from itertools import zip_longest from tempfile import tempdir import firebase_admin from firebase_admin import credentials from firebase_admin import db from find_s1_image import find_img_value import pandas as pd from sklearn.metrics import classification_report, confusion_matrix, accuracy_score import statsmodels.api as sm import seaborn as sns sns.set() from zipfile import ZipFile from sklearn.ensemble import RandomForestRegressor from sklearn.model_selection import train_test_split import csv import time import json from find_modis_ndvi import find_modis_ndvi import numpy as np from sklearn.cluster import KMeans from find_study_area_values import find_study_area_values from find_study_area_values3 import find_study_area_values3 from make_area_estimate_image import make_area_estimate_image from make_egypt_estimate_image import make_egypt_estimate_image from sentinelhub import WebFeatureService, BBox, CRS, MimeType, CRS, BBox, WmsRequest,DataCollection import traceback from firebase_admin import firestore from PIL import Image import numpy as np from google.cloud import storage import os from firebase_admin import db from get_mask import get_mask import cv2 import scipy.ndimage from google.oauth2 import service_account import datetime from datetime import date from segment_sample import get_main_segment import requests import traceback from ftplib import FTP import paramiko import pysftp storage_client = storage.Client.from_service_account_json("servicekey.json"); bucket_name = 'farmbase-b2f7e.appspot.com' cred = service_account.Credentials.from_service_account_file('servicekey.json') #cred = credentials.Certificate('servicekey.json') bucket = storage_client.bucket(bucket_name) try: firebase_admin.initialize_app(credentials.Certificate('servicekey.json'), {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') db_firestore = firestore.client() def upload_file_via_sftp(hostname, port, username, password, local_file_path, remote_directory): try: # try: # conn = pysftp.Connection(host=hostname,port=port,username=username, password=password) # print("connection established successfully") # except: # print('failed to establish connection to targeted server') # # file path of local file and targeted location # # local_file='tmp.txt' # # target_location='/' # # call conn.put() method to upload file to server # conn.put(local_file_path, remote_directory) #Create an SSH client ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname, port, username, password) # Create an SFTP client from the SSH client sftp = ssh.open_sftp() # # Get the current directory where the Python script is located # script_directory = os.path.dirname(os.path.realpath(__file__)) # # Construct the local file path based on the script directory # local_file_path = os.path.join(script_directory, local_file_path) # Change to the remote directory (create it if it doesn't exist) try: sftp.chdir(remote_directory) except IOError: sftp.mkdir(remote_directory) sftp.chdir(remote_directory) # Upload the local file to the remote directory remote_file_path = remote_directory + '/' + local_file_path.split('/')[-1] print(local_file_path) print(remote_file_path) if not os.path.exists(local_file_path): print('doesnt exist') return sftp.put(local_file_path, remote_file_path) # Close the SFTP session and the SSH connection sftp.close() ssh.close() print(f"File '{local_file_path}' uploaded to '{remote_file_path}' successfully.") except Exception as e: print(traceback.format_exc()) print(f"An error occurred: {str(e)}") # except FileNotFoundError as e: # print(f"Local file not found: {e}") # except PermissionError as e: # print(f"Permission error: {e}") # except pysftp.SSHException as e: # print(f"SFTP error: {e}") # except Exception as e: # print(f"An unexpected error occurred: {e}") def process_segments(): #storage_client = storage.Client() zip_requests_obj = db_firestore.collection(u'SegmentRequests').where('Processed','==',0).get() for temp_d in zip_requests_obj: time_stamp = temp_d.id time_stamp = str(time_stamp) print(temp_d.id) print(temp_d.to_dict()) temp_main_obj = temp_d.to_dict() uid = temp_main_obj["UID"] coords = temp_main_obj["Point"] new_coords = coords.split(",") lat = float(new_coords[0]) lng = float(new_coords[1]) found_coordinates = get_main_segment(lat,lng,uid, "sample") temp_main_obj["Processed"] = 1 temp_main_obj["Points"] = found_coordinates db_firestore.collection('SegmentRequests').document(time_stamp).set(temp_main_obj) hostname = "ox4.27a.myftpupload.com" # Replace with your FTP server hostname username = "SdfbBC9yoGDOGt@ox4.27a.myftpupload.com" # Replace with your FTP username password = "YC0fvrn=vi0mrK" # Replace with your FTP password local_file_path = "Gharbia.png" # Replace with the local file path remote_directory = "Gharbia.png" # Replace with the remote directory #upload_file_ftp(hostname, username, password, local_file_path, remote_directory) #### don't forget to add comments, specially for hard coded part hostname = "ox4.27a.myftpupload.com" # Replace with your FTP server hostname #hostname = 'farmonaut.com' username = "W1abomKYhx9M5v" # Replace with your FTP username password = "MWOODJX8RIITg5" # Replace with your FTP password local_file_path = "./SFTP/Damietta.png" # Replace with the local file path remote_directory = "" # Replace with the remote directory #upload_file_ftp(hostname, username, password, local_file_path, remote_directory) # Example usage: upload_file_via_sftp(hostname, 22, username, password, local_file_path, remote_directory) #upload_file_ftp(hostname, username, password, local_file_path, remote_directory) # i = 0 # while i == 0: # process_segments() # time.sleep(60)