Saving webcam images from a website?!

Saving webcam images from a website using a bash script on your Mac (or other UNIX based system).

My wife is an artist and uses several large kilns in her studio. As the studio is not next to our house, she needed to get over to the studio every time she wanted to check the status and or temperature of the firing program of the kilns. So to spend less time and energy on traveling back and forth, I got my wife a webcam, and programmed it to send photos of the digital kiln controller to her website.

The concept of checking the kiln temperature online:

KilnpyrometerKiln computerwebcamWebsitebrowser☺Happy wife☺

The workings of checking the kiln temperature online:

  • The kiln temperature is monitored by the kiln computer via a pyrometer.
  • The kiln computer, which controls the status / temperature / firing program of the kilns, is monitored by a webcam.
  • The webcam sends a photo, every x minutes, of the kiln computer to a website.
  • My wife can check the status / temperature / firing program of the kilns, when ever she wants, with a browser. ==> ☺Happy wife☺

While chit chatting, about the advantages of having the webcam installed, the question arose whether it was possible to store the images and keep some sort of logging. This was a trigger for me to start searching for a solution, eventually I ended up with writing a bash script using wget and without using crontab (crontab is a unix based scheduler).

The concept of logging the kiln temperature:

KilnpyrometerKiln computerwebcamWebsitescriptComputer with images

The workings of logging the kiln temperature:

  • The kiln temperature is monitored by the kiln computer via a pyrometer.
  • The kiln computer, which controls the status / temperature / firing program of the kilns, is monitored by a webcam.
  • The webcam sends a photo, every x minutes, of the kiln computer to a website.
  • A script downloads every x minutes the photo of the kiln computer from a website to a computer.
  • My wife can put the firing program and kiln temperature in a timeline by reading out the stored photos on her computer.

You can download the spreadsheet here. And… she can still check the status / temperature / firing program of the kilns, when ever she wants, with a browser. ==> ☺Happy wife☺

  • There is this thingy though that you have cope with two independent cycles. One cycle uploads an image from the webcam to the website and the other cycle downloads the image from the website to the computer. To make a steady logging I choose to download more images than there were uploaded. In other words… My download cycle is shorter than my upload cycle. This results in double images (which you can remove later on).

wget – concept:

wget –no-check-certificate –output-document=IMAGENAME$(date +%Y-%m-%d_%H:%M:%S).jpg “URL TO YOUR IMAGE”

  • This downloads a file (in this case an image) from a website and saves it to disk while incorporating a date/time stamp in its name.

wget – example – I used:

wget –no-check-certificate –output-document=kilnstatus$(date +%Y-%m-%d_%H:%M:%S).jpg http://www.fleurvandenberg.nl/<secretfolder>/kilnstatus_1.jpg

  • This downloads the file kilnstatus_1.jpg from the website http://www.fleurvandenberg.nl/<secretfolder>/ and saves it to disk while incorporating a date/time stamp in its name resulting in a file called <kilnstatus2018-05-04_07/31/38.jpg>.

This is how you can do the same on your Mac:

Open a Terminal window

Make a folder to store your images in:

mkdir kilnlogging

In your Terminal window go to the folder you just created:

cd ./kilnlogging

From your applications open TextEdit and create a new file called ‘kilnlogging.sh’ in the folder kilnlogging you just created. Or download the script here.

Convert it to plain text by clicking Format > Make Plain Text

Copy, paste and save the code below, between <script> and </script>:

<script>

#!/bin/bash

while true; do

wget –no-check-certificate –output-document=IMAGENAME$(date +%Y-%m-%d_%H:%M:%S).jpg “URL_TO_YOUR_IMAGE” && echo “still in process wait another 10 min ….”

sleep 600

done

</script>

Change the values of IMAGENAME and URL_TO_YOUR_IMAGE to it’s desired values that meet your specifications.

In your Terminal window change executable status of the file you just created by typing:

sudo chmod +x ./kilnlogging.sh

This will give the terminal permission to run the file.

You can now execute the script from your Terminal window by typing:

./kilnlogging.sh

Of course you can stop the script at any moment by pressing:

CTRL Z

After running the kilnlogging script for several days I ended up with quite a collection of the same images. To make them unique I made a ‘kilnlog cleaning script’ called KilnLogCleaner.sh

Concept cleaner script:

md5 -r *.jpg | sort -t ‘ ‘ -k 4 -r | awk ‘BEGIN{lasthash = “”} $1 == lasthash {print $2} {lasthash = $1}’ | xargs rm

This line looks at the md5 values of all images in the folder, and in case the md5 values are the same, it removes the doubles.

From your applications open TextEdit and create a new file called ‘KilnLogCleaner.sh’ in the folder kilnlogging you just created. Or download the script here.

Convert it to plain text by clicking Format > Make Plain Text

Copy, paste and save the code below, between <script> and </script>:

<script>

#!/bin/bash

md5 -r *.jpg | sort -t ‘ ‘ -k 4 -r | awk ‘BEGIN{lasthash = “”} $1 == lasthash {print $2} {lasthash = $1}’ | xargs rm

</script>

In your Terminal window change executable status of the file you just created by typing:

sudo chmod +x ./KilnLogCleaner.sh

This will give the terminal permission to run the file.

You can now execute the script from your Terminal window by typing:

./KilnLogCleaner.sh

Suggestions for improving this article are welcome, please let me know and drop me a line.