10
10
# third party
11
11
import paramiko
12
12
13
- class AllowAnythingPolicy (paramiko .MissingHostKeyPolicy ):
14
- def missing_host_key (self , client , hostname , key ):
15
- return
16
-
17
13
18
14
def print_callback (filename , bytes_so_far , bytes_total ):
15
+ """Log file transfer progress"""
19
16
rough_percent_transferred = int (100 * (bytes_so_far / bytes_total ))
20
17
if (rough_percent_transferred % 25 ) == 0 :
21
18
print (f'{ filename } transfer: { rough_percent_transferred } %' )
22
19
23
20
24
21
def get_files_from_dir (sftp , out_path ):
22
+ """Download files from sftp server that have been uploaded in last day
23
+ Args:
24
+ sftp: SFTP Session from Paramiko client
25
+ out_path: Path to local directory into which to download the files
26
+ """
27
+
25
28
current_time = datetime .datetime .now ()
26
- seconds_in_day = 24 * 60 * 60
27
29
28
30
# go through files in recieving dir
29
- files_to_download = []
31
+ filepaths_to_download = {}
30
32
for fileattr in sftp .listdir_attr ():
31
33
file_time = datetime .datetime .fromtimestamp (fileattr .st_mtime )
32
- time_diff_to_current_time = current_time - file_time
33
- if time_diff_to_current_time .total_seconds () <= seconds_in_day :
34
- files_to_download .append (fileattr .filename )
35
-
36
- filepaths_to_download = {}
37
- for file in files_to_download :
38
- full_path = path .join (out_path , file )
39
- if path .exists (full_path ):
40
- print (f"{ file } exists, skipping" )
41
- else :
42
- filepaths_to_download [file ] = full_path
34
+ filename = fileattr .filename
35
+ if current_time - file_time < datetime .timedelta (days = 1 ) and \
36
+ not path .exists (filename ):
37
+ filepaths_to_download [filename ] = path .join (out_path , filename )
43
38
44
39
# make sure we don't download more than 2 files per day
45
- assert len (files_to_download ) <= 2 , "more files dropped than expected"
40
+ assert len (filepaths_to_download ) <= 2 , "more files dropped than expected"
46
41
47
42
# download!
48
43
for infile , outfile in filepaths_to_download .items ():
@@ -51,21 +46,29 @@ def get_files_from_dir(sftp, out_path):
51
46
52
47
53
48
def download (out_path , ftp_conn ):
49
+ """Downloads files necessary to create CHC signal from ftp server.
50
+ Args:
51
+ out_path: Path to local directory into which to download the files
52
+ ftp_conn: Dict containing login credentials to ftp server
53
+ """
54
54
55
55
# open client
56
- client = paramiko .SSHClient ()
57
- client .set_missing_host_key_policy (AllowAnythingPolicy ())
58
-
59
- client .connect (ftp_conn ["host" ], username = ftp_conn ["user" ],
60
- password = ftp_conn ["pass" ][1 :] + ftp_conn ["pass" ][0 ],
61
- port = ftp_conn ["port" ],
62
- allow_agent = False , look_for_keys = False )
63
- sftp = client .open_sftp ()
64
-
65
- sftp .chdir ('/dailycounts/All_Outpatients_By_County' )
66
- get_files_from_dir (sftp , out_path )
67
-
68
- sftp .chdir ('/dailycounts/Covid_Outpatients_By_County' )
69
- get_files_from_dir (sftp , out_path )
70
-
71
- client .close ()
56
+ try :
57
+ client = paramiko .SSHClient ()
58
+ client .set_missing_host_key_policy (paramiko .AutoAddPolicy ())
59
+
60
+ client .connect (ftp_conn ["host" ], username = ftp_conn ["user" ],
61
+ password = ftp_conn ["pass" ][1 :] + ftp_conn ["pass" ][0 ],
62
+ port = ftp_conn ["port" ],
63
+ allow_agent = False , look_for_keys = False )
64
+ sftp = client .open_sftp ()
65
+
66
+ sftp .chdir ('/dailycounts/All_Outpatients_By_County' )
67
+ get_files_from_dir (sftp , out_path )
68
+
69
+ sftp .chdir ('/dailycounts/Covid_Outpatients_By_County' )
70
+ get_files_from_dir (sftp , out_path )
71
+
72
+ finally :
73
+ if client :
74
+ client .close ()
0 commit comments