I created a simple script that is used to implement Google's Chromecast wallpaper-images as the wallpaper of my Gnome desktop.
The script extract the images from 2 different sources:
- A static webpage containing a collection of the Chromecast images
- A dynamic (javscript driven) webpage that shows a different wallpaper at an interval set by Google.
The reasons I use two different sources are:
- I already had a working script that used only the static page & wanted to challenge myself
- When only using the dynamic page somehow the first image when loading that webpage is always the same (or one from a very minor subset with little variation) and I want to see an immediately different wallpaper each time I run the script.
My current code looks like this:
#!/usr/bin/env python
"""This script runs in the background to download wallpapers used by Google's
chromecast devices and set them as a desktop wallpaper in Linux environments
using Gnome desktop. The wallpaper that is being set will change
automatically within a given interval."""
import io
import os
import subprocess
import time
from random import shuffle
from bs4 import BeautifulSoup
from PIL import Image
from pyvirtualdisplay import Display
import requests
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
def first_picture():
"""Retrieve a random image from a non-interactive website. This
will be the first image to which the Gnome desktop will be set. It
will (1) give the user visual feedback that the script is working
and (2) prevent the same image (or minor set of images) to be shown
whenever the script is run."""
request = requests.get('https://github.com/dconnolly/'\
'chromecast-backgrounds')
content = request.content
soup = BeautifulSoup(content, features="lxml")
images = soup.find_all("img")
sourcelist = []
current_dir = os.path.dirname(os.path.realpath(__file__))
for image in images:
if "camo" in image["src"]:
sourcelist.append(image["src"])
# Implemented a try-except because pil_image sometimes raises an error:
# raise IOError("cannot identify image file %r" % (filename if filename
# else fp))
# OSError: cannot identify image file <_io.BytesIO object at
# 0x66d3bea6d3b0>
try:
shuffle(sourcelist)
raw_image = requests.get(sourcelist[0])
pil_image = Image.open(io.BytesIO(raw_image.content))
except IOError:
first_picture()
temp_local_image_location = (current_dir + "/interactive_wallpaper."
+ pil_image.format)
pil_image.save(temp_local_image_location)
subprocess.Popen(["/usr/bin/gsettings", "set",
"org.gnome.desktop.background",
"picture-uri", "'" + temp_local_image_location + "'"],
stdout=subprocess.PIPE)
time.sleep(40)
def change_desktop(image_source):
"""Tap into the dynamic webpage that is displaying a different
wallpapers-like images at a given interval."""
request = requests.get(image_source)
image = Image.open(io.BytesIO(request.content))
image_format = image.format
current_dir = os.path.dirname(os.path.realpath(__file__))
temp_local_image_location = (current_dir + "/interactive_wallpaper."
+ image_format)
image.save(temp_local_image_location)
subprocess.Popen(["/usr/bin/gsettings", "set",
"org.gnome.desktop.background",
"picture-uri", "'" + temp_local_image_location + "'"],
stdout=subprocess.PIPE)
time.sleep(20)
def set_display_and_start_browser():
"""Start virtual display and selenium driver. This is needed
for the selenium-initiated browser to grab the dynamically
loaded images dfrom the webpage"""
display = Display(visible=0, size=(1920, 1080))
display.start()
desired_capabilities = DesiredCapabilities.CHROME.copy()
desired_capabilities['connection'] = "keep-alive"
browser = webdriver.Chrome(desired_capabilities=desired_capabilities)
url = "https://clients3.google.com/cast/chromecast/home/"
browser.get(url)
first_picture()
while True:
element = browser.find_element_by_id("picture-background")
image_source = element.get_attribute("src")
change_desktop(image_source)
time.sleep(20)
Any tips, insights, remarks, opinions, etc. more then welcome!
Curious to learn what you think.