@@ -19,19 +19,47 @@ def _download_metalinksdb(verbose=True):
1919 """
2020 requests = _check_if_installed ("requests" )
2121
22- METALINKS_URL = "https://figshare.com/ndownloader/files/47567597 "
22+ METALINKS_URL = "https://figshare.com/ndownloader/files/60861292 "
2323
2424 # Define the local filename to save the downloaded database
2525 db_file_name = 'metalinksdb.db'
2626 db_path = os .path .join (os .getcwd (), db_file_name )
2727
28- # Check if the database file already exists
29- if not os .path .exists (db_path ):
30- _logg ("Downloading database..." , verbose = verbose )
31- response = requests .get (METALINKS_URL )
28+ # Check if the database file already exists and is valid
29+ if os .path .exists (db_path ):
30+ # Check if file is empty or corrupted
31+ if os .path .getsize (db_path ) == 0 :
32+ _logg ("Existing database file is empty. Removing and re-downloading..." , verbose = verbose )
33+ os .remove (db_path )
34+ else :
35+ return db_path
36+
37+ # Download the database
38+ _logg ("Downloading database..." , verbose = verbose )
39+ try :
40+ # Figshare requires a browser-like User-Agent to bypass WAF
41+ headers = {
42+ 'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
43+ }
44+ response = requests .get (METALINKS_URL , headers = headers , stream = True , allow_redirects = True )
45+ response .raise_for_status () # Raise an error for bad status codes
46+
3247 with open (db_path , 'wb' ) as f :
33- f .write (response .content )
34- _logg (f"Database downloaded and saved to { db_path } ." , verbose = verbose )
48+ for chunk in response .iter_content (chunk_size = 8192 ):
49+ f .write (chunk )
50+
51+ # Validate the downloaded file
52+ file_size = os .path .getsize (db_path )
53+ if file_size == 0 :
54+ os .remove (db_path )
55+ raise RuntimeError ("Downloaded file is empty. Please check the URL and try again." )
56+
57+ _logg (f"Database downloaded and saved to { db_path } ({ file_size } bytes)." , verbose = verbose )
58+ except (requests .exceptions .RequestException , OSError , RuntimeError ) as e :
59+ # Clean up failed download
60+ if os .path .exists (db_path ):
61+ os .remove (db_path )
62+ raise RuntimeError (f"Failed to download database: { e } " ) from e
3563
3664 return db_path
3765
0 commit comments