Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: APoD Background Setter

  1. #1
    Join Date
    Jan 2009
    Location
    Townsville, Australia
    Beans
    7

    APoD Background Setter

    Hi there,

    After spending a couple of weeks tinkering (and simultaneously learning bash) with the script found here (the second script, and many thanks to the authors), I can now enjoy the "Astronomy Picture of the Day" (APoD) as my wallpaper fetched daily with zero effort. I thought it would be nice to share with other interested people out there.

    The APoD website is here if you would like to see what it's all about.

    If you would like this script setup on your system, follow these steps:


    1) Download the script I've helped modify. Save it anywhere you like and call it whatever you want, just remember where it is. I've called mine apodwallpaper and by default it saves the APoD pictures to ~/Pictures/APoD/, although this is easily changed.

    Code:
    #!/bin/bash
    #Filename: apodwallpaper
    #Purposes: Downloads NASA Astronomy Picture of the Day and displays it as the GNOME background 
    #Author(s): acvwJosh of Ubuntu Forums, modified by Colin Dean <http://cad.cx>, o1911
    
    # change this if you want the image to be in a different directory
    DIRECTORY=$HOME/Pictures/APoD
    cd $DIRECTORY
    
    # fetch the apod site's text, to determine the latest apod
    SITE=http://apod.nasa.gov/apod/index.html
    
    if $(wget $SITE); then
    	# the prefix of the filename is to be the current date
    	FILENAME=$(cat $(echo $SITE | rev | cut -d "/" -f -1 | rev) | grep $(date +%Y) | head -1)
    
    	# format this date to have underscores instead of spaces
    	FILENAME=$(echo $FILENAME | tr " " "_")
    	
    	# check if today's file exists
    	if [ ! -f $FILENAME* ]; then
    		# if not, download image from apod site
    		wget -A.jpg,.JPG -R.txt -r -l1 --no-parent -nH $SITE
    
    		# find largest file (biggest res. picture)
    		BIGFILESIZE=$(find apod/ -printf '%s\n' | sort -nr | head -1)
    		
    		# check if biggest file is a dir (no picture today)
    		if [ $BIGFILESIZE == 4096 ]; then
    		## there is no picture for today
    			# set today's picture to be yesterday's
    			YESTERDAY=$(( $(date +%s) - 86400 )) # 86400 seconds in a day
    			ln -sf "$(find $DIRECTORY/ | grep -i $(date -d "@$YESTERDAY" +%Y_%B_%d))" "$HOME/wallpaper"
    			
    			## set today's picture to be a random one
    			#NUMBEROFFILES=$(find . | wc -l)
    			#NUMBER=$(( $RANDOM % $NUMBEROFFILES ))
    			#ln -sf "$DIRECTORY/$(ls | tail -$NUMBEROFFILES | head -$NUMBER | tail -1)" $HOME/wallpaper
    			
    			rm -rf ap* *.apod robots.txt* index.html*
    			exit 0
    		fi
    		
    		## we have a picture for today
    		# get its relative path for movement to ${DIRECTORY}
    		BIGGESTFILE=$(find apod/ -size ${BIGFILESIZE}c)
    		# also get its base filename to append
    		BIGGESTFILENAME=$(find apod/ -size ${BIGFILESIZE}c -printf '%f\n')
    		# declare our final filename for today's picture
    		WALLPAPER=$FILENAME-$BIGGESTFILENAME
    		# move it to the proposed directory
    		mv $BIGGESTFILE $DIRECTORY/$WALLPAPER
    	else
    		WALLPAPER=$(find $DIRECTORY/ -name $FILENAME* -printf "%f\n")
    	fi
    else # wget failed. internet up?
    	exit 1
    fi
    
    # as cron cannot set the wallpaper on its own, use a symbolic link to be a pointer to the background
    rm $HOME/wallpaper
    ln -sf "$DIRECTORY/$WALLPAPER" $HOME/wallpaper
    
    # get rid of cruft
    rm -rf ap* *.apod robots.txt* index.html*
    exit 0
    2) Setup your crontab to make this script run automatically. You can edit your crontab with the command "crontab -e". If you are unfamiliar with text-editors, nano will be the easiest to use. More about crontab here. To give a clue as how your crontab should look, this is what calls my apod script:

    Code:
    # m h  dom mon dow   command
    */30 * * * * /home/username/bin/apodwallpaper
    3) Get your GNOME session to use the wallpaper as your background- do the usual right-click desktop, change desktop background, add, and point to the 'wallpaper' file in your home ($HOME) directory. Everytime cron runs apodwallpaper and downloads a new picture, your desktop will now automatically update to it.

    The reason I've my script called at many times in the day is just so I might still get the picture if my computer is off for a period there. I've made the script so that it won't redownload the picture every time so calling it many times is not a problem.

    Hopefully that's enough to get most people setup. If you can think of how to make this script even better, please say so or even do so and post back here. Cheers!

    edit: script wasn't working... fixed now.
    edit again: made the script much better; now there is no dependence on your local timezone.
    Last edited by o1911; January 30th, 2011 at 02:48 AM. Reason: updated the script and setup steps for its current state

  2. #2
    Join Date
    Sep 2005
    Location
    Haarlem, Netherlands
    Beans
    23
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: APoD Background Setter

    Why not get the picture-of-the-day of National Geographic? I was sooo tired of APOD, so I altered some script.

    Code:
    #!/bin/sh
    #Based on code of APOD, you can really find everywhere on the web.
    
    #Downloading html of the Picture-Of-The-Day
    wget -N http://photography.nationalgeographic.com/photography/photo-of-the-day/ -O /tmp/ngpod.html
    
    #Getting the URL of the image
    img_location=`egrep -o "http://[^<]*1600x1200[^>]*\.jpg" /tmp/ngpod.html`
    
    rm /tmp/ngpod.html
    
    #Download image
    TODAY=$(date +'%Y%m%d')
    wget $img_location -O ~/Afbeeldingen/ngpod/$TODAY.jpg
    
    # Setting background-image.
    # NB: Use an absolute URL
    gconftool-2 -t string -s /desktop/gnome/background/picture_filename "/home/vincent/Afbeeldingen/ngpod/$TODAY.jpg"
    gconftool-2 -t string -s /desktop/gnome/background/picture_options "zoom"
    # Possible values are "none", "wallpaper" (eg tiled), "centered", "scaled", "stretched", "zoom"
    Just run this script whenever you don't like the previous anymore. Check out http://photography.nationalgeographi...to-of-the-day/ for more info and to see what you've missed.

  3. #3
    Join Date
    Apr 2010
    Beans
    6

    Re: APoD Background Setter

    Quote Originally Posted by o1911 View Post
    Hi there,

    After spending a couple of weeks tinkering
    ...
    edit: script wasn't working... fixed now.
    edit again: made the script much better; now there is no dependence on your local timezone.
    fantastic script and thank you for sharing it.
    however, i would really love to have the text description for the picture too in the same way as this from here. if you're able to amend the script to do that as well or give me some pointers to how to do it myself (only very basic skills at moment...) i'd be incredibly grateful. ta.

  4. #4
    Join Date
    Mar 2010
    Location
    Paradise, NJ
    Beans
    9
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Talking Re: APoD Background Setter

    Quote Originally Posted by sierraindigo View Post
    fantastic script and thank you for sharing it.
    however, i would really love to have the text description for the picture too in the same way as this from here. if you're able to amend the script to do that as well or give me some pointers to how to do it myself (only very basic skills at moment...) i'd be incredibly grateful. ta.
    A very rough outline of a proper APoD plugin.

    This is probably the first piece of software I've shared with anyone. Bear with me.

    I'm trying to get Launchpad set up so I can share this code, but, like I said, I'm quite new to this. Anyone interested in this project who might want to help me would be greatly appreciated.

    1. Today's Astronomy Picture of the Day.
    2. One large toggle button. When clicked, hides, explanation and button label changes to "Show". I intend to make it small, square and translucent.
    3. Title of photo. Currently reads 'None" because my method of extracting this info from the APoD page isn't very exact. It worked yesterday. Will probably work later today.
    4. Explanation. Notice the poor formatting. I'm such a n00b I can't overcome this.
    5. Today's date.

    As of now, it simply downloads the APoD daily page, and extracts the relevant information from that.
    I'm working on storing this information in Desktop Couch, formatting the paragraph and improving the visibility of the text with a background color, and a bunch of other things. I'm pretty new to this, so everyone should take a look at the code and add you're suggestions/improvements/whathaveyou.

    Code:
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    # This file is part of the APoD Desktop Art and Description plug-in
    #
    # Kopimi c2010 James Martin < nvsbl.monstr at gmail dot com >
    #
    # 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
    #
    #
    # APoD DESKTOP DESCRIPTION WIDGET
    # A program to print the Astronomy Picture of the Day
    # description in a widget, along with the picture as
    # the desktop background. This widget will be collapsable,
    # functioning exactly as the Windows 7 APoD background changer does.
    #
    # Will also function as commandline apod-description-getter, 
    # which will then be used for my .zshrc file instead of that stupid
    # WoW calendar someone packed in with it.
    #
    
    # get page
    # find paragraph
    # put paragraph, link and picture in DesktopCouch
    # print to desktop with cairo
    # 
    
    import urllib
    import re
    import datetime
    import pygtk
    pygtk.require('2.0')
    import gtk
    import cairo
    import glob
    
    from desktopcouch.records.server import CouchDatabase
    from desktopcouch.records.record import Record
    
    global MONTHS
    MONTHS = ("January",
              "February",
              "March",
              "April",
              "May",
              "June",
              "July",
              "August",
              "September",
              "October",
              "November",
              "December")
    
    global title
    global descrip
    global date
    
    global IMGDR
    IMGDIR = "/home/nvsbl/Pictures/wallpaper/APoD/"
    APOD = "http://antwrp.gsfc.nasa.gov/apod/astropix.html"
    
    # Transparent window with cairo
    def transparent_expose(widget, event):
        """Make the given widget transparent."""
        cr = widget.window.cairo_create()
        cr.set_operator(cairo.OPERATOR_CLEAR)
        region = gtk.gdk.region_rectangle(event.area)
        cr.region(region)
        cr.fill()
        return False
    
    class DesktopWindow(gtk.Window):
        """A transparent and borderless window, fixed to desktop."""
        def __init__(self, *args):
            gtk.Window.__init__(self, *args)
            
            self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_DOCK)
            self.set_keep_below(True)
            self.set_decorated(False)
            self.stick()
            
            screen = self.get_screen()
            rgba = screen.get_rgba_colormap()
            self.set_colormap(rgba)
            self.set_app_paintable(True)
            self.connect("expose-event", transparent_expose)
    
    global toggle
    toggle = True
            
    class DescriptionWidget:
        global title
        global descrip
        global date
        
        """"Widget with actual description in it."""
        def callback(self, widget, data=None):
            global toggle
            if toggle:
                self.label.hide()
                self.button.set_label("Show")
                toggle = False
                print "Hiding information."
            else:
                self.label.show()
                self.button.set_label("Hide")
                toggle = True
                print "Showing information."
            
        def __init__(self):
            self.window = DesktopWindow()
            self.window.move(20,50)
            self.window.set_default_size(500,500)
            
            self.window.connect("destroy", lambda wid: gtk.main_quit())
            self.window.connect("delete_event", lambda a1,a2:gtk.main_quit())
            
            self.box = gtk.VBox()
            self.window.add(self.box)
            
            self.label = gtk.Label()
    
            self.button = gtk.Button(label='Hide')
            self.button.set_focus_on_click(False)
            focus = self.button.get_focus_on_click()
            if focus:
                print "Focus on click:\tTrue"
            else:
                print "Focus on click:\tFalse"
            self.button.connect("clicked", self.callback, "hide info")
                    
            self.box.pack_start(self.button, expand=False, fill=False)
            print "Packing button"
            self.box.pack_start(self.label, expand=True, fill=True)
            print "Packing label"
            
            self.label.set_markup("""<span foreground='#ffffff'><b>%s</b>\n<b>Explanation</b>: %s\n<i>%s</i></span>""" % (title, descrip,date,));
            
            self.window.show_all()
            print "Showing everything."
            
            
    def get_page(url=APOD):
        """Opens page"""
        print "Getting page %s" % url
        return urllib.urlopen(url).read()
    
    def month2num(month):
        """Takes a given month and converts it to the appropriate number."""
        x = 0
        while x < len(MONTHS):
            if month == MONTHS[x]:
                return x + 1
            x = x + 1
        return False
    
    def num2month(num):
        """Takes a given number and converts it to a month."""
        if str(type(num)) == "<type 'int'>":
            return MONTHS[num-1]
        
        
    def is_today(date):
        """Takes a date and determines whether that date is today's.
        Date accepted in form YYYY Month DD"""
        today = datetime.date.today()
        if date == today:
            return True
        else:
            return False
    
    def d2str(date):
        year = str(date.year)
        month = num2month(date.month)
        day = str(date.day)
        
        return year + " " + month + " " + day
    
    def get_title(page=None):
        print page.split("""alt="See Explanation.
    Moving the cursor over the image will bring up an annotated version.
    Clicking on the image will bring up the highest resolution version
    available."></a> 
    </center> """)
    
    def get_date(page):
        """Finds date on page
        f=format. d = datetime object, str = string"""
        
        date_pattern = "\d+\s[a-zA-Z]+\s\d+"
        p = re.compile(date_pattern)
        q = p.search(page)
        start = q.start()
        end = q.end()
        date = page[start:end]
        sdate = date.split(" ")
        
        year = int(sdate[0])
        month = int(month2num(sdate[1]))
        day = int(sdate[2])
        
        return datetime.date(year,month,day)
            
        
    def get_descrip(page):
        strip_before_junk = page.split("<b> Explanation: </b>")[1].strip()    
        strip_after_junk = strip_before_junk.split("<p> <center>")[0].strip()
        return strip_after_junk
    
    def todays_img_exists():
        todaystr = get_todaystr()
        filelist = glob.glob(IMGDIR + '/*.jpg')
        date = todaystr.split(' ')
        month = num2month(date[0])
        year = str(date[1])
        day = str(date[2])
        newdatestr = year + "_" + month + "_" + day
        for f in filelist:
            if f.find(newdatestr):
                pass
        
    # Desktop couch stuff
    def get_todaystr():
        today = datetime.date.today()
        month = str(today.month)
        if len(month) == 1:
            month = '0' + month
        year = str(today.year)
        day = str(today.day)
        if len(day) == 1:
            day = '0' + day
        return month + " " + year + " " + day
        
    def update_couchdb():
        
                
        
        db = CouchDatabase("apodbackgroundchanger", create=True)
        record_type = "somerecord.html"
        #new_record = Record("date" : todaystr,
    #                        "description" : descrip,
    #                        "image" : }, record_type)
        #new_record.attach(
    
    def record_exists(db, record_type):
        todaystr = get_todaystr()
        results = db.get_records(record_type = record_type, create_view=True)
        for records in results:
            record = records.value
            if record["date"] == todaystr:
                return True
            else:
                return False
    
    def main():
        global descrip
        global date
        global title
    #    update_couchdb()
        page = get_page(APOD)
        date = datetime.date.today()
        print "Getting date: %s\n" % date
        if is_today(date):
            date = get_date(page)
            title = get_title(page)
            print "Getting title: %s\n" % title
            descrip = get_descrip(page)
            print "Getting descrip: %s\n" % descrip
    
            instance = DescriptionWidget()
            gtk.main()
        
    if __name__ == "__main__":
        main()
    edit: My code is very sloppy. I'm sorry about that.
    Attached Images Attached Images
    Last edited by nvsbl; June 15th, 2010 at 06:11 AM. Reason: Fixed image

  5. #5
    Join Date
    Mar 2010
    Location
    Paradise, NJ
    Beans
    9
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Exclamation Re: APoD Background Setter

    UPDATE June 15 2010:

    Thus far, this is what I've accomplished:

    When this script first gets the relevant information, it writes it to a Desktop Couch DB. Subsequent calls to the script during that day will get the info from the couch, instead of online.

    Button has an icon, but still isn't transparent. When the button is clicked, it hides the description, but in it's absence the button becomes the height of the HBox. Must fix this.

    Also need to give the info box a slightly-transparent background. Today (June 15) the image is very light, so white text doesn't display very well. Must fix this as well.

    I'm also working on a rating system, so you can keep track of your favorite APoD pictures. Also, every picture is added to a slideshow, so you can browse previous pics as backgrounds.

    Any input at this stage would be wonderful. I know this is a clumsy hack right now, but I'm working on improving that. Ideas are always welcome. I'm @nvsbl on Identica, and @nvsblmnstr on Twitter. Plus there's forum messages and all that jive, and the good old fashioned reply to this very thread. The choice is yours.
    Attached Images Attached Images
    Last edited by nvsbl; June 15th, 2010 at 07:24 AM.

  6. #6
    Join Date
    Aug 2009
    Location
    UK
    Beans
    138
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: APoD Background Setter

    I followed the first post. It has been working fine for a while, but recently it has stopped working unless I log in and log back out. Any ideas?
    A0753 8GB RAM 60GB SSD x64 12.10 GNOME 3 E520 * 4GB RAM 6TB RAID x64 12.04 GNOME 3 * ASROCK 330 2GB RAM 4GB CF x64 XBMCbuntu

  7. #7
    Join Date
    Aug 2009
    Location
    UK
    Beans
    138
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: APoD Background Setter

    Maybe it was something to do with the names of the actual pictures? Yesterday and today seem to have updated without a login-logout.
    A0753 8GB RAM 60GB SSD x64 12.10 GNOME 3 E520 * 4GB RAM 6TB RAID x64 12.04 GNOME 3 * ASROCK 330 2GB RAM 4GB CF x64 XBMCbuntu

  8. #8
    Join Date
    Aug 2009
    Location
    UK
    Beans
    138
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: APoD Background Setter

    I'm still not sure what's going on with this. Sometimes it updates, sometimes it doesn't. Perhaps the OP could weigh in?
    A0753 8GB RAM 60GB SSD x64 12.10 GNOME 3 E520 * 4GB RAM 6TB RAID x64 12.04 GNOME 3 * ASROCK 330 2GB RAM 4GB CF x64 XBMCbuntu

  9. #9
    Join Date
    Jan 2009
    Location
    Townsville, Australia
    Beans
    7

    Re: APoD Background Setter

    Apologies for my absence; I'm glad that my script has been some help, however. I've since made many improvements to it, let me share that. There's no more need for the cron hack either- just point gnome's background to the symbolic link created by the script, and it will update in real-time.

    As for the additional information alongside the picture... that's an interesting idea. I'm not sure how to get that to work, off the top of my head. It appears nvsbl is onto something though.

    Code:
    #!/bin/bash
    #Filename: apodwallpaper
    #Purposes: Downloads NASA Astronomy Picture of the Day and displays it as the GNOME background 
    #Author(s): acvwJosh of Ubuntu Forums, modified by Colin Dean <http://cad.cx>, o1911
    
    # change this if you want the image to be in a different directory
    DIRECTORY=$HOME/Pictures/APoD
    cd $DIRECTORY
    
    # fetch the apod site's text, to determine the latest apod
    SITE=http://apod.nasa.gov/apod/index.html
    
    if $(wget $SITE); then
    	# the prefix of the filename is to be the current date
    	FILENAME=$(cat $(echo $SITE | rev | cut -d "/" -f -1 | rev) | grep $(date +%Y) | head -1)
    
    	# format this date to have underscores instead of spaces
    	FILENAME=$(echo $FILENAME | tr " " "_")
    	
    	# check if today's file exists
    	if [ ! -f $FILENAME* ]; then
    		# if not, download image from apod site
    		wget -A.jpg,.JPG -R.txt -r -l1 --no-parent -nH $SITE
    
    		# find largest file (biggest res. picture)
    		BIGFILESIZE=$(find apod/ -printf '%s\n' | sort -nr | head -1)
    		
    		# check if biggest file is a dir (no picture today)
    		if [ $BIGFILESIZE == 4096 ]; then
    		## there is no picture for today
    			# set today's picture to be yesterday's
    			YESTERDAY=$(( $(date +%s) - 86400 )) # 86400 seconds in a day
    			ln -sf "$(find $DIRECTORY/ | grep -i $(date -d "@$YESTERDAY" +%Y_%B_%d))" "$HOME/wallpaper"
    			
    			## set today's picture to be a random one
    			#NUMBEROFFILES=$(find . | wc -l)
    			#NUMBER=$(( $RANDOM % $NUMBEROFFILES ))
    			#ln -sf "$DIRECTORY/$(ls | tail -$NUMBEROFFILES | head -$NUMBER | tail -1)" $HOME/wallpaper
    			
    			rm -rf ap* *.apod robots.txt* index.html*
    			exit 0
    		fi
    		
    		## we have a picture for today
    		# get its relative path for movement to ${DIRECTORY}
    		BIGGESTFILE=$(find apod/ -size ${BIGFILESIZE}c)
    		# also get its base filename to append
    		BIGGESTFILENAME=$(find apod/ -size ${BIGFILESIZE}c -printf '%f\n')
    		# declare our final filename for today's picture
    		WALLPAPER=$FILENAME-$BIGGESTFILENAME
    		# move it to the proposed directory
    		mv $BIGGESTFILE $DIRECTORY/$WALLPAPER
    	else
    		WALLPAPER=$(find $DIRECTORY/ -name $FILENAME* -printf "%f\n")
    	fi
    else # wget failed. internet up?
    	exit 1
    fi
    
    # as cron cannot set the wallpaper on its own, use a symbolic link to be a pointer to the background
    rm $HOME/wallpaper
    ln -sf "$DIRECTORY/$WALLPAPER" $HOME/wallpaper
    
    # get rid of cruft
    rm -rf ap* *.apod robots.txt* index.html*
    exit 0
    Last edited by o1911; January 30th, 2011 at 02:40 AM. Reason: updated script

  10. #10
    Join Date
    Apr 2010
    Beans
    6

    Re: APoD Background Setter

    Quote Originally Posted by o1911 View Post
    There's no more need for the cron hack either- just point gnome's background to the symbolic link created by the script, and it will update in real-time.
    just looked this up again as my background had failed to update for a couple of days. when you say that the cron hack isn't needed anymore i take it you mean the .make_Xdbus steps? in which case i just call the script direct from cron now?

    thanks for your work.

Page 1 of 3 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •