PDA

View Full Version : Get daylight info (in Conky)



ycar
October 1st, 2009, 11:53 AM
The following is an example how to extract daylight information from www.wolframalpha.com site and display it in Conky.

Please check the attached screenshot (at the end of the post) which shows the result (daylight info is present at the bottom side of image).

Background
Daylight information (sunrise/sunset time, duration of daylight) can be accessed using WolframAlpha Computation Knowledge Engine.
A search for "sun" in http://www.wolframalpha.com returns a HTML page which contains information like (for my case):


Daylight information for October 1 in Lemesos, Cyprus:
sunrise | 6:43 am EEST
sunset | 6:33 pm EEST
duration of daylight | 11 hours 50 minutes

The source of the page contains the following:

<h1><span>Daylight information for October 1 in Lemesos, Cyprus:</span></h1>
...
<img src="..." id="i_0200_1"
alt="sunrise | 6:43 am EEST\nsunset | 6:33 pm EEST\nduration of daylight | 11 hours 50 minutes"
...


Extract daylight information
The first step is to get Daylight Information from WolframAlpha site. This information can be easily obtained by sending a request for "sun" keyword.
The request looks like this: "http://www.wolframalpha.com/input/?i=sun"

Using curl command this could be implemented like:

curl --silent --output ${ANSWER_FILE} "http://www.wolframalpha.com/input/?i=sun"
Where ANSWER_FILE is the temporary file where the HTML page (the answer from server) will be stored.

The second step is to parse the HTML page (answer) in order to extract the desired information. In our case we are looking for Location, Sunrise/Sunset Time, Daylight duration.
The simplest way is to do not try to interpret the entire page, but just look for some particular patterns.

Location
Check HTML page for a pattern like: "Daylight information for <DATE_AND_LOCATION>:"
Use sed and the following expression in order to extract location info:

sed -n 's/.*Daylight information for \(.*\):<.*/\1/p' ${ANSWER_FILE} | head -n 1

Sunrise
Check HTML page for a pattern like: "sunrise | xx:xx am" (on my hemisphere the sun always rises at AM :) ):
Use sed and the following expression in order to extract sunrise info:

sed -n 's/.*sunrise | \([0-9]\{1,2\}:[0-9]\{1,2\}\) am.*/\1/p' ${ANSWER_FILE} | head -n 1
Sunset
Check HTML page for a pattern like: "sunset | xx:xx pm" (on my hemisphere the sun always sets at PM :) ):
Use sed and the following expression in order to extract sunset info:

sed -n 's/.*sunset | \([0-9]\{1,2\}:[0-9]\{1,2\}\) pm.*/\1/p' ${ANSWER_FILE} | head -n 1
Duration of daylight
Check HTML page for a pattern like: "duration of daylight | xx hours yy minute":
Use sed and the following expression in order to extract daylight info:

sed -n 's/.*duration of daylight | \([0-9]\{1,2\}\) hours \([0-9]\{1,2\}\) minute.*/\1:\2/p' ${ANSWER_FILE} | head -n 1
Automatize using a Script
All the above steps could be automatized using a script file. The script file will extract all the information and store it in a temporary file (something like: /tmp/conky-daylight-inf.result).
The temporary file could be used later in many ways, like displaying it in Conky.
The script must be executed in two cases:

On user log-in (like specifying the script name in "Startup Applications" for Gnome)
On day change (here the best way is to use cron and add something like "0 0 * * * <script_name>" in crontab file)

The complete bash script is attached (at the end of the post).

Display daylight information in Conky
The daylight information (obtained using the above script and stored in a temporary file) could be easely displayed in conky by modifying the .conkyrc script in the following way:

${color orange}Daylight Info${color}${hr}
${execi 1800 cat /tmp/conky-daylight-inf.result}

Where /tmp/conky-daylight-inf.result is the temporary file generated by the script.
The refresh time (of 1800 sec) doesn't really have a meaning here. And it could be increased to a bigger value. Because anyway the decision about refreshing this info should be decided by the bash script and not conky. For this reason (if you use Conky to display daylight information produced by the bash script) the best thing is to modify the script in order to send a refresh signal to conky, add the following line at the end of the script:

# Refresh conky
killall -SIGUSR1 conky


The complete conky resource file is attached (at the end of the post).

Postscript um
Please consider that this article is just a personal research and it's not intended to provide a final/100% working solution.
The main intention was to show some nice possibilities for people having knowledge in scripting/regex/HTML and a desire to make computer working on their needs :).
Therefore:

The script is highly dependent per WolframAlpha site changes. In case WA team decides to change the page design - the script must be overwritten.
The script is not tested in many other cases (like another hemisphere/location/time format... etc.. etc...)
The author is not going to give support for this script in the future and is not responsible for maintaining it :).


Thanks ;)

kevdog
October 2nd, 2009, 12:12 AM
Nice how-to, I can see however this is really dependent on the http site format -- watch them change that.