-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpconnection.py
58 lines (48 loc) · 1.57 KB
/
ftpconnection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
from typing import List
import pysftp
FTP_HOST = os.getenv("FTP_HOST")
FTP_USER = os.getenv("FTP_USER")
FTP_PWD = os.getenv("FTP_PWD")
class FTPConnection:
"""
An FTP server connection object for downloading and managing files on an FTP server.
"""
def __init__(self):
"""
Initialize FTP connection using pysftp.
"""
self.cnopts = pysftp.CnOpts()
self.cnopts.hostkeys = None
self.ftpsrv = pysftp.Connection(
host=FTP_HOST,
username=FTP_USER,
password=FTP_PWD,
cnopts=self.cnopts,
)
def get_directory_names(self, remote_dir_path: str) -> List[str]:
"""
Return a list of all the directory names. For SEIS, the directory names are the school names.
Params:
remotedir (str): path of the remote directory.
Return:
list: string names of the directories.
"""
return self.ftpsrv.listdir(remote_dir_path)
def download_all(
self,
remote_dir_path: str,
local_dir_path: str,
sub_dir_names: List[str],
file_names: List[str]
) -> None:
"""
Loop through all sub-directories and download the files defined in init.
"""
for school in sub_dir_names:
for file_name in file_names:
self.ftpsrv.get(
f"{remote_dir_path}/{school}/{file_name}",
f"{local_dir_path}/{school}_{file_name}",
preserve_mtime=True,
)