Skip to content

Instantly share code, notes, and snippets.

@gaycookie
Last active May 15, 2021 10:50
Show Gist options
  • Save gaycookie/bb3b90bd76c26ba6b567e4838bdd8e2d to your computer and use it in GitHub Desktop.
Save gaycookie/bb3b90bd76c26ba6b567e4838bdd8e2d to your computer and use it in GitHub Desktop.
Konachan wallpaper downloader and automaticly sets it as your wallpaper!
import requests
import shutil
import os
folder = ".wallpapers"
tags = ["catgirl", "order:random", "width:1920", "height:1080", "rating:questionableless"]
url = "https://konachan.net/post.json?limit=1&tags=" + "+".join(tags)
workingDir = os.getcwd()
downloadDir = os.path.join(workingDir, folder)
def dirHandler():
if not os.path.exists(downloadDir):
os.mkdir(downloadDir)
def oldFileHandler():
for file in os.listdir(downloadDir):
filePath = os.path.join(downloadDir, file)
try:
if os.path.isfile(filePath): os.unlink(filePath)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (filePath, e))
def downloadImage(url):
fileName = url.split("/")[-1]
req = requests.get(url, stream = True)
if req.status_code == 200:
req.raw.decode_content = True
with open(os.path.join(downloadDir, fileName), 'wb') as file:
shutil.copyfileobj(req.raw, file)
setWallpaper(fileName)
else:
return False
def setWallpaper(filename):
file = os.path.join(downloadDir, filename)
# This works only on Linux when using Gnome.
os.system("gsettings set org.gnome.desktop.background picture-uri " + file)
def getRandomImage():
req = requests.get(url)
downloadImage(req.json()[0]["file_url"])
if __name__ == "__main__":
dirHandler()
oldFileHandler()
getRandomImage()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment