Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
agmmnn committed Sep 24, 2021
0 parents commit 2631515
Show file tree
Hide file tree
Showing 12 changed files with 698 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
__pycache__/
.vscode
_other
downs
folder
build
dist
*.spec
*.manifest
*.egg-info
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 agmmnn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
77 changes: 77 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
![screenshot](https://user-images.githubusercontent.com/16024979/134737875-9e9a5daf-d6ed-414b-937f-54e67feb0025.png)
<div align="center">
<a href="https://github.com/agmmnn/polydown">
<img alt="GitHub release (latest by date)" src="https://img.shields.io/github/v/release/agmmnn/polydown"></a>
<a href="https://pypi.org/project/polydown/">
<img alt="PyPI" src="https://img.shields.io/pypi/v/polydown"></a>

Batch downloader for [polyhaven.com](https://polyhaven.com/). You can download hdris, textures and models in all sizes. This project uses Poly Haven's [Public API](https://github.com/Poly-Haven/Public-API).
</div>

# How to Install

- `pip install polydown`

# How to Use
```
$ polydown hdris
# download all available sizes of all hdris into current folder.
> 🔗(polyhaven.com/hdris['all sizes'])=>🏠
```
```
$ polydown <asset_type>
# download all assets of this asset type to the current folder in all available sizes.
# asset types: "hdris", "textures", "models".
```
```
$ polydown textures -c
# list of category in the given asset type.
```
```
$ polydown hdris -f hdris_down -s 2k 4k
# download all hdris with given sizes into "hdris_down" folder.
# /if there is no such folder it will create it./
> 🔗(polyhaven.com/hdris['2k', '4k'])=>🏠(hdris_down)
```
```
$ polydown models -c decorative -f folder -s 1k
# download all models in the "decorative" category into the "folder".
> 🔗(polyhaven.com/models/decorative['all sizes'])=>🏠
Downloaded files structure:
```
![model files structure](https://user-images.githubusercontent.com/16024979/134737874-cc04a42e-5855-4acb-9394-dac08352efee.png)

# Arguments:

```
<asset_type> "hdris, textures, models"
-h, --help show this help message and exit
-f, --folder target download folder.
-c, --category category to download.
-s, --sizes size(s) of downloaded asset files. eg: 1k 2k 4k
-o, --overwrite overwrite if the files already exists. otherwise the current task will be skipped.
-no, --noimgs do not download 'preview, render, thumbnail...' images.
-v, --version show program's version number and exit
```

# To-Do
- [ ] Unit Tests
- [ ] Progressbar for current download task(s)
- [ ] Select the file format to download
- [ ] Download a specific asset, "polydown hdris stuttgart_suburbs"

# Requirements
- Python >3.5

## Dependencies
- [requests](https://pypi.org/project/requests/)
- [rich](https://github.com/willmcgugan/rich)

# License
[MIT](https://github.com/agmmnn/polydown/blob/master/LICENSE)
Empty file added polydown/__init__.py
Empty file.
80 changes: 80 additions & 0 deletions polydown/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import argparse
import datetime
from .cli import polycli

__version__ = "0.1"

ap = argparse.ArgumentParser()
ap.add_argument(
"asset_type",
type=str,
nargs="*",
help='"hdris, textures, models"',
)
ap.add_argument(
"-f",
"--folder",
action="store",
type=str,
default="",
help="target download folder.",
)
ap.add_argument(
"-c",
"--category",
nargs="?",
const="",
help="category to download.",
)
ap.add_argument(
"-s",
"--sizes",
nargs="+",
default=[],
help="size(s) of downloaded asset files. eg: 1k 2k 4k",
)
# ap.add_argument(
# "-ff",
# "--file_format",
# action="store",
# type=str,
# help="target download folder.",
# )
ap.add_argument(
"-o",
"--overwrite",
action="store_true",
default=False,
help="Overwrite if the files already exists. otherwise the current task will be skipped.",
)
ap.add_argument(
"-no",
"--noimgs",
action="store_true",
default=False,
help="Do not download 'preview, render, thumbnail...' images.",
)
ap.add_argument("-v", "--version", action="version", version="%(prog)s v" + __version__)
args = ap.parse_args()


def cli():
if args.asset_type == []:
print("<asset_type> is required.")
exit(0)

execution_start_time = datetime.datetime.now()

try:
polycli(args)
except KeyboardInterrupt:
print("\nKeyboardInterrupt!")

print("Total runtime: {}".format(datetime.datetime.now() - execution_start_time))


if __name__ == "__main__":
try:
cli()
except KeyboardInterrupt:
print("\nKeyboardInterrupt!")
81 changes: 81 additions & 0 deletions polydown/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import os
import requests
import json
from rich import print as rprint

from .poly import Poly


def polycli(args):
# rprint(args, "\n")
asset_type = args.asset_type[0]
folder = args.folder
overwrite = args.overwrite
sizes = args.sizes
category = args.category
noimgs = args.noimgs
s = requests.Session()

# ->🔒asset type->
asset_type_list = list(json.loads(s.get("https://api.polyhaven.com/types").content))
if asset_type not in asset_type_list:
rprint(f"'{asset_type}' is not a valid asset type!")
exit()

# ->🔒category->
if category == "":
js = json.loads(
s.get(f"https://api.polyhaven.com/categories/{asset_type}").content
)
rprint(f"[green]There are {len(js)} available categories for {asset_type}:")
rprint(js)
exit()
elif category != None:
asset_category_list = list(
json.loads(
s.get(f"https://api.polyhaven.com/categories/{asset_type}").content
)
)
if category not in asset_category_list:
rprint(
f"[red]{category} is not a valid category.[/red]\nEnter empty '-c' argument to get the category list of the {asset_type}."
)
exit()

# ->🔒file_format->
# if file_format == None:
# pass
# elif asset_type == "hdris" and file_format not in ["exr", "hdr"]:
# rprint(f"[red]{file_format} is not a valid file format for {asset_type}.[/red]")
# exit()
# elif asset_type in ["models", "textures"] and file_format not in [
# "jpg",
# "png",
# "exr",
# ]:
# rprint(f"[red]{file_format} is not a valid file format for {asset_type}.[/red]")
# exit()

# ->🔒folder->
if os.path.exists(folder):
down_folder = folder + "\\" if folder[:-1] != "\\" else ""
else:
down_folder = os.getcwd() + "\\" + folder + "\\" if folder[:-1] != "\\" else ""
try:
if os.path.exists(down_folder) == False:
os.mkdir(down_folder)
print("Folder not found, creating...")
except Exception as e:
rprint("[red]Error: " + str(e))
exit()

rprint(
f"\n[cyan]🔗(polyhaven.com/{asset_type}"
+ (f"/{category}" if category != None else "")
+ ("['all sizes']" if sizes == [] else str(sizes))
+ f")=>🏠"
+ (f"({folder})" if not folder == "" else "")
+ "\n"
)

Poly(asset_type, s, category, down_folder, sizes, overwrite, noimgs)
Loading

0 comments on commit 2631515

Please sign in to comment.