Hack 61 Saving Daily Horoscopes to Your iPod

figs/moderate.gif figs/hack61.gif

You've got a zillion songs on your new iPod, and you're traveling around town oblivious to the sounds of the city. Worried about getting hit by a car, finding that special someone, or knowing when to ask for that raise? Take your horoscope along with you by running this hack daily .

With Apple's newest iPods, the functionality you can bring along with you has greatly improved. Not only can you sync up your iCal calendars or Address Book entries, you can also include little snippets of text in the new Notes feature. Limited to 4 KB per note and 1,000 notes, there's certainly room for improvement, but the ability to add your own navigational elements (either to other Notes or to songs and playlists) and paragraph styling (via HTML's <P> and <BR> tags) is a good start for some interesting applications.

This isn't to say that this hack is particularly interesting or useful, but it does give an example of programmatically determining the path to the currently mounted iPod via Perl. It's not foolproof, though; if you're rich enough to have more than one iPod mounted at the same time, then one will be chosen at random. Dealing with more than one iPod is an exercise you can pay someone else to do.

The Code

Save the following code to a file called horopod.pl :

 #!/usr/bin/perl -w # # HoroPod - save your daily horoscope to the iPod. # http://disobey.com/d/code/ or contact morbus@disobey.com. # # This code is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # use strict; $++; my $VERSION = "1.0"; use File::Spec::Functions; # make sure we have the modules we need, else die peacefully. eval("use LWP;"); die "[err] LWP is not installed.\n" if $@; # really cheap Perl-only way of finding the path to # the currently mounted iPod. searches the mounted # Volumes for an iPod_Control folder and uses that. my $ipod = glob("/Volumes/*/iPod_Control"); unless ($ipod) { die "[err] Could not find an iPod: $!\n"; } $ipod =~ s/iPod_Control//g;  # we want one directory higher. my $ipod_dir = catdir($ipod, "Notes", "Horoscopes"); mkdir $ipod_dir;  # no error checking by intention. # create a downloader, faking the User-Agent to get past filters. my $ua = LWP::UserAgent->new(agent => 'Mozilla/4.76 [en] (Win98; U)'); # now, load up our horoscopes. first, define all the # signs - these are used throughout the forloop. my @signs = qw( aries taurus gemini cancer leo virgo libra                 scorpio sagittarius capricorn aquarius pisces ); # loop through each sign. foreach my $sign (@signs) {     # make it purdier for humans.     my $display_sign = ucfirst($sign);     # the Yahoo! URL, specific to the current sign.     print "Grabbing horoscope for $display_sign...\n";     my $url = "http://astrology.yahoo.com/us/astrology/".                 "today/$sign"."dailyhoroscope.html";     # suck down the data or die.     my $data = $ua->get($url)->content       or die "[err] Could not download any data: $!\n";     # snag the date by signature, not design.     $data =~ /(\w{3} \w{3}\.? \d{1,2}, \d{4})/; my $date = ;     # and get the relevance. we could use an     # HTML parser, but this is mindlessly easier.     my $preface = '<font face="Arial" size="-1" color=black>';     my $anteface = '</font></TD></TR></table>'; # ante up!     $data =~ /$preface(.*)$anteface/i; my $proverb = ;     # save this proverb to our file.     my $ipod_file = catfile($ipod_dir, $display_sign);     open(IPOD_FILE, ">$ipod_file") or die "[err] Could not open file: $!\n";     print IPOD_FILE "$display_sign\n$date\n\n";     print IPOD_FILE "$proverb\n"; close(IPOD_FILE); } 

Running the Hack

To run the hack, make sure your iPod is mounted as a FireWire HD (i.e., you can see it on your Desktop when it's plugged into your Mac or docking bay), launch the Terminal application (Applications Utilities Terminal), and type perl horopod.pl on the command line. After a few lines of output, your newly scraped horoscopes should be on your iPod as a Horoscopes folder under the Notes feature. They'll be one fileor noteper sign.

Hacking the Hack

There are a few ways you can tweak the script, all mindlessly simple. For one, you might not want your horoscopes under a directory called Horoscopes ; to change that behavior, merely tweak my $ipod_dir = catdir($ipod, "Notes", "Horoscopes"); to your desired path.

Concerning the source data itself, Yahoo! Horoscopes has a number of different sorts of predictions available; you can get them tweaked to Music, Movies, Romance, and what have you. Be sure to check out http://astrology.yahoo.com/ to find your desired version. When tweaking the script to support another type, you'll want to tweak the URL being used, making sure to place $sign where appropriate:

 my $url = "http://astrology.yahoo.com/us/astrology/".                 "today/  $sign  "."dailyhoroscope.html"; 

Also, tweak the $preface and $anteface of the code surrounding the actual fortune :

 my $preface = '<font face="Arial" size="-1" color=black>'; my $anteface = '</font></TD></TR></table>'; # ante up! 

Alternatively, you could scrap the entire horoscope feature and combine in another of the data scrubbers within this book, utilizing only the iPod- related code in this hack.

See Also

  • VersionTracker (http://www. versiontracker .com) for other iPod utilities, including Pod2Go (for weather, stocks, and more); PodNotes (for the latest news and driving directions); and VoodooPad (for Wiki-like Notes editing).



Spidering Hacks
Spidering Hacks
ISBN: 0596005776
EAN: 2147483647
Year: 2005
Pages: 157

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net