Decorate your desktop with random wallpapers with GoodFon
I sat one evening, there was nothing to do, and I decided to slightly diversify my desktop by writing a small script that put on it a random picture from GoodFon . The language I chose was simple - simple, scripted, powerful, namely, incomparable Python.
If you want to make yourself and your desktop nice - details are under the cut.
Since python is a cross-platform language, this script will work not only under linux, but also under windows.
So, let's begin.
GoodFon has already done some of the work for us by implementing the ability to view random wallpapers on the site. So we don’t have to choose them ourselves by parsing a bunch of pages. We can only climb onto the page with random wallpapers, select one of the pictures and put it on the desktop as wallpaper. Goodfon has one drawback - an unregistered user can download no more than 10 EMNIP images per day, but we don’t need to.
I decided to make the script more interesting than just performing its function, so it will be replete with slightly excessive “communication” with the user.
To get started, we connect the necessary ones.
I have made this script in two versions, I will talk about the second in the next post so as not to make the post too long, but in the first version there is a blank for the second, namely the "default mode", described in a separate function. The choice of the operating mode is carried out by the configuration text file conf.txt.
First, we need to determine the screen resolution so that the picture gets out in it, and not at the maximum size. To do this, we will use the winAPI function GetSystemMetrics.
where 0 and 1 are the resolutions for height and width, respectively.
The part of the code where we read the file, select the operation mode, and so on, I will omit the rest, since this is not so important, we will proceed immediately to the selection, downloading and installation of the picture on the wallpaper.
Here we follow the link of random wallpapers , read the page and use regular expressions to pull out links to pages with pictures from it. The link to the image itself consists of a link to the page with the image + resolution in the format 1900x800 + extension .jpg. After this, a completely simple step remains - to create a picture file, read the deleted file into it and set it as wallpaper.
I had to tinker with the last point, since, according to Google, installing wallpapers in Windows 7 is not as simple as, say, in XP. While searching for a solution to the problem, I came across an example where a small WallpaperChanger application was used for this, which I used. To do this, you need to run the application itself, indicating to it as an argument our downloaded file.
That's it, the script is completed, and we have random wallpapers on our table. You can put such a script into startup, and each time you start your computer you will have new wallpapers =)
In order to transfer it to Linux, you need to change all a couple of lines, namely, instead of winAPI, you can use X functions (or some other , I did not delve into this topic, because there was no point), but to set the wallpaper use, for example, feh.
The entire WallpaperChanger script
If you want to make yourself and your desktop nice - details are under the cut.
Since python is a cross-platform language, this script will work not only under linux, but also under windows.
So, let's begin.
Materiel
GoodFon has already done some of the work for us by implementing the ability to view random wallpapers on the site. So we don’t have to choose them ourselves by parsing a bunch of pages. We can only climb onto the page with random wallpapers, select one of the pictures and put it on the desktop as wallpaper. Goodfon has one drawback - an unregistered user can download no more than 10 EMNIP images per day, but we don’t need to.
Implementation
I decided to make the script more interesting than just performing its function, so it will be replete with slightly excessive “communication” with the user.
To get started, we connect the necessary ones.
import sys
import time
from win32api import GetSystemMetrics
import urllib2
import os.path
import random
import re
import subprocess
import time
I have made this script in two versions, I will talk about the second in the next post so as not to make the post too long, but in the first version there is a blank for the second, namely the "default mode", described in a separate function. The choice of the operating mode is carried out by the configuration text file conf.txt.
First, we need to determine the screen resolution so that the picture gets out in it, and not at the maximum size. To do this, we will use the winAPI function GetSystemMetrics.
print "Разрешение вашего экрана - ", GetSystemMetrics(0), "x", GetSystemMetrics(1)
where 0 and 1 are the resolutions for height and width, respectively.
The part of the code where we read the file, select the operation mode, and so on, I will omit the rest, since this is not so important, we will proceed immediately to the selection, downloading and installation of the picture on the wallpaper.
print "Дефолтный режим запущен. Начинаем сканирование гудфона..."
print "Начинаем выбор и загрузку картинки..."
page = urllib2.urlopen("http://www.goodfon.ru/mix.html")
html = page.read()
p = re.compile(r"/wallpaper/[0-9]+\.html")
allWal = p.findall(html)
walInd = random.randint(0, 41)
p2 = re.compile(r"[0-9]+")
walIndex = p2.findall(allWal[walInd])
newUrl = "http://www.goodfon.ru/image/" + walIndex[0] + "-" + str(GetSystemMetrics(0)) + "x" + str (GetSystemMetrics(1)) + ".jpg"
filename = walIndex[0] + "-" + str(GetSystemMetrics(0)) + "x" + str(GetSystemMetrics(1)) + ".jpg"
print filename
print "Файл найден, начинаем закачку..."
jpeg = urllib2.urlopen(newUrl)
outputJpeg = open(filename, 'wb')
outputJpeg.write(jpeg.read())
outputJpeg.close()
print "Файл закачен. Устанавливаем обои..."
wallpaperPath = os.path.abspath(os.curdir) + "\\" + filename
cmd = "WallpaperChanger.exe" + " " + wallpaperPath
subprocess.Popen(cmd, shell = True)
print "Обои установлены. Удачной работы!"
time.sleep(5)
Here we follow the link of random wallpapers , read the page and use regular expressions to pull out links to pages with pictures from it. The link to the image itself consists of a link to the page with the image + resolution in the format 1900x800 + extension .jpg. After this, a completely simple step remains - to create a picture file, read the deleted file into it and set it as wallpaper.
I had to tinker with the last point, since, according to Google, installing wallpapers in Windows 7 is not as simple as, say, in XP. While searching for a solution to the problem, I came across an example where a small WallpaperChanger application was used for this, which I used. To do this, you need to run the application itself, indicating to it as an argument our downloaded file.
That's it, the script is completed, and we have random wallpapers on our table. You can put such a script into startup, and each time you start your computer you will have new wallpapers =)
In order to transfer it to Linux, you need to change all a couple of lines, namely, instead of winAPI, you can use X functions (or some other , I did not delve into this topic, because there was no point), but to set the wallpaper use, for example, feh.
The entire WallpaperChanger script