# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/10_urls.ipynb.

# %% auto 0
__all__ = ['urljoin', 'stream']

# %% ../nbs/10_urls.ipynb 5
import os
from pathlib import Path

try: import requests
except: ...

try: from urllib3.util.url import parse_url
except: ...

try: from tqdm.auto import tqdm
except: ...

# %% ../nbs/10_urls.ipynb 7
from quac import pathlike, pathq, strq
from nchr import SLASH

# %% ../nbs/10_urls.ipynb 9
from .util import fullpath

# %% ../nbs/10_urls.ipynb 11
def urljoin(*parts: str) -> str:    
    url = SLASH.join([s.strip(SLASH) for s in parts])
    return parse_url(url).url

# %% ../nbs/10_urls.ipynb 12
def stream(uri: pathlike, file: pathq = None, desc: strq = None):
    if file is None: file = fullpath(Path('~/Downloads') / os.path.basename(uri))    
    if desc is None: desc = file.stem

    response = requests.get(uri, stream=True)
    total = int(response.headers.get('content-length', 0))
    
    with tqdm.wrapattr(open(file, 'wb'), 'write', miniters=1, desc=desc, total=total) as fout:
        for chunk in response.iter_content(chunk_size=4096):
            fout.write(chunk)
