brandonedens.org

Astronomy Picture of the Day Wallpaper

back

Screenshots

Description

APOD or the astronomy picture of the day generator is a program I wrote in 2006-03-13 to generate desktop wallpapers based off of the astronomy picture of the day website.

The current version of the program is written in Ruby and has dependencies on wget and Rmagick.

Source Code

        
#!/usr/bin/ruby -w
#--
# apod - script to pull the astronomy picture of the day and generate an image
# suitable as a desktop wallpaper.
# See: http://antwrp.gsfc.nasa.gov/apod
#
# Copyright (c) 2006 Matthew Brandon Edens - brandon@cs.uri.edu
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#++

# This program requires both RMagick and wget to function.
require 'ftools'
require 'fileutils'
require 'date'
require 'tmpdir'
require 'RMagick'

# globals - tweak the program here
SCREEN_WIDTH=1280
SCREEN_HEIGHT=768

TMPDIR="/tmp/apod"

HOME="/home/brandon"
WALLPAPER="#{HOME}/wallpaper.jpg"
HIGHRES="#{HOME}/media/pictures/astronomy_picture_of_the_day/highres"
DESKTOP="#{HOME}/media/pictures/astronomy_picture_of_the_day/wallpaper"

def wrap(str, max_size)
    all = []
    line = ''
    for l in str.split
        if (line+l).length >= max_size
            all.push(line)
            line = ''
        end
        line += line == '' ? l : ' ' + l
    end
    all.push(line).join("\n")
end

def month_to_number(str)
  case str
  when "January"
    return 1
  when "February"
    return 2
  when "March"
    return 3
  when "April"
    return 4
  when "May"
    return 5
  when "June"
    return 6
  when "July"
    return 7
  when "August"
    return 8
  when "September"
    return 9
  when "October"
    return 10
  when "November"
    return 11
  when "December"
    return 12
  end
end


if $0 == __FILE__
  url = ARGV[0]

  # some initial directory creation
  FileUtils.mkdir(TMPDIR) unless File.directory?(TMPDIR)

  # pull down the needed astronomy picture of the day images and html
  wget = Kernel.system("wget -q -nd -nc -O#{TMPDIR}/index.html #{ARGV[0] ? ARGV[0] : 'http://antwrp.gsfc.nasa.gov/apod/index.html'}")
  wget = Kernel.system("wget -q -nd -nc -r -l1 -P#{TMPDIR} --accept jpg,png,gif,jpeg,index.html #{ARGV[0] ? ARGV[0] : 'http://antwrp.gsfc.nasa.gov/apod/index.html'}")
  # open the astronomy index.html to get names of images and explanation
  index = File.open("#{TMPDIR}/index.html")
  st = index.read =~ /<body.*?>/im
  index.rewind
  en = index.read =~ /<\/body>/im
  index.rewind
  text = index.read[st...en]

  # get the date from the index html page pulled down from the site
  re = /([0-9][0-9][0-9][0-9]) (January|February|March|April|May|June|July|August|September|October|November|December) ([0-9]+)/m
  md = re.match(text)
  year = md[1]
  month = month_to_number(md[2])
  day = md[3]
  date = "#{md[1]} #{md[2]} #{md[3]}"
  date_directory = "#{year}-#{month}-#{day}"

  # get the high resolution image filename
  re = /#{date}.*?<a href="(.*?)">/im
  md = re.match(text)
  tmp_str = md[1]
  re = /.*\/(.*?)$/m
  md = re.match(tmp_str)
  highres = md[1]

  # get the low resolution image filename
  re = /#{date}.*?<img src="(.*?)"/im
  md = re.match(text)
  tmp_str = md[1]
  re = /.*\/(.*?)$/m
  md = re.match(tmp_str)
  lowres = md[1]

  # get the explanation
  re = /(Explanation:.*?)Tomorrow's picture:/m
  md = re.match(text)
  explanation = md[1]
  explanation = wrap(explanation.gsub(/<.*?>/m, ""), 65)

  # generate the desktop wallpaper
  image = Magick::ImageList.new("#{TMPDIR}/#{lowres}")
  background = Magick::Image.new(SCREEN_WIDTH, SCREEN_HEIGHT) {
    self.background_color = 'black'
  }
  text = Magick::Draw.new
  wallpaper = background.composite(image,
                                   Magick::EastGravity,
                                   Magick::OverCompositeOp)
  text.annotate(wallpaper, (SCREEN_WIDTH - image.columns),
                SCREEN_HEIGHT, 10, 10, explanation) {
    self.gravity = Magick::WestGravity
    self.pointsize = 14
    self.stroke = 'transparent'
    self.fill = '#eeeeee'
    self.font_weight = Magick::BoldWeight
  }
  wallpaper.write("#{TMPDIR}/wallpaper.jpg")

  # make the directories for permanent storage
  FileUtils.mkdir("#{DESKTOP}/#{date_directory}") unless
    File.directory?("#{DESKTOP}/#{date_directory}")
  FileUtils.mkdir("#{HIGHRES}/#{date_directory}") unless
    File.directory?("#{HIGHRES}/#{date_directory}")

  # copy wallpaper to today's wallpaper location
  FileUtils.copy("#{TMPDIR}/wallpaper.jpg", "#{WALLPAPER}")

  # copy wallpaper to permanent location
  FileUtils.copy("#{TMPDIR}/wallpaper.jpg",
                 "#{DESKTOP}/#{date_directory}/wallpaper.jpg")

  # copy highres to its permanent location
  FileUtils.copy("#{TMPDIR}/#{highres}",
                 "#{HIGHRES}/#{date_directory}/#{highres}")

  # clean up afterwards
  #FileUtils.rm("#{TMPDIR}/index.html")
  #FileUtils.rm("#{TMPDIR}/#{highres}")
  #FileUtils.rm("#{TMPDIR}/#{lowres}")
  #FileUtils.rm("#{TMPDIR}/wallpaper.jpg")
  #FileUtils.rmdir("#{TMPDIR}")

  # set the X background
  Kernel.system("xsetbg -quiet -onroot -fullscreen #{WALLPAPER}")

end