diff options
author | Biswakalyan Bhuyan <biswa@surgot.in> | 2022-08-18 15:08:47 +0530 |
---|---|---|
committer | Biswakalyan Bhuyan <biswa@surgot.in> | 2022-08-18 15:08:47 +0530 |
commit | f24d15873fe6db464821d56b1f45c0c95343a941 (patch) | |
tree | b82c49828511fb26a5464aa196d3ffcb32728a42 | |
parent | 1650be777d09223439ddd167bd6f25f693654840 (diff) | |
download | waldl-f24d15873fe6db464821d56b1f45c0c95343a941.tar.gz waldl-f24d15873fe6db464821d56b1f45c0c95343a941.tar.bz2 waldl-f24d15873fe6db464821d56b1f45c0c95343a941.zip |
program
-rwxr-xr-x | waldl.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/waldl.py b/waldl.py new file mode 100755 index 0000000..9e7c0ba --- /dev/null +++ b/waldl.py @@ -0,0 +1,46 @@ +#!/usr/bin/python3 +import os +import sys +import requests +import threading +import random +import pathlib +import string + +DOWNLOAD_DIR = f"{str(pathlib.Path.home())}/pix/wall" + +def generate_id(): + return ''.join(random.choices(string.ascii_lowercase + string.digits, k=6)) + +def get_ext(url): + ext = os.path.splitext(url)[1] + return ext + +def download_wallpaper(url): + print(f"Downloading {url}") + res = requests.get(url, allow_redirects=True) + download_path = f"{DOWNLOAD_DIR}/{generate_id()}{get_ext(url)}" + open(download_path, 'wb').write(res.content) + print(f"Downloading done of {url}") + + +def wallpaper_search_api(query): + query_url = f"https://wallhaven.cc/api/v1/search?q={query}" + res = requests.get(query_url) + response = res.json() + dl_links = [] + for wallpaper in response["data"]: + dl_links.append(wallpaper["path"]) + + return dl_links + +os.makedirs(DOWNLOAD_DIR, exist_ok=True) +if len(sys.argv) < 2: + print("Usage waldl.py <search_query>") + quit() + +query = sys.argv[1].replace(' ', '+') +wallpapers = wallpaper_search_api(query) +for wallpaper in wallpapers: + download_wallpaper(wallpaper) + |