
    c0i              	           d Z ddlZddlZddlmZ ddlZddlmZ ddlmZm	Z	 dde
deded	efd
Zd Zedk(  r e        yy)z
Downloads OSM PBF files from Geofabrik if they don't already exist.

Usage:
    python -m scripts.download_pbf ohio
    python -m scripts.download_pbf egypt india thailand
    python -m scripts.download_pbf --list
    N)Path)tqdm)get_geofabrik_urllist_available_locationslocation
output_dirforcereturnc           
         | j                         } 	 t        |       }|t        j                         }|dz  | z  }|j                  dd       ||  dz  }|j                         r4|s2|j                         j                  dz  }t        d	| d
|dd       |S t        d| j                          d       t        d        	 t        j                  |dd      5 }|j                          t!        |j"                  j                  dd            }	d}
t%        |d      5 }t'        |	ddd|  d      5 }|j)                  |
      D ]-  }|j+                  |       |j-                  t/        |             / 	 ddd       ddd       ddd       |j                         j                  dk  rt        d      |j                         j                  dz  }t        d| d
|dd       |S # t        $ r.}t        d|        t	        j
                  d       Y d}~d}~ww xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# t        j0                  j2                  $ rM}t        d|        |j                         r|j5                          t	        j
                  d       Y d}~yd}~wt6        $ rM}t        d|        |j                         r|j5                          t	        j
                  d       Y d}~yd}~ww xY w)aD  
    Download a PBF file from Geofabrik if it doesn't exist.
    
    Args:
        location: Location name (e.g., 'ohio', 'egypt')
        output_dir: Directory to save the file (default: output/{location}/)
        force: Force re-download even if file exists
        
    Returns:
        Path to the downloaded file
    u   ❌    NoutputT)parentsexist_okz.osm.pbf   u   ✅ File already exists:  (.1f MB)u!   ⬇️  Downloading OSM data for z...u
   🔗 URL: x   )streamtimeoutzcontent-lengthr   i    wbBi   )totalunit
unit_scaleunit_divisordesc)
chunk_sizei  u<   Downloaded file seems too small — possible download error.u   ✅ Download complete: u   ❌ Download failed: u   ❌ Error: )lowerr   
ValueErrorprintsysexitr   cwdmkdirexistsstatst_size
capitalizerequestsgetraise_for_statusintheadersopenr   iter_contentwriteupdatelen
exceptionsRequestExceptionunlink	Exception)r   r   r	   urlebase_dirfilename	file_sizer
total_size
block_sizefbarchunks                 scripts/download_pbf.pydownload_pbfrD      s    ~~H) 88:(83
TD1xj11H MMO++{;	)(2i_DIJ 
-h.A.A.C-DC
HI	Jse
"\\#dC8A QYY]]+;Q?@JJh%D ! z*- ^^z^BEGGENJJs5z* C-% 9" ==?""Z/[\\MMO++{;	'zIc?$GH[  QCj8- -%% 980 // %aS)*??OO A3 ??OO	s   G, I
 AH>"H26AH&:H2H>
A!I
 ,	H#5#HH#&H/+H22H;	7H>>II
 
L'AJ//L;ALLc            
         t        j                  dt         j                  d      } | j                  ddd       | j                  dd	d
       | j                  dd	d       | j                  dt        d       | j                         }|j                  rt                y |j                  s0| j                          t        d       t        j                  d       t        dt        |j                         d       g }g }|j                  D ]B  }	 t        ||j                  |j                         }|j#                  ||f       t                D t        d       t        dt        |       dt        |j                                |rHt        d       |D ]8  \  }}|j'                         j(                  dz  }t        d| d| d|d d!       : |r,t        d"t        |              |D ]  }t        d|         t        d       y # t$        $ r.}t        d| d| d       |j#                  |       Y d }~3d }~ww xY w)#Nz%Download OSM PBF files from Geofabrika  
Examples:
  Download single location:
    python -m scripts.download_pbf ohio
    
  Download multiple locations:
    python -m scripts.download_pbf ohio kentucky new-york
    python -m scripts.download_pbf egypt india thailand
    
  Force re-download:
    python -m scripts.download_pbf ohio --force
    
  List available locations:
    python -m scripts.download_pbf --list
        )descriptionformatter_classepilog	locations*z5Location names to download (e.g., ohio, egypt, india))nargshelpz--list
store_truez,List all available locations in the registry)actionrL   z--forcez%Force re-download even if file existsz--output-dirz5Custom output directory (default: output/{location}/))typerL   u5   
❌ Error: Please provide at least one location namer   u   
📥 Downloading z location(s)...
)r   r	   u   ❌ Failed to download z: 
z<============================================================u   ✅ Successfully downloaded: /u   
📁 Downloaded files:r   u     • r   r   r   u   
❌ Failed: )argparseArgumentParserRawDescriptionHelpFormatteradd_argumentr   
parse_argslistr   rI   
print_helpr!   r"   r#   r3   rD   r   r	   appendr7   r'   r(   )parserargs
downloadedfailedr   filepathr9   r<   s           rC   mainr_   `   sH   $$; <<F( D   ;   4   D   D yy " >>FG 
DNN 344E
FGJFNN
	$#??jjH
 x23G # 
(O	)#j/):!C<O;P
QR()",Hh //;?IF8*Bxj9S/FG #- s6{m,-HF8*%&  
(O'  	$+H:Rs"=>MM(##	$s   ?H	I#II__main__)NF)__doc__rR   r"   pathlibr   r*   r   scripts.geofabrik_registryr   r   strboolrD   r_   __name__     rC   <module>ri      sa     
    RI3 ID I IQU IX\~ zF rh   