Sunday, October 6, 2013

Picture of the Day - Part 3

Continuing on from Part 1 and Part 2.

I wasn't happy with the results of the Automator workflow.
It was nice to have a play around and see what it could do, but it seemed that I was wanting to do some more advanced functions, like text append, that just weren't available or easy in Automator.

So I used my Bash scripting power to come up with another approach that gave me the flexibility that I needed.

#build up the url
DATE=`date +%Y-%m-%d`

temp=$(/opt/local/bin/lynx -dump -nonumbers -listonly -image_links $MYURL | grep en\. | grep -E "(.jpg|.JPG)" | grep -v thumb)

temp2=$(/opt/local/bin/lynx -dump -nonumbers -listonly -image_links $temp | grep upload | grep -E "(.jpg|.JPG)" | grep -v thumb | uniq)

#get the image
/opt/local/bin/wget --no-check-certificate -O /Users/jono/Documents/Backgrounds/1.jpg $temp2

To break it down,
First I would build up the URL in the form that I needed.

Then I would use the command line browser lynx to grab the image links out of that URL. I would filter the results on some text like 'en.' (to make sure I hit and '.jpg' (to make sure I just got jpg results) but I didn't want any URLs returned with the text 'thumb' - as they were thumbnails, so I used the grep filter 'grep -v thumb' to NOT include those results.

So I should now have the URL of the wikipedia file document in the form

That file document, again had just a larger version of the thumbnail on that page, but it linked to the original higher resolution image.

So I ran lynx again, applied pretty much the same filters, and made sure that I only had unique results (the pipe to uniq command)

I now have the link to the full resolution uploaded file and I use the command wget to save it straight to the location I wanted named as the filename I wanted (1.jpg)

No need to save it and then move and rename, like I tried to do in Automator.

Now my geektool geeklet that is looking in that location every 5 secs or so kicks in and changes the desktop background to the newly download picture.

Now you can just set that up as a login script and you can have dynamic fresh Picture of the Day each time you log in.

No comments:

Post a Comment