day_glow.sh
#!/bin/bash
#
# to make this work:
# 1 - $ sudo apt-get install imagemagick
# 2 - Extract this ZIP to your hard drive
# 3 - $ chmod +x day_glo.sh
# 4 - $ ./day_glo.sh picture max min woeid
# - picture = path to the background picture that you use \
# this script will create a <picture>.glow file \
# which should be set as your background
# - max = an integer value for the maximum brightness you \
# want during the brightest part of the day
# - min = same as max, but during the darkest part of the day \
# I find that a max of 100 & min of 25 do nicely
# - woeid = this is your location identifier as Yahoo understands it \
# you can fire up the included 'woeid.html' file to find \
# the number for your location
# 5 - $ crontab -e
# - every hour = "0 * * * * /path/to/day_glow.sh /path/to/background.jpg 100 25 12799337"
# - this will run the script on the hour, every hour
# - "/path/to/day_glo.sh" = file path to this script
# - "/path/to/background.jpg" = the image to be used as the backgorund \
# the script will output to "/path/to/background.jpg.glow" \
# which should be set as your background
# - "100 25" = this sets the max brightness to 100 (midday) & min to 25 (midnight)
# - "12799337" = WOEID for Vancouver, Washington - check woeid.html for yours
NOW=$(date +%s)
PIC=$1
MAX=$2
MIN=$3
WOEID=$4
wget -q http://weather.yahooapis.com/forecastrss?w=$WOEID -O- | \
grep astronomy | \
sed -E 's/.*sunrise="([^ ]*) am" sunset="([^ ]*) pm"\/>/\1 \2/' | \
while read -r SUNUP SUNDN; do
SUNUP_DTS=$(date "+%Y-%m-%d $SUNUP:00")
SUNDN_DTS=$(date "+%Y-%m-%d $(( ${SUNDN%${SUNDN: -3}} + 12 )):00")
SUNUP_EPC=$(date --date="$SUNUP_DTS" +%s)
SUNDN_EPC=$(date --date="$SUNDN_DTS" +%s)
HALFDAY=$(( ($SUNDN_EPC-$SUNUP_EPC)/2 ))
MIDDAY=$(( $HALFDAY+$SUNUP_EPC ))
if [[ $NOW > $MIDDAY ]]; then
DIFF=$(( $NOW-$MIDDAY ))
elif [[ $MIDDAY > $NOW ]]; then
DIFF=$(( $MIDDAY-$NOW ))
fi
# UPDATE - ADDED THIS BLOCK TO PREVENT FULL BLACKNESS
if [[ $DIFF -gt $HALFDAY ]]; then
DIFF=$HALFDAY
fi
PERC=$(( 100-(100*$DIFF/$HALFDAY) ))
SCALE=$(( $MAX-$MIN ))
VAL=$(( ($PERC*$SCALE/100)+$MIN ))
convert -modulate $VAL,100,100 $PIC $PIC.glow
done
woeid.html
<html>
<head>
<title>Ben's Really Simple WOEID Converter</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.4.min.js'></script>
<script type='text/javascript'>
function lookup_woeid() {
var place = encodeURI($('#place').val());
$.getJSON(
'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text%3D%22'+place+'%22&format=json',
function(data) {
$('#output').html('');
if (data.query.count == 0) {
return false;
} else if (data.query.count == 1) {
var place = data.query.results.place;
$('<tr><td>'+place.woeid+'</td><td>'+place.name+'</td><td>'+place.admin1.content+'</td></tr>')
.appendTo('#output');
} else {
var places = data.query.results.place;
for (var p in places) {
place = places[p];
$('<tr><td>'+place.woeid+'</td><td>'+place.name+'</td><td>'+place.admin1.content+'</td></tr>')
.appendTo('#output');
}
}
}
);
}
</script>
</head>
<body>
<table style='height:100%;width:100%;'>
<tr style='height:50%;'>
<td valign='middle'>
<center>
Place Name or ZIP:
<input type='text' id='place' />
<br>
<button onClick='lookup_woeid()'>find it</button>
</center>
</td>
</tr>
<tr>
<td>
<center>
<table border='1px'>
<thead>
<tr>
<th>WOEID</th><th>Name</th><th>Place</th>
</tr>
</thead>
<tbody id='output'>
</tbody>
</table>
</center>
</td>
</tr>
</table>
</body>
</html>


0 comments:
Post a Comment