Skip to content

Instantly share code, notes, and snippets.

@CTK-WARRIOR
Last active December 4, 2023 21:00
Show Gist options
  • Save CTK-WARRIOR/e474e4f1af1a46ee8405fe929c2b5e91 to your computer and use it in GitHub Desktop.
Save CTK-WARRIOR/e474e4f1af1a46ee8405fe929c2b5e91 to your computer and use it in GitHub Desktop.
Set random anime wallpaper in every x minutes.
# pip install beautifulsoup4 requests schedule
import os
import random
import re
import requests
from bs4 import BeautifulSoup
import ctypes
import schedule
import time
# after x minutes, it will change wallpaper
minutes = 1
print(f"Wallpaper will change every {minutes} minute(s) 🙂")
def set_random_wallpaper():
page = random.randint(1, 500)
# get the html content of the website
htmlContent = requests.get(f"https://wall.alphacoders.com/by_category.php?id=3&name=Anime+Wallpapers&filter=4K+Ultra+HD&page={page}").text
# parse the html content
soup = BeautifulSoup(htmlContent, 'html.parser')
# find the wallpaper container
thumb_containers = soup.find_all('div', attrs={'class': 'thumb-container'})
# list variable to store urls
wallpapers = []
# loop over all container
for container in thumb_containers:
# get preview image url
url = container.div.a.picture.img['src']
# extract digit code from url
codes = re.findall("\d{2,}", url)
# get the format of the image
image_format = url.split(".")[::-1][0]
# generate new url
new_url = url.split("thumb")[0]
# create real high quality image url from above extracted data : <domain>/<digit code>.<image_format>
wallpapers.append(f"{new_url}{codes[1]}.{image_format}")
# send request to get image content
image = requests.get(random.choice(wallpapers))
# save image
with open('temp.jpg', 'wb') as f:
f.write(image.content)
#finally set wallpaper
ctypes.windll.user32.SystemParametersInfoW(20,0,os.path.abspath("temp.jpg"),0)
# Time of schedule
schedule.every(minutes).minutes.do(set_random_wallpaper)
while True:
schedule.run_pending()
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment