Hack41.Feed Your MP3s to Movable Type


Hack 41. Feed Your MP3s to Movable Type

Use Movable Type's XML-RPC support to podcast your MP3s automatically.

Perl makes it easy to create a script that will podcast an MP3 for you automatically, using Movable Type's support for automatic blogging through its XML-RPC mechanism. This is partnered on the Perl side by the Net:: MovableType module [Hack #7].

Hack 6.6.1. The Code

Save this file as ap.pl:

 use Net::MovableType; use MP3::Info; use Net::SFTP; use strict; # The specifics of your MovableType blog use constant MT_XMLRPC_SERVICE =>  "http://myhost.com/mt/mt-xmlrpc.cgi "; use constant MT_LOGIN =>  "Melody "; use constant MT_PASSWORD =>  "Nelson "; use constant MT_BLOGID =>  1;  # The base URL of the directory where the podcasts go use constant PODCAST_URL_BASE =>  "http://myhost.com/podcast/ ";  # The remote directories for SFTP to use to login and upload # the file use constant REMOTE_DIRECTORY =>  "html/podcast/  >"; use constant REMOTE_HOST =>  "localhost  >"; use constant REMOTE_LOGIN =>"  username  >"; use constant REMOTE_PASSWORD =>  "password  >"; # Post an entry to the blog using MovableType's XML-RPC mechanism # through Net::MovableType  sub postToBlog($$$)      {  my ( $title, $comment, $podcastURL ) = @_;     my $mt = new Net::MovableType( MT_XMLRPC_SERVICE,      MT_LOGIN, MT_PASSWORD );     $mt->blogId( MT_BLOGID );     my $description = $comment;    $description .= "\n\n";    $description .= "<a href=\"$podcastURL\">Listen to '$title'</a>";                  my $entry = { title => $title, description => $description };        $mt->newPost( $entry, 1 );           } # A progress indicator for SFTP my $g_last_percent = -100; sub progress {   my($sftp, $data, $offset, $size) = @_;  my $percent = int ( ( $offset / $size ) * 100 );  if ( $percent - $g_last_percent > 5 )  {   print " ".$percent."%\n";   $g_last_percent = $percent;       }     }   # Post the MP3 file by uploading it to the site and then adding    # the blog entry     sub postMP3($)   {    my ( $fileName ) = @_;  # Get the title and comment of the MP3  my $info = new MP3::Info( $fileName );  my $title = $info->title();  my $comment = $info->comment(); # Upload the podcast using SFTP print "Uploading podcast file…\n"; my $sftp = new Net::SFTP( REMOTE_HOST,    user => REMOTE_LOGIN, password => REMOTE_PASSWORD );  $sftp->put( $fileName, REMOTE_DIRECTORY.$fileName, \&progress ); print "Done.\n"; # Post the blog entry print "Posting the blog entry\n"; postToBlog( $title, $comment, PODCAST_URL_BASE.$fileName ); } # Check to make sure that the user has specified an MP3 file die "You must specify an MP3 file" unless ( $ARGV[0] ); # Post the MP3 file to the site as a podcast postMP3( $ARGV[0] ); 

You can get the title and contents of the blog entry from the title and comment files of the ID3 tags in your MP3 file. After that, all you need to do is upload the file, and you can use Net::SFTP to do that using the SFTP protocol.

To start using the script, you will have to change the constants at the top that specify the location of the blog, and the login parameters for the server where it uploads the podcast.

The script uses three modules: MP3::Info to get the tag information from the MP3 file, Net::SFTP to do the SFTP upload, and Net::MovableType to post the blog entry. All of these modules are available through CPAN, and you can use this command to install them automatically on Unix machines:

 % perl -MCPAN -e "install Net::MovableType" 

Replace the module name with the one you want to install, and the CPAN module will download and install it automatically [Hack #7].

Hack 6.6.2. Running the Hack

To test the script, I built a simple sound file in Audacity and set the title and comment tags to something meaningful before I exported it as MP3. I saved the file as test1.mp3 and ran the script this way:

 % perl ap.pl test1.mp3  Uploading podcast file… 0% 35% 70% Done. Posting the blog entry 

After the script executes, you can check your blog to make sure it was posted properly. If you want to alter the text format of the posting, just change the section of the postToBlog subroutine that builds the description string.

This script depends on the MT::Enclosure plug-in being installed to function properly [Hack #38].



    Podcasting Hacks
    Podcasting Hacks: Tips and Tools for Blogging Out Loud
    ISBN: 0596100663
    EAN: 2147483647
    Year: 2003
    Pages: 144

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