Hack42.Podcast by Email


Hack 42. Podcast by Email

Use an email parser on your ISP server to automate podcasting to Movable Type.

Wouldn't it be great if you could email your podcasts to your weblog and have them posted automatically? The text content of the message becomes the podcast entry, the subject becomes the title, and any attachments are put into the right spot and are linked into the body of the blog entry.

This simple Perl script does just that.

Hack 6.7.1. The Code

Save this code as poster.pl:

 use FileHandle; use MIME::Base64; use strict; # The base MovableType directory use constant MT_DIR =>  "/Library/WebServer/CGI-Executables/mt/";  # The blog ID to post to use constant BLOG_ID => 1;  # The blog login and password use constant BLOG_LOGIN =>  "Melody";  use constant BLOG_PASSWORD =>  Nelson";  # The UNIX directory where the podcasts should go use constant PODCAST_DIR =>  "/Library/WebServer/Documents/podcast/";  # The base URL to use for the podcasts use constant PODCAST_URL =>  "http://myhost.com/podcast/";  BEGIN { unshift @INC, MT_DIR . 'lib'; unshift @INC, MT_DIR . 'extlib'; } use MT::XMLRPCServer; use SOAP::Lite; $MT::XMLRPCServer::MT_DIR = MT_DIR; use constant WAITING_FOR_BOUNDARY => 1; use constant IN_HEADER => 2; use constant IN_BODY => 3; my $boundary = undef; my $state = WAITING_FOR_BOUNDARY; my $header = ""; my $body = ""; my $subject = undef; my $textbody = undef; my $podcasturl = undef; sub process($$)  {  my ( $header, $body ) = @_; if ( $header =~ /text\/plain;/ ) { $textbody = $body; } if ( $header =~ /audio\/mpeg/ && $header =~ /filename=(.*?)$/ ) { my $filename = $1; $podcasturl = PODCAST_URL.$filename; $body =~ s/\n//g; $body =~ s/\r//g; my $out = new FileHandle( PODCAST_DIR.$filename, "w" );  binmode $out;  print $out decode_base64( $body );  $out->close();  } } while( <> ) { if ( !$boundary && /Content\-Type\: multipart\/mixed; boundary=(.*?)$/ )   { $boundary = "--".$1."\n";  }  if ( !$subject && /^Subject:\s+(.*?)$/ ) { $subject = $1; } if ( $state == IN_HEADER ) { $state = IN_BODY if ( $_ eq "\n" ); chomp; if ( /^\s/ ) { s/^\s+//; $header .= $_; } else { $header .= "\n".$_; } } if ( $_ eq $boundary ) { process( $header, $body ) if ( $header && $body ); $state = IN_HEADER; $header = ""; $body = ""; } if ( $state == IN_BODY ) { $body .= $_; } } $textbody .= "\n\n<a href=\"$podcasturl\">Listen to '$subject'</a>"; my $item = { title => $subject, description => $textbody }; MT::XMLRPCServer::newPost( "", BLOG_ID, BLOG_LOGIN, BLOG_PASSWORD, $item, 1 ); 

Hack 6.7.2. Running the Hack

This script is meant to be called by Sendmail or Qmail when new mail is sent to an account. With Sendmail, you create a .forward file in the home directory of your ISP account. With Qmail, the file is .qmail. Either way, the content is the same, and it looks like this:

 | perl /myhome/myscripts/poster.pl  

The pipe symbol (|) means the mailer should run the script and pipe the contents of the message into it through STDIN.

After you have uploaded the script, you need to change the variables at the top to match your setup. The first variable, MT_DIR, is the full path to the Movable Type installation directory. It's important because the script will use that installation to post the blog entry using the XML-RPC newPost handler.

The PODCAST_DIR constant is the Unix directory where podcast audio or video files should be stored on the server. The PODCAST_URL constant is the corresponding URL for that directory.

The other interesting part of this script is the code that decodes the Base64encoded attachment. The binmode call is important here because it throws the file handle into binary mode so that the MP3 file isn't corrupted as it's built.

To test the script, send your email account (the primary account associated with your hosted site) a message with an MP3 file as an attachment. Normally your Unix email address will be separate from your POP3 email boxes. So, you can use this script on your Unix email address. You should check with your ISP to make sure this configuration can work for you.

The great part about this script is that it can take email from any source and post it. You can upload podcasts from your PC. On newer phones, you can record sound and then send it as an attachment over email and podcast directly from your phone!

Hack 6.7.3. See Also

  • "Blog Your Podcast" [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