Friday, June 17, 2011

timbit! Getting description from NASA's Astronomy Picture Of the Day

After writing this bit to Retrieve NASA's Picture Of the Day for my wallpaper I immediately wanted the description that goes along with the image so that I know what I'm looking at.

I considered adding the description to the picture itself so that I could read it whenever I'm looking at the wallpaper.  But that's ugly, detracting from the image.  And the description is usually several lines long (more than 10) so it might not fit well on the screen.

So then I considered writing a plugin for my panel (taskbar) that would pop-up a little widget with the text.  Seemed simple enough but I'm using lxde which only supports native plugins in C - there are no bindings for other languages.  Still undaunted I looked around for documentation - here's what their wiki says about it:

http://wiki.lxde.org/en/How_to_write_plugins_for_LXPanel

That's right - there is no documentation other than some random individuals that took it upon themselves to post some solutions.  The ones I found weren't perfect and I quickly realized this would take more than an hour.  NOT worth it.

So back to bash I go - I'll get the description at the same time as the image and put it in an html file next to the image.  Then I created a shortcut in the LXPanel to open chromium to that file whenever I wanted.

#!/bin/bash

BASE_URL=http://apod.nasa.gov/apod/
PAGE_URL=${BASE_URL}/astropix.html
APOD_IMG=~/images/wallpapers/apod.jpg
APOD_PAGE=~/images/wallpapers/apod_page.html
APOD_DESC=~/images/wallpapers/apod.html

# get the file once
wget -qO ${APOD_PAGE} ${PAGE_URL}

grep "IMG" ${APOD_PAGE} | \
    wget -qO ${APOD_IMG} \
    ${BASE_URL}`awk -F "\"" '{print $2}'`

START_INT=`grep -n "<b> Explanation: </b>" ${APOD_PAGE} | \
    awk -F : '{print $1}'`
END_INT=`grep -n "<p> <center>" ${APOD_PAGE} | \
    awk -F : '{print $1}'`
grep -A $(( ${END_INT} - ${START_INT} - 1 )) \
    "<b> Explanation: </b>" \
    ${APOD_PAGE} > ${APOD_DESC}
rm ${APOD_PAGE}


Definitely more wordy than the original but the varying length of the description required a little more work to determine the number of lines to scrape from the html file.

I'm interested in more concise examples. I tried sed but that gets multiple lines in a way similar to grep.

Then, after adding a shortcut to my panel for chromium to open the file I also added a keyboard shortcut to make getting the description easier than anything IN THE WORLD!

Scraping the description just to open it locally with a browser is a little ghetto but it's faster than loading a bookmark and writing the bash solution only took a few minutes.

No comments:

Post a Comment