Friday, August 04, 2006

APOD Viewer v1

I love APOD - Astronomy Picture of the Day - as I'm privately quite interested in the universe and all that stuff. But I do not visit APOD daily, so once in a while I browse the site.

But, wouldn't it be nice to have a slideshow with the pictures?

Surprise! PowerShell to the rescue.

This script really show the power of PowerShell. It has a web client, caculates dates and controls Internet Explorer. Give it a shot. If you make any clever changes, please post those as comments. I have a big wishlist myself, but this got me going.



function apod {
param([int] $days=1) # show latest picture by default
$baseurl="http://antwrp.gsfc.nasa.gov/apod/"

# create IE COM object
$ie = new-object -com internetexplorer.application

# create .net object
$webclient = new-object system.net.webclient

# date manipulation
$date=[datetime]::now
$date.AddDays(1) # start tomorrow as the loops starts by subtracting a day

$ie.fullscreen=$true
$ie.visible=$true
# You may have to press the window in the taskbar to make it appear


# An easy way to do a for loop - could have used for, but as said this is easier
1..$days | % {

# back one day
$date=$date.AddDays(-1)

# construct url
$url=$baseurl+"ap" + $date.tostring("yyMMdd") + ".html"
$url

# get HTML page
$html = $webclient.downloadstring($url)
$html.length

# Pick up the link the picture has - links to the high resolution version
# I want to display
# This is not idiot proff coding - but, hey, scripts are easy to change

$html -match '<a href="([^"]*)">\W*<img'
$matches.count
# get the value matching the pattern in parenthesis
$img=$matches[1]
$img

# show high resolution picture
$ie.navigate($baseurl+$img)

# wait for IE to load picture
while ($ie.busy) {sleep -s 1;"busy"}

# give me 10 seconds to enjoy the picture
sleep -s 10

}

# shutdown IE
$ie.quit()

$ie=$null

# wish list: load pictures overlapping (but keep presentation interval)
# wish list: include some kind of fancy transition
# wish list: pick out the explanation and present it in some way
# add our own...

}

No comments: