Skip to content

Commit 127aa79

Browse files
committed
Add DownloadAction argparse action for --download
Encapsulate the dbip.csv download process into an argparse action subclass: OptionalBooleanAction. (see https://docs.python.org/3/library/argparse.html#action) This way the download of the required dbip.com file is earlier and the blocks file parameter (args.blocks) can be set as soon as possible in order to avoid trying to read a non existing file. Enables the usage of --download/--no-download. Bumps the required minimum Python version to 3.9.
1 parent 4cb1b1b commit 127aa79

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

nft_geoip.py

+30-24
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@
5151
'country_alpha_2')
5252

5353

54+
class DownloadAction(argparse.BooleanOptionalAction):
55+
"""
56+
Custom BooleanOptionalAction to download db-ip csv in case the user
57+
specified so.
58+
"""
59+
def __call__(self, parser, namespace, values, option_string=None):
60+
super().__call__(parser, namespace, values, option_string=option_string)
61+
if namespace.download:
62+
filename = namespace.dir+'dbip.csv.gz'
63+
url = 'https://download.db-ip.com/free/dbip-country-lite-{}.csv.gz'.format(time.strftime("%Y-%m"))
64+
print('Downloading db-ip.com geoip csv file...')
65+
r = requests.get(url, stream=True)
66+
if r.status_code == 200:
67+
with open(filename, 'wb') as f:
68+
r.raw.decode_content = True
69+
shutil.copyfileobj(r.raw, f)
70+
else:
71+
sys.exit('Error trying to download DB-IP lite geoip csv file. Bailing out...')
72+
73+
with gzip.open(filename, 'rb') as f_in:
74+
with open(namespace.dir+'dbip.csv', 'wb') as f_out:
75+
shutil.copyfileobj(f_in, f_out)
76+
os.remove(filename)
77+
# Update blocks arg with the downloaded file
78+
setattr(namespace, 'blocks', open(namespace.dir+'dbip.csv', 'r', encoding='utf-8'))
79+
5480
def strip_accent(text):
5581
"""
5682
Remove accented characters. Convert to ASCII.
@@ -263,10 +289,10 @@ def create_parser():
263289
f'(default: {DEFAULT_FILE_ADDRESS})',
264290
required=False,
265291
dest='blocks')
266-
parser.add_argument('-d', '--download', action='store_true',
267-
help='fetch geoip data from db-ip.com. This option overrides --file-address',
268-
required=False,
269-
dest='download')
292+
parser.add_argument('-d', '--download', action=DownloadAction,
293+
help='Fetch geoip data from db-ip.com. Overrides --file-address.',
294+
default=False,
295+
required=False)
270296
parser.add_argument('-o', '--output-dir',
271297
help='Existing directory where downloads and output will be saved. '
272298
'(default: working directory)',
@@ -295,26 +321,6 @@ def create_parser():
295321
# Add trailing / for folder path if there isn't
296322
args.dir += '/' if args.dir[-1:] != '/' else ''
297323

298-
if args.download:
299-
filename = args.dir+'dbip.csv.gz'
300-
url = 'https://download.db-ip.com/free/dbip-country-lite-{}.csv.gz'.format(time.strftime("%Y-%m"))
301-
print('Downloading db-ip.com geoip csv file...')
302-
r = requests.get(url, stream=True)
303-
if r.status_code == 200:
304-
with open(filename, 'wb') as f:
305-
r.raw.decode_content = True
306-
shutil.copyfileobj(r.raw, f)
307-
else:
308-
sys.exit('Error trying to download DB-IP lite geoip csv file. Bailing out...')
309-
310-
with gzip.open(filename, 'rb') as f_in:
311-
with open(args.dir+'dbip.csv', 'wb') as f_out:
312-
shutil.copyfileobj(f_in, f_out)
313-
os.remove(filename)
314-
315-
# Update blocks arg with the downloaded file
316-
args.blocks = open(args.dir+'dbip.csv', 'r', encoding='utf-8')
317-
318324
if not (args.blocks or args.locations):
319325
parser.print_help()
320326
sys.exit('Missing required address and location csv files.')

0 commit comments

Comments
 (0)