Created
February 3, 2023 09:59
-
-
Save wooparadog/63a5ef352ce5c2fc607590ac75a9b680 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/python | |
import os | |
import random | |
import string | |
import shutil | |
import time | |
from argparse import ArgumentParser | |
from typing import Any, Dict | |
import dbus | |
import dbus.service | |
from dbus.mainloop.glib import DBusGMainLoop | |
import requests | |
DBusGMainLoop(set_as_default=True) | |
class WallpaperDBus(dbus.service.Object): | |
def __init__(self, object_path): | |
dbus.service.Object.__init__(self, dbus.SessionBus(), object_path) | |
@dbus.service.signal(dbus_interface="org.freedesktop.portal.Wallpaper") | |
def SetWallpaperURI(self, uri, options): | |
print("DBus change wallpaper", uri, options) | |
dbus_handler = WallpaperDBus("/org/freedesktop/portal/desktop") | |
def get_wallpaper(q, categories, purity, sorting, atleast, order, apikey, ratios): | |
page = 1 | |
seed = "".join( | |
random.choice(string.ascii_uppercase + string.digits) for _ in range(6) | |
) | |
while True: | |
params: Dict[str, Any] = dict( | |
apikey=apikey, | |
q=q, | |
categories=categories, | |
purity=purity, | |
sorting=sorting, | |
atleast=atleast, | |
order=order, | |
ratios=ratios, | |
page=page, | |
) | |
if sorting == "random": | |
params.update(seed=seed) | |
try: | |
r = requests.get("https://wallhaven.cc/api/v1/search", params=params) | |
except Exception as e: | |
print(f"failed to call api: {e}") | |
yield None | |
continue | |
result = r.json() | |
images = result.get("data", []) | |
if not images: | |
break | |
for image in images: | |
yield image | |
page += 1 | |
def save_wallpaper(uri): | |
filename = os.path.basename(uri) | |
full_path = os.path.join(os.path.expanduser("~/Wallpaper/"), filename) | |
if os.path.exists(full_path): | |
return full_path | |
print("Downloading new wallpaper... ", uri) | |
r = requests.get(uri, stream=True) | |
if r.status_code == 200: | |
with open(full_path, "wb") as f: | |
r.raw.decode_content = True | |
shutil.copyfileobj(r.raw, f) | |
return full_path | |
def main( | |
q, categories, purity, sorting, atleast, order, apikey, ratios, loop_sec: int = 0 | |
): | |
for wallpaper in get_wallpaper( | |
q, categories, purity, sorting, atleast, order, apikey, ratios | |
): | |
if wallpaper: | |
try: | |
full_path = save_wallpaper(wallpaper["path"]) | |
dbus_handler.SetWallpaperURI( | |
full_path, | |
dict( | |
width=wallpaper["dimension_x"], height=wallpaper["dimension_y"] | |
), | |
) | |
except Exception as e: | |
print(f"Failed to download image: {e}") | |
if loop_sec: | |
time.sleep(loop_sec) | |
else: | |
return | |
if __name__ == "__main__": | |
parser = ArgumentParser() | |
parser.add_argument( | |
"-q", | |
"--search", | |
dest="q", | |
help="""tagname / -tagname / +tag1 +tag2 / +tag1 -tag2 / | |
@username / id:123 - Exact tag search (can not be combined) / | |
type:{png/jpg} / like:wallpaper ID""", | |
default="id:9552", | |
) | |
parser.add_argument( | |
"-c", | |
"--categories", | |
dest="categories", | |
help="categories: 100/101/111*/etc (general/anime/people)", | |
default="111", | |
) | |
parser.add_argument( | |
"-p", | |
"--purity", | |
dest="purity", | |
help="purity: 100*/110/111/etc (sfw/sketchy/nsfw)", | |
default="100", | |
) | |
parser.add_argument( | |
"-s", | |
"--sorting", | |
dest="sorting", | |
help="sorting: date_added*, relevance, random, views, favorites, toplist", | |
default="date_added", | |
) | |
parser.add_argument( | |
"-a", "--atleast", dest="atleast", help="atleast: 1920x1200 / etc", default="" | |
) | |
parser.add_argument( | |
"-o", "--order", dest="order", help="order: *desc / asc", default="desc" | |
) | |
parser.add_argument( | |
"-r", | |
"--ratios", | |
dest="ratios", | |
help="ratios: 16x9 / 16x10 / landscape / portrait", | |
) | |
parser.add_argument("-k", "--apikey", dest="apikey", help="apikey", default="") | |
parser.add_argument( | |
"-l", | |
"--loop-sec", | |
type=int, | |
dest="loop_sec", | |
help="loop time in seconds", | |
default=0, | |
) | |
options = parser.parse_args() | |
main( | |
q=options.q, | |
apikey=options.apikey, | |
categories=options.categories, | |
purity=options.purity, | |
sorting=options.sorting, | |
atleast=options.atleast, | |
order=options.order, | |
ratios=options.ratios, | |
loop_sec=options.loop_sec, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment