Monday, October 7, 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.

 
#!/bin/bash
#build up the url
MYURL="http://en.wikipedia.org/wiki/Template:POTD/";
DATE=`date +%Y-%m-%d`
MYURL=$MYURL$DATE

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 en.wikipedia.org) 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
http://en.wikipedia.org/wiki/File:xxxxxxxxxx.jpg

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.

Sunday, October 6, 2013

Picture of the day - Part 2

Picking up where Part 1 left off, I tried to do a better way to create a picture of the day for Geektool to use.

 As Wikipedia picture of the day was in the standard format

http://en.wikipedia.org/wiki/Template:POTD/yyyy-mm-dd 

I decided to just grab what I needed.
At first I researched using variables in Automator, but it seem to handle appending text,all the forums had people creating Applescripts to build up dynamic text.

So I whipped up the applescript:

 set myURL to "http://en.wikipedia.org/wiki/Template:POTD/"
 
 set myshort to short date string of (current date)
 set myday to items 1 through 2 of myshort
 set mymonth to items 4 through 5 of myshort
 set myyear to items 7 through 10 of myshort
 
 set myURL to myURL & myyear & "-"
 set myURL to myURL & mymonth & "-"
 set myURL to myURL & myday
 
 return myURL

in order to get constancy on the date I had to force the short dat to be 2 digits\2digits\4 digits
You can do this in System Preferences > Language and Text > Formats > Customize
that gave the short date the format 01/01/1970 where I could use the "to items 1 through 2" to pick it apart so I could use it as I wanted.

So I came up with what I hoped was a working workflow:

Except it didn't work.
It dumped the files to folder abc123 okay, again it had the extra images I didn't need as well as the POTD in .jpg format.
Putting in a 'view results' into each step of the workflow, I could see what was going on.
It seems that my 'Filter Finder Items' wasn't working as I expected. It didn't return anything to be renamed, and then moved. Maybe I wasn't using it properly and selecting Desktop > abc123 properly.
Maybe I could work it out, but I still wasn't happy that the Save Images was only getting the thumbnail of the Picture of the Day.

How I solved it in a way I was happy with will be part 3.

Picture of the day - Part 1

Okay, so I use the great app Geektool to change/show some desktop pictures.

I have Geektool configured to look in my backgrounds folder for a file 1.jpg and display it as my background picture. I thought it would be cool if that picture changed daily.

So what site has a great looking picture of the day? Wikipedia of course.

So my idea was to use Mac OS X Automator to follow this workflow:

Use the main wikipedia URL
Get the links from the page
Filter them down as the POTD always contained http://en.wikipedia.org/wiki/Template:POTD/ then todays date
Get the contents of the page
Save images

So I created this workflow, which worked okay.


That dumped the files to the folder abc123 on my desktop but it wasn't efficient. It would grab all the other images like the wikipedia logo as well as the picture of the day as small thumbnails.
I tried to use automators various other finder functions to move and rename, but I didn't have much luck.

I thought that I could do better by getting only the exact image I wanted at the size I wanted, so see what else I tried in Part 2.


Sunday, August 18, 2013

Conditional Statement in Bash on OS X

Ok,
I had a bit of trouble getting a conditional statement to evaluate on my bash script in OS X.
I was trying to perform an action if I was online or not.

A good way to test if you are online - ping google of course!

For testing purposes I just got the computer to use text to speech to let me know the result.
Here is my working example, spaces are important!

#!/bin/bash

FLAG=0

ping -q -c 1 google.com && FLAG=1
if [ $FLAG = "1" ] ; then
say $FLAG
else 
say $FLAG
fi

exit

If you can ping google,the computer will speak "one"

If you you want to test, change google.com to something you cant resolve such as googlekdfsjbg.com and run it again.

You shouldn't be able to ping googlekdfsjbg.com so the computer will speak "zero"

Of course this is a simple example and you need to tailor to your situation, just because you can't ping google doesn't mean you are not online. For example pings could be blocked on your network, however this suits my situation.

enjoy!

Thursday, August 1, 2013

Raspberry Pi Temperature Sensing.

Thanks to Adafruits Raspberry Pi tutorials on temperature sensing,
and this blog on Raspberry Pi temperature sensing to Sen.se.

I decided to do my own.
I also wanted to store the results locally, so I also saved it to a file.

I used a DS18B20 1-wire temperature sensor
Adafruit T-Cobbler
Breadboard and wires.

I wired it up just like they did in their diagrams.



Add one wire support.
In /boot/config.txt add
dtoverlay=w1-gpio
I subscribed to Sen.se and in your profile you can find your API key.

After checking out their tutorials I then wrote a bash script to grab the the temperature, then use curl and JSON to upload it.

Here is the script replace SERIALNUMBERHERE with the serial number of your sensor.
API KEY HERE with your API key, and SENSEFEEDID with your Sen.se feed id.

#!/bin/bash

sudo modprobe w1-gpio

sudo modprobe w1-therm

cd /sys/bus/w1/devices/SERIALNUMBERHERE/


URL="http://api.sen.se/events/?sense_key=API KEY HERE"


DATE=$(date)

NOW=$(date +"%d_%m_%Y")


DATA=$(cat w1_slave)


DATA1=${DATA:69:2}

DATA2=${DATA:71}

DATA3=$DATA1.$DATA2


OUTPUT="$DATE , $DATA1.$DATA2"


echo $OUTPUT >> /root/output_$NOW.csv


curl -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"feed_id": SENSEFEEDID,"value": '$DATA3'}' $URL



I then use Cron to schedule a reading every few minutes.

Then back in Sen.se you can graph it or manipulate it like you want.

Monday, July 29, 2013

First Post.

Well its about time.  Here it is. A blog. Yeah. You know what a blog is.