Hack30.Create a Portal for Your Memory Stick Files


Hack 30. Create a Portal for Your Memory Stick Files

Why not keep a directory of all the local images and text files on your Version 2.0 PSP in HTML format, for easy access via the browser? Better yet: why not automate the process with Perl and automatically grab all of your psp-tagged del.icio.us bookmarks in there to boot?

Well, you have a PSP with Version 2.0 or later of the firmware, so you don't have all the great homebrew emulators available for the earlier versions. However, unless you downgrade from Version 2.0 [Hack #11], you are making do by loading every JavaScript-capable game or program you can find onto your Memory Stick and converting all your documents to text files for viewing via the nifty little browser that Sony included with Version 2.0 of the firmware.

Let's really trick out your PSP by putting together a Perl script that will run on Windows, Mac OS X, or Linux, with only a few dependencies. This script will automatically scrape the photos contained in your Memory Stick's /PSP/PHOTO/ folder, scrape any text files contained in /PSP/NOTES/, optionally grab any links on your del.icio.us (http://del.icio.us) account that are tagged with "psp," and build an index.html file linking to all these files and located at the root directory of your Memory Stick.

3.11.1. Install the Dependencies

This script uses PerlMagick and cURL to work its magic. Since Perl is included in most modern systems, there are only a few bits that you need to make sure are in place before beginning.

3.11.1.1. Windows.

If you are on Windows, first make sure you have Perl installed (we suggest ActivePerl, which you can get from http://www.activestate.com). Next, grab PerlMagick, which is part of the ImageMagick package available under the Windows Binary Release section of the ImageMagick site (http://www.imagemagick.org/script/binary-releases.php). Next, install cURL from http://curl.haxx.se/download.html and put the executable in your PATH. C:\Windows will work, but we suggest you add an entry to your PATH and put it there. If you choose the version of cURL with SSL support, you may need to install additional libraries. This script does not require SSL support.

3.11.1.2. Mac OS X.

If you are on Mac OS X, you need to install ImageMagick. Download the source of ImageMagick (http://www.imagemagick.org/script/install-source.php). Extract the source, and then from the Terminal (which is located in /Applications/Utilities/), type the following commands:

 cd ImageMagick-<version> ./configure && make sudo make install 

Each line is a separate command. Enter the first command, then hit return and wait while a bunch of text flies by in the Terminal window. This will build ImageMagick from the source. If the string of text comes up with any errors at the end after the second command, then try running the command again with sudo at the beginning. After the third command, when sudo is invoked you will be asked for a password (and possibly a warning, if this is the first time you invoke sudo). Enter the Administrator password for your computer.

After ImageMagick successfully compiles, you may need to compile Perl-Magick, if it was not compiled automatically. Enter the following commands to build PerlMagick (the PerlMagick directory is a subdirectory of the ImageMagick-<version> directory):

 cd PerlMagick perl Makefile.PL sudo make install 

Again, if there are any errors in step two, try invoking sudo at the beginning of the command.

Make sure you check the installation documentation that accompanies ImageMagick (in particular, the README and INSTALL files), since you may need some other programs that ImageMagick depends on. For example, on Mac OS X, we needed to install libjpeg (see http://www.ijg.org/) from the source tarball (jpegsrc.v6b.tar.gz), using the following commands:

 tar xvfz jpegsrc.v6b.tar.gz cd jpeg-6b/ ./configure && make sudo make install sudo make install-lib sudo ranlib /usr/local/lib/libjpeg.a 

3.11.1.3. UNIX/Linux.

Make sure that you have PerlMagick installed. This should be available in your Linux distributions package repository. If not, you can follow the Mac OS X instructions to compile it from source. You will also need cURL.

3.11.2. The Code

 #!/usr/bin/perl -w use strict; use Image::Magick; my $MAXDEPTH = 1; # Maximum depth of photo albums opendir PSP, "PSP" || die "Could not open PSP: $!\n"; open HTML, (">index.html") || die "Could not open index.html: $!\n"; # Display the top of the HTML file. # print HTML <<EOF; <html> <head><title>My PSP</title></head> <body style="width: 480px; height: 272px;"> EOF # Check the photos and notes subdirectories # while (my $dir = readdir(PSP)) {   if ($dir eq "PHOTO") {     list_photos("PSP/PHOTO", 0, "<hr><h2>My Pictures</h2>");   }   if ($dir eq "NOTES") { list_items("PSP/NOTES", "My Files") }; } closedir(PSP); # Import PSP bookmarks from del.icio.us # its_delicious(); # Display the end of the HTML file. # print HTML "</body>"; close (HTML); # Enumerate anything found in the notes subdirectory # and add a link to it. #    sub list_items {     my $dirname = shift;     my $what = shift;     print HTML qq[<hr><h2>$what</h2>\n<ul>\n];     opendir DIR, "$dirname" || die "Could not open $dirname: $!\n";     while (my $item = readdir(DIR)) {            # Skip things that aren't files.       #       next unless -f "$dirname/$item";       # Construct a hyperlink to the target filename.       #       my $target = "$dirname/$item";       print HTML qq[<li><a href="$target">$item</a></li>\n];     }     print HTML qq[</ul>\n];     closedir (DIR);   }   # Enumerate anything found in the photos subdirectory   # and add a link to it.   #   sub list_photos {     my $dir = shift;     my $depth = shift; $depth++;     my $separator = shift;     # Create the thumb directory if it does not exist.     my $thumbdir = "$dir/PSPTHUMBS";     if (not -d $thumbdir) {       mkdir ($thumbdir, 0775) || die "Could not mkdir $thumbdir: $!\n";     }     print HTML $separator;     my $handle;     opendir $handle, "$dir" || die "Could not open $dir: $!\n";     while (my $file = readdir($handle)) {       # Skip things that aren't files.       #       next if -d "$dir/$file";       # Scale the photo       #       my $thumb = "${file}_sm.jpg";       my $image = new Image::Magick;       my $err = $image->Read("$dir/$file");       if ($err) {         warn $err;       } else {         print STDERR "Processing image $file…\n";       }       $image->Scale(height => "32",                       width => "32");       $image->Write("$thumbdir/$thumb");       # Construct a hyperlink to the target filename.       #       my $img = "$thumbdir/$thumb";       my $target = "$dir/$file";       print HTML qq[<img src="/books/4/527/1/html/2/$img" />] .         qq[<a href="$target">$file</a><br/>\n];   }   # Collect all the images in subdirectories   #   rewinddir($handle);   while (my $file = readdir($handle)) {     next if $file eq ".";     next if $file eq "..";     next if $file =~ /^pspthumbs$/i;     if (-d "$dir/$file" && $depth == $MAXDEPTH) {       list_photos("$dir/$file", $depth, "<p>$file:</p>");     }   }   closedir ($handle); } # Import del.icio.us bookmarks tagged PSP # sub its_delicious {   print <<EOF; http://del.icio.us is a social bookmarking service. If you've got an account on del.icio.us, I can import your PSP tagged bookmarks EOF   print "Tell me your del.icio.us username or leave blank to skip: [] ";   my $username = <STDIN>;   chomp($username); chomp($username); print "[$username]\n";   if ($username) {     my $url = "http://del.icio.us/html/$username/psp" .               "?count=9999&tags=no&rssbutton=no";     open (CURL, "curl \"$url\" |") || die "Could not launch cURL: $!\n";     print HTML qq[<hr><h2>PSP Bookmarks</h2>\n];     while (<CURL>) {       print HTML $_;     }     close(CURL);   } } 

Type this code into your text editor of choice and save it as make_index.pl. Place this file in the root directory of your PSP's Memory Stick, either by dragging it there or executing one of the following commands:


Unix, Mac OS X, Linux

 cp make_index.pl /Volumes/Untitled 


Windows

 xcopy make_index.pl F:\ 

If your Memory Stick has a name other than Untitled, Mac OS X users should use the appropriate directory name. If you are on Windows, choose the drive letter (F:, in these examples) that your PSP was assigned. If you are on Linux, be sure to replace /Volumes/Untitled with the mount point [Hack #3] of your PSP.


To run the script, you must run the following command from the command line.

In Mac OS X, use the Terminal (located in /Applications/Utilities/) and run the following commands:

 cd /Volumes/Untitled perl make_index.pl 

These are the same commands you will use in UNIX/Linux.

In Windows, open a command prompt and run:

 F: cd \ perl make_index.pl 

Once the script begins running, you will be prompted for your del.icio.us username. If you enter nothing, this step will be skipped. After the command has completed running, you can navigate to the root level of your Memory Stick and you will see a newly created index.html file. Double-click on this file to launch it in your computer's web browser and inspect the results of the code.

3.11.3. On Your PSP

Once you have this script created, you can run it every time your PSP is connected to your computer to update the index.html file.

On your PSP, make sure that you bookmark file:/index.html in the browser, so that you can easily navigate to this index of all of your photos, text files stored in the Notes folder, and your del.icio.us bookmarks tagged with PSP. Consider making it your home page.

3.11.4. Hacking the Hack

In its current form, this script is both rather basic and easily hacked to add more features.

3.11.4.1. More tags.

To grab more tags, you can modify this snippet of code:

 if ($username) {     my $url = "http://del.icio.us/html/$username/psp" .               "?count=9999&tags=no&rssbutton=no"; 

To have the script grab all of your del.icio.us bookmarks regardless of tags, or to add other tags in the form of +games+playstation+whatever to further refine the bookmarks you retrieve, simply remove the /psp.

3.11.4.2. Automate it even more.

On OS X, you could easily create a simple AppleScript to run each time your PSP is mounted so that you don't have to remember to enter the commands to run the Perl script:

 Tell Finder     do shell script "cd /Volumes/Untitled"     do shell script "perl make_index.pl" end tell 

Save this code as an AppleScript app called PSP on your desktop and simply double-click it whenever the PSP is mounted.

3.11.4.3. Add some more links.

Also, if there are a few favorite sites that you always want linked on this page, just add that information in the script. For example, this snippet of code writes the beginning part of the index.html file:

 # Display the top of the HTML file. # print HTML <<EOF; <html> <head><title>My PSP</title></head> <body style="width: 480px; height: 272px;"> EOF 

After the <body> declaration and before the EOF, you can add a whole other section of HTML if you like. For example, something like the following would add links for Google and Yahoo!:

 # Display the top of the HTML file. # print HTML <<EOF; <html> <head><title>My PSP</title></head> <body style="width: 480px; height: 272px;"> <h1>Search Engines</h1> <a href="http://www.google.com">Google</a><br /> <a href="http://www.yahoo.com">Yahoo!</a><br /> EOF 

3.11.4.4. Take the Web with you.

To keep it quick and dirty, we used cURL instead of LWP (http://lwp.linpro.no/lwp/), a collection of Perl modules for hacking the Web. You could easily change this script to use LWP, although you'd need to make sure you install LWP on each machine you want to run this script. But while you're adding LWP support, you could take advantage of some of LWP's advanced features. For example, in addition to pulling down your del.icio.us bookmarks, you could mirror a few pages from each linked site for offline reading. For more information on hacking with LWP, see Spidering Hacks (O'Reilly).

Have fun hacking away at the script.

Brian Jepson and C.K. Sample III




PSP Hacks
PSP Hacks: Tips & Tools for Your Mobile Gaming and Entertainment Handheld
ISBN: 0596101430
EAN: 2147483647
Year: 2006
Pages: 108

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