|
7 | 7 | import glob |
8 | 8 | import logging |
9 | 9 | import os |
| 10 | +import shutil |
10 | 11 | import sys |
11 | 12 | from io import BytesIO |
12 | 13 | from pathlib import Path |
|
20 | 21 | import py7zr |
21 | 22 | import requests |
22 | 23 | from _helpers import BASE_DIR, aggregate_fuels, get_conv_factors |
| 24 | +from googledrivedownloader import download_file_from_google_drive as download_gdrive |
23 | 25 |
|
24 | 26 | _logger = logging.getLogger(__name__) |
25 | 27 |
|
@@ -380,14 +382,32 @@ def calc_sector(sector): |
380 | 382 | os.remove(f) |
381 | 383 |
|
382 | 384 | # Feed the dictionary of links to the for loop, download and unzip all files |
383 | | - for key, value in d.items(): |
384 | | - zipurl = value |
385 | | - |
386 | | - with urlopen(zipurl) as zipresp: |
387 | | - with ZipFile(BytesIO(zipresp.read())) as zfile: |
388 | | - zfile.extractall(os.path.join(BASE_DIR, "data/demand/unsd/data")) |
| 385 | + try: |
| 386 | + for key, value in d.items(): |
| 387 | + zipurl = value |
| 388 | + |
| 389 | + with urlopen(zipurl) as zipresp: |
| 390 | + with ZipFile(BytesIO(zipresp.read())) as zfile: |
| 391 | + zfile.extractall( |
| 392 | + os.path.join(BASE_DIR, "data/demand/unsd/data") |
| 393 | + ) |
| 394 | + |
| 395 | + path = os.path.join(BASE_DIR, "data/demand/unsd/data") |
| 396 | + except: |
| 397 | + _logger.warning( |
| 398 | + f"Could not open the file from {zipurl}. " |
| 399 | + "Using the data stored in google drive." |
| 400 | + ) |
| 401 | + download_gdrive( |
| 402 | + file_id="1VUV0X-tTQECi2pHdE5EWXjPI2yeCdk6F", |
| 403 | + dest_path=os.path.join(BASE_DIR, "data/demand/unsd/unsd.zip"), |
| 404 | + unzip=True, |
| 405 | + ) |
389 | 406 |
|
390 | | - path = os.path.join(BASE_DIR, "data/demand/unsd/data") |
| 407 | + # clean up __MACOSX folder if it exists |
| 408 | + macosx_root_path = os.path.join(BASE_DIR, "data/demand/unsd/__MACOSX") |
| 409 | + if os.path.exists(macosx_root_path): |
| 410 | + shutil.rmtree(macosx_root_path) |
391 | 411 |
|
392 | 412 | # Get the files from the path provided in the OP |
393 | 413 | all_files = list( |
|
0 commit comments