-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbom_ftp.py
68 lines (54 loc) · 2.25 KB
/
bom_ftp.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
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 1 15:45:32 2021
@author: imchugh
"""
import ftplib
import pathlib
import sys
#------------------------------------------------------------------------------
### Constants
#------------------------------------------------------------------------------
DIRS_DICT = {'satellite': 'anon/gen/gms',
'mslp': 'anon/gen/fwo'}
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
### Classes
#------------------------------------------------------------------------------
class bom_ftp_getter():
"""Get ftp chart file lists from bom ftp server and get files"""
def __init__(self, img_type):
try:
self.img_type = DIRS_DICT[img_type]
except KeyError as e:
raise Exception('img_type must be one of: {}'
.format(', '.join(list(DIRS_DICT.keys())))) from e
bom_ftp = 'ftp.bom.gov.au'
ftp = ftplib.FTP(bom_ftp)
ftp.login()
self.ftp = ftp
def get_file_list(self, file_type=None, search_str=None):
files = self.ftp.nlst(self.img_type)
if file_type:
files = [x for x in files if file_type in x]
if search_str:
files = [x for x in files if search_str in x]
return files
def get_file(self, src_file, output_dir, output_name=None):
if not output_name:
out_file = pathlib.Path(output_dir) / pathlib.Path(src_file).name
else:
out_file = pathlib.Path(output_dir) / output_name
with open(out_file, 'wb') as f:
self.ftp.retrbinary('RETR {}'.format(src_file), f.write)
if __name__ == "__main__":
out_path = sys.argv[1]
getter = bom_ftp_getter(img_type='satellite')
file_list = getter.get_file_list(file_type='jpg', search_str='IDE00135.20')
getter.get_file(src_file=file_list[-1], output_dir=out_path,
output_name='sat_img.jpg')
getter = bom_ftp_getter(img_type='mslp')
file_list = getter.get_file_list(file_type='png', search_str='IDY00030')
getter.get_file(src_file=file_list[-1], output_dir=out_path,
output_name='mslp.png')