📜 ⬆️ ⬇️

We decorate the desktop with random wallpapers with GoodFon

I was sitting one evening, there was nothing to do, and I decided to slightly diversify my desktop by writing a small script writing a random picture with GoodFon . I chose a simple language - simple, scripting, powerful, namely, incomparable Python.

If you want to make yourself and your desktop nice - the details 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 select them by the method of parsing a heap of pages. We can only climb on the page with a random wallpaper, select one of the pictures and put it on your 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 more.

Implementation



I decided to make the script more interesting than just performing my function, so it will be replete with slightly excessive "communication" with the user.

First 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 this script made in two versions, I will tell 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 mode of operation is selected by the configuration text file conf.txt.

First we need to determine the screen resolution, so that the image is selected in it, and not in the maximum size. To do this, we will use the winAPI GetSystemMetrics function.

 print "   - ", GetSystemMetrics(0), "x", GetSystemMetrics(1) 

where 0 and 1 are the resolutions for height and width, respectively.

We omit the part of the code where we read the file, select the mode of operation, and so on, since this is not so important, we will go straight to choosing, downloading and installing images 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 1900x800 format + the .jpg extension. After this, a completely simple step remains - to create an image file, read the deleted file into it and set it as wallpaper.

I had to tinker with the last point, because, according to Google, installing wallpaper in Windows 7 is not as simple as, say, in XP. During the search for a solution to the problem, I came across an example where a small application, WallpaperChanger, was used for this, which I used. To do this, you need to run the application itself, specifying our downloaded file as an argument.

Everything, the work of the script is completed, and we have random wallpapers on our table. Such a script can be put in autoload, and each time you start your computer you will have a new wallpaper =)

In order to transfer it to Linux, you need to change all a couple of lines, namely, instead of winAPI to determine whether to use the functions of X (or which others, I did not go into this topic, because there was no point), but to install wallpaper, for example, feh.

Whole script
WallpaperChanger

Source: https://habr.com/ru/post/134521/


All Articles