How my new awesome wallpaper is powered by Powershell

Introduction
Last week, in the middle of my timetable gap, waiting with my German friend for the Finnish for Exchange Students class he showed me the Apple maps application, and I really liked how you are able to see projected sunlight in Earth, and as I really liked space stuff pictures, I thought it would be nice if I’d have a dynamic wallpaper that changes automatically where I can see this, thus comparing sunlight hours between my home country and Turku (Finland), where I’m living nowadays.

At first, I saw my own skills so limited since I mostly know about PHP, C, Java, and a little bit of other programming languages. After this I made me promise to learn some day Python, but anyway, knowing about C++ would also be okey in this situation. My final decision? Powershell!

Getting down to work
I have never programmed using Powershell, and I was quite surprised how a lot of things can be done in a so easy way.
I researched about this kind of planet and sunlight images, and I found a Czech webpage where I can select coordinates and zoom, so I just adjusted this to my needs and this is the result.

As I have never programmed in PS, I researched how to change and update the wallpaper, and although it was quite easy to implement (only two lines), I decided to rely on the operating system itself (it is always suppose to have higher perfomance, rather than programming oneself the same) and the only work I did in PS is to download a couple of images which will be rotating every 5 minutes (also they are downloaded every 5 minutes).
Note: I selected a 5 minutes interval for mainly two reasons: I am not able to send too many requests to the Czech wesite, I think it’s enough and this way I don’t spend too many resources.

I’m also interested in making some kind of video where I can see the sunlight at the same hour every day, in order to see how the sunlight changes during the year, so I also programmed this in the script below.

Source code

<#
 # This function will save an image given source and destination
 # @param source of the image.
 # @param destination where the image will be saved.
#>
Function SaveImage
{
    #Get the params and cast them into strings
    $source = [string]$args[0]
    $destination = [string]$args[1]
    
    #Download it, and wait for 5min
    $wc.DownloadFile($source, $destination)
    Start-Sleep 300

}


$source = "http://www.fourmilab.ch/cgi-bin/Earth?img=learth&imgsize=1000&opt=-l&lat=50&ns=North&lon=20&ew=East&alt=2000&date=0&dynimg=y";
$wc = New-Object System.Net.WebClient
$date = Get-Date
#Name for my picture I will store in the other folder
$pictureName = "$($date.Day)-$($date.Month)-$($date.Year).jpeg"
#Other folder where I will store all the pictures to make the movie
$pictureFolder = "C:\Users\JuanMiguel\Pictures\light\"
$pictureFullPath = "$pictureFolder$pictureName"
#Where I will store the images I will use as wallpaper
$destination = "C:\Users\JuanMiguel\image"

#I will save the picture always after 19h
while($date.Hour -lt 19)
{
    SaveImage $source "$($destination)1.jpeg"
    SaveImage $source "$($destination)2.jpeg"
    #I take the date again
    $date = Get-Date
}

#Check if that picture exists, otherwise I will save it
if(!(Test-Path $pictureFullPath))
{
    SaveImage $source $pictureFullPath
}

#Endless loop
while(1)
{
    SaveImage $source "$($destination)1.jpeg"
    SaveImage $source "$($destination)2.jpeg"
}

As PowerShell is a high level programming language (very high indeed), I tried to program the script in a non-traditional way, checking the less information as possible. Of course, it would have been cleaner if I’d only used one infinite loop asking all the time about the hour and checking if the image I want to save exists, but that way I have to ask many times to the computer, therefore, more work, more wasted resources, less battery, etc. Sometimes it’s better to think more about the performance and less about how easy to read the code is, mainly when we have so simple code like this.


17h and almost dark.. but winter will be fun!

Leave a Reply

Your email address will not be published. Required fields are marked *