|
23 | 23 | import subprocess
|
24 | 24 | import tempfile
|
25 | 25 | import csv
|
| 26 | +from requests.exceptions import RequestException, Timeout |
26 | 27 | from dateutil.relativedelta import relativedelta
|
27 | 28 | from aclimate_cpt.tools_d_r import DownloadProgressBar,DirectoryManager
|
28 | 29 |
|
@@ -959,53 +960,62 @@ def best_GI(self,rutas):
|
959 | 960 | return best_gi[0]
|
960 | 961 |
|
961 | 962 |
|
962 |
| - def download_gz_predictor_file(self,url, path ,timeout=300): |
963 |
| - # url = urls[0] |
964 |
| - # path = all_path_down[0] |
965 |
| - code = False |
966 |
| - while code== False: |
967 |
| - try: |
968 |
| - response = requests.get(url, stream=True, timeout=timeout) |
969 |
| - response.raise_for_status() # Check for HTTP errors |
970 |
| - |
971 |
| - if(response.status_code != 200): |
972 |
| - raise Exception("Code status error") |
973 |
| - |
974 |
| - # Obtener el tamaño esperado del archivo del encabezado "Content-Length" |
975 |
| - expected_size = int(response.headers.get('Content-Length', 0)) |
976 |
| - |
977 |
| - # Descargar el archivo y verificar el tamaño |
978 |
| - downloaded_size = 0 |
979 |
| - |
980 |
| - progress_bar = tqdm(total=expected_size, unit='B', unit_scale=True, desc=path.split('/')[-1], miniters=1) |
981 |
| - |
982 |
| - with open(path, 'wb') as f: |
983 |
| - for chunk in response.iter_content(chunk_size=8192): |
984 |
| - downloaded_size += len(chunk) |
985 |
| - f.write(chunk) |
986 |
| - progress_bar.update(len(chunk)) |
987 |
| - progress_bar.close() |
988 |
| - |
989 |
| - if downloaded_size == expected_size: |
990 |
| - print(".gz file downloaded successfully") |
991 |
| - |
992 |
| - # Descomprimir el archivo descargado |
993 |
| - with gzip.open(path, 'rb') as f_in: |
994 |
| - with open(path.replace('.gz',''), 'wb') as f_out: |
995 |
| - shutil.copyfileobj(f_in, f_out) |
996 |
| - os.remove(path) |
997 |
| - print(".gz file uncompressed successfully") |
998 |
| - code = True |
999 |
| - return print("Process done successfully") |
| 963 | + def download_gz_predictor_file(self, url, path ,timeout=1000, force= False): |
| 964 | + |
| 965 | + if force or os.path.exists(path.replace('.gz','')) == False: |
| 966 | + |
| 967 | + if os.path.exists(path.replace('.gz','')): |
| 968 | + os.remove(path.replace('.gz','')) |
| 969 | + |
| 970 | + code = False |
| 971 | + while code== False: |
| 972 | + try: |
| 973 | + response = requests.get(url, stream=True, timeout=timeout) |
| 974 | + response.raise_for_status() # Check for HTTP errors |
1000 | 975 |
|
1001 |
| - else: |
1002 |
| - print(f" Downloaded file size ({downloaded_size} bytes) doesn't match expected size ({expected_size} bytes).") |
| 976 | + if(response.status_code != 200): |
| 977 | + raise Exception("Code status error") |
| 978 | + |
| 979 | + # Obtener el tamaño esperado del archivo del encabezado "Content-Length" |
| 980 | + expected_size = int(response.headers.get('Content-Length', 0)) |
| 981 | + |
| 982 | + # Descargar el archivo y verificar el tamaño |
| 983 | + downloaded_size = 0 |
| 984 | + |
| 985 | + progress_bar = tqdm(total=expected_size, unit='B', unit_scale=True, desc=path.split('/')[-1], miniters=1) |
| 986 | + |
| 987 | + with open(path, 'wb') as f: |
| 988 | + for chunk in response.iter_content(chunk_size=8192): |
| 989 | + downloaded_size += len(chunk) |
| 990 | + f.write(chunk) |
| 991 | + progress_bar.update(len(chunk)) |
| 992 | + progress_bar.close() |
| 993 | + |
| 994 | + if downloaded_size == expected_size: |
| 995 | + print(".gz file downloaded successfully") |
| 996 | + |
| 997 | + # Descomprimir el archivo descargado |
| 998 | + with gzip.open(path, 'rb') as f_in: |
| 999 | + with open(path.replace('.gz',''), 'wb') as f_out: |
| 1000 | + shutil.copyfileobj(f_in, f_out) |
| 1001 | + os.remove(path) |
| 1002 | + print(".gz file uncompressed successfully") |
| 1003 | + code = True |
| 1004 | + return print("Process done successfully") |
| 1005 | + |
| 1006 | + else: |
| 1007 | + print(f" Downloaded file size ({downloaded_size} bytes) doesn't match expected size ({expected_size} bytes).") |
| 1008 | + os.remove(path) |
| 1009 | + except (RequestException, IOError, Timeout) as e: |
| 1010 | + if isinstance(e, Timeout): |
| 1011 | + print(f"Download timed out: {e}. Retrying...") |
| 1012 | + else: |
| 1013 | + print(f"Download failed: {e}") |
1003 | 1014 | os.remove(path)
|
1004 |
| - except (requests.exceptions.RequestException, IOError) as e: |
1005 |
| - print(f"Download failed: {e}") |
1006 |
| - os.remove(path) |
1007 |
| - |
1008 |
| - |
| 1015 | + else: |
| 1016 | + print("\tFile already downloaded!",path) |
| 1017 | + |
| 1018 | + |
1009 | 1019 | return False
|
1010 | 1020 |
|
1011 | 1021 |
|
|
0 commit comments