Skip to content

Commit

Permalink
Merge pull request #591 from Som-Energia/MOD_esios_ftp_server
Browse files Browse the repository at this point in the history
Copiar ESIOS zip a servidor FTP
  • Loading branch information
oriolpiera authored Jul 5, 2024
2 parents a282af7 + 173a9c3 commit d65251d
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions scripts/download_esios_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import re
import os
from time import sleep
import paramiko

FILE_TYPE = 'liquicomun'
FILE_PREFERENCE = ['A2_liquicomun', 'A1_liquicomun']
Expand All @@ -47,7 +48,7 @@ def get_search_day():
return tomorrow.strftime('%d-%m-%Y')


def download_files(file_type, server, server_port):
def download_files(file_type, server, server_port, ftp_server, ftp_username, ftp_password):
day_of_search = get_search_day()
step("Listing avaiable files {}...".format(FILE_TYPE))
output = Popen(
Expand Down Expand Up @@ -101,7 +102,6 @@ def download_files(file_type, server, server_port):

step("Coping file to the server {}...".format(server))
call(["scp", "-P", server_port, file_path, server])
os.remove('{}/{}'.format(BIN_PATH, filename))
step("File {} copied to {} successfully".format(filename, server))

step("Unzipping file on remote server {}...".format(server))
Expand All @@ -112,6 +112,21 @@ def download_files(file_type, server, server_port):
print ssh.stderr.readlines()
step("File {} unzip successfully".format(filename))

step("Coping file to SFTP server {}".format(ftp_server))

with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(hostname=ftp_server, port=server_port,
username=ftp_username, password=ftp_password)
sftp = ssh.open_sftp()
sftp.chdir('/{}'.format(FILE_TYPE))
sftp.put(file_path, filename)

step("All done")
step("Removing file from localhost")
os.remove('{}/{}'.format(BIN_PATH, filename))


if __name__ == "__main__":
step("Start run: {}".format(datetime.today().strftime('%d-%m-%Y %H:%M:%S')))
Expand All @@ -135,8 +150,27 @@ def download_files(file_type, server, server_port):
type=str,
help="SSH server port",
)
parser.add_argument(
"--ftp_server",
dest="ftp_server",
type=str,
help="FTP server address",
)
parser.add_argument(
"--ftp_username",
dest="ftp_username",
type=str,
help="FTP user name",
)
parser.add_argument(
"--ftp_password",
dest="ftp_password",
type=str,
help="FTP user password",
)
args = parser.parse_args()

download_files(args.file_type, args.server, args.server_port)
download_files(args.file_type, args.server, args.server_port,
args.ftp_server, args.ftp_username, args.ftp_password)
step("Finsish run: {}".format(datetime.today().strftime('%d-%m-%Y %H:%M:%S')))
step("=============================================\n")

0 comments on commit d65251d

Please sign in to comment.