From f24d15873fe6db464821d56b1f45c0c95343a941 Mon Sep 17 00:00:00 2001 From: Biswakalyan Bhuyan Date: Thu, 18 Aug 2022 15:08:47 +0530 Subject: program --- waldl.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 waldl.py 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 ") + quit() + +query = sys.argv[1].replace(' ', '+') +wallpapers = wallpaper_search_api(query) +for wallpaper in wallpapers: + download_wallpaper(wallpaper) + -- cgit v1.2.3-59-g8ed1b