def download(url): fname = os.path.basename(url) path = os.path.join(hdri_dir, fname) if not os.path.isfile(path): content = rq.get(url, timeout=5).content os.makedirs(hdri_dir, exist_ok=True) with open(path, "wb") as f: f.write(content) return path
def __init__( self, resolution="4k", category="all", hdri_dir="./bpycv_hdri_cache", download=True, ): self.resolution = resolution self.category = category self.hdri_dir = hdri_dir os.makedirs(hdri_dir, exist_ok=True) print('Starting download ".hdr" file from "hdrihaven.com" in side threads') self.downloading = download if self.downloading: setTimeout(self.prepare) self.set_hdr_paths()
def __init__( self, hdri_dir="./bpycv_hdri_cache", resolution="4k", category="all", download=False, debug=False, ): """ Download and manage hdri file from https://hdrihaven.com/hdris/ Parameters ---------- hdri_dir : str, optional hdri dir. The default is "./bpycv_hdri_cache". resolution : str, optional choice [1k, 2k, 4k, 8k, 16k, 19k]. The default is "4k". category : str, optional refer to https://hdrihaven.com/hdris/ side bar choice one [All, Outdoor, Architecture, Building, Europe, Field, Forest, Grass, Hill, Park, Path, River, Road, Rock, Sun1, Tree, View, Skies, Indoor, Studio, Nature, Urban, Night, Sunrise/Sunset, Morning/Afternoon, Midday, Clear, Partly Cloudy, Overcast, High Contrast, Medium Contrast, Low Contrast, Natural Light, Artificial Light]. The default is "all". download : bool, optional If True, auto download from https://hdrihaven.com/hdris/ by another threading using requesets. The default is False. """ self.resolution = resolution self.category = category.lower().replace("/", "-") self.hdri_dir = hdri_dir os.makedirs(hdri_dir, exist_ok=True) self.downloading = download self.debug = debug if self.downloading: print( 'Starting download ".hdr" file from "hdrihaven.com" in side threads' ) if debug: self.prepare() else: setTimeout(self.prepare) self.set_hdr_paths()
def download(url): fname = os.path.basename(url) file_path = os.path.join(hdri_dir, fname) for ext in self.exts: path = f"{file_path}.{ext}" if os.path.isfile(path): return path for ext in self.exts: path = f"{file_path}.{ext}" downlaod_url = f"{url}.{ext}" r = rq.get(downlaod_url, timeout=5) if r.status_code != 200: continue os.makedirs(hdri_dir, exist_ok=True) with open(path, "wb") as f: f.write(r.content) return path raise Exception(f"Con't download {file_path}")
def download(name): try: if self.debug: print(name) prefix = f"{name}_{resolution}" paths = boxx.glob(os.path.join(hdri_dir, prefix + "*")) if len(paths): return paths[0] url = f"https://hdrihaven.com/hdri/?h={name}" html = BeautifulSoup( rq.get(url, timeout=5).text, features="html.parser", ) href = [ a["href"] for a in html.find_all("a") if f"_{resolution}." in a.get("href") ][0] cats = [ a.text for a in html.find( text="Categories:").parent.parent.find_all("a") ] tags = [ a.text for a in html.find( text="Tags:").parent.parent.find_all("a") ] name = f"{prefix}.{'='.join(cats)}.{'='.join(tags)}.{href[-3:]}" path = pathjoin(hdri_dir, name) r = rq.get(href, timeout=5) assert r.status_code == 200 os.makedirs(hdri_dir, exist_ok=True) with open(path, "wb") as f: f.write(r.content) return path except Exception as e: if self.debug: boxx.pred - name boxx.g() raise e