|
51 | 51 | 'country_alpha_2')
|
52 | 52 |
|
53 | 53 |
|
| 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 | + |
54 | 80 | def strip_accent(text):
|
55 | 81 | """
|
56 | 82 | Remove accented characters. Convert to ASCII.
|
@@ -263,10 +289,10 @@ def create_parser():
|
263 | 289 | f'(default: {DEFAULT_FILE_ADDRESS})',
|
264 | 290 | required=False,
|
265 | 291 | 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) |
270 | 296 | parser.add_argument('-o', '--output-dir',
|
271 | 297 | help='Existing directory where downloads and output will be saved. '
|
272 | 298 | '(default: working directory)',
|
@@ -295,26 +321,6 @@ def create_parser():
|
295 | 321 | # Add trailing / for folder path if there isn't
|
296 | 322 | args.dir += '/' if args.dir[-1:] != '/' else ''
|
297 | 323 |
|
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 |
| - |
318 | 324 | if not (args.blocks or args.locations):
|
319 | 325 | parser.print_help()
|
320 | 326 | sys.exit('Missing required address and location csv files.')
|
|
0 commit comments