#!/usr/bin/perl # # post2blog - a tool to post a new entry to Movable Type with pictures and locations # # Copyright (C) 2003 - 2004 Jiro Motonaga # All rights reserved. # This is free software with NO WARRANTY. # # You can redistribute it and/or modify it under the terms of # the GNU General Public License version 2. # BEGIN { my $MT_DIR = '/Library/WebServer/Documents/mt/'; unshift @INC, $MT_DIR . 'extlib'; unshift @INC, $MT_DIR . 'lib'; } use strict; my $url = 'http://www.your-site.com/movabletype/mt-xmlrpc.cgi'; # CGIPath + 'mt-xmlrpc.cgi' my $blog_id = 2; my $username = 'Melody'; my $password = 'Nelson'; my $parse_path = '/Users/Shared/post2blog'; my ($maxx, $maxy) = (320, 320); use MIME::Parser; #use Data::Dumper; use File::Basename; use Jcode; use XMLRPC::Lite; use Image::Info qw(image_info dim); use MT::Sanitize; my $j = new Jcode; #open(OUT, ">$parse_path/mtmail"); my $str=''; while() { $str = $str.$_; #print OUT $_; } #close(OUT); my $parser = new MIME::Parser; $parser->output_under($parse_path); $parser->filer->ignore_filename(1); my $entity = $parser->parse_data($str); my $head = $entity->head; my $sub = $head->get('Subject'); $sub = MT::Sanitize->sanitize($sub); my $message_id = $head->get('Message-ID'); my $cnttype = $entity->mime_type; $sub = $j->set($sub)->mime_decode->utf8; $sub =~ s/[\r\n]//g; $sub =~ s/ +$//g; $message_id =~ s/[<>]//g; $message_id =~ s/\@.*//g; $message_id =~ s/[\r\n]//g; my $partnum = $entity->parts; my (@texts, @attachs, $cnttype, $cntpath, $cntname); if ($entity->is_multipart) { # multi parts for (my $pn = 0; $pn < $partnum; $pn++) { $cnttype = $entity->parts($pn)->mime_type; $cntpath = $entity->parts($pn)->bodyhandle->path; $cntname = $entity->parts($pn)->head->mime_attr('content-type.name') || $entity->parts($pn)->head->mime_attr('content-disposition.filename'); $cntname = $j->set($cntname)->mime_decode->utf8; arrange_parts($pn, $cnttype, $cntpath, $cntname); } } else { # single part $cnttype = $entity->mime_type; $cntpath = $entity->bodyhandle->path; $cntname = $entity->head->mime_attr('content-type.name') || $entity->head->mime_attr('content-disposition.filename'); $cntname = $j->set($cntname)->mime_decode->utf8; arrange_parts(1, $cnttype, $cntpath, $cntname); } my $cont; foreach (@texts) { open(CONT, $_); while () { s/\r\n/\n/g; s/\r/\n/g; $cont = $cont.$_; } close(CONT); } $cont = $j->set($cont)->utf8; $cont = MT::Sanitize->sanitize($cont); my @conts = split(/\n\n\n/, $cont); my $description = shift(@conts); my $text_more = join("\n\n\n", @conts); my $title = $sub; my $rpc = XMLRPC::Lite->new; $rpc->proxy($url); foreach my $fileref (@attachs) { my ($file, $orgfilename) = @$fileref; my ($filename, $filepath, $suffix) = fileparse($file, '\.(jpe?g|gif|png)'); open(BIN, $file); my $bindata; while () { $bindata = $bindata.$_; } close(BIN); my $ret = $rpc->call('metaWeblog.newMediaObject', $blog_id, $username, $password, { 'bits' => $bindata, 'name' => $filename.$suffix }, 1 ); #print Dumper $ret->result; #print Dumper $ret->fault; my $returl = $ret->result->{url}; if ($returl =~ /\.jpe?g$/i) { my $info = image_info($file); if (ref $info) { my @lats = $info->{GPSLatitude}; my @lons = $info->{GPSLongitude}; my @alts = $info->{GPSAltitude}; if ((scalar @lats[0])&&(scalar @lons[0])&&($lats[0][1]*$lats[0][3]*$lats[0][5]*$lons[0][1]*$lons[0][3]*$lons[0][5])) { my $dlat = $lats[0][0]/$lats[0][1] + $lats[0][2]/$lats[0][3]/60 + $lats[0][4]/$lats[0][5]/3600; my $dlon = $lons[0][0]/$lons[0][1] + $lons[0][2]/$lons[0][3]/60 + $lons[0][4]/$lons[0][5]/3600; @lats = deg2dms($dlat); @lons = deg2dms($dlon); my $lat = $lats[0].'.'.$lats[1].'.'.$lats[2]; my $lon = $lons[0].'.'.$lons[1].'.'.$lons[2]; my $map = "http://www.at-navi.com/map/checkMap.jsp?lat=$lat&lon=$lon&unit=0&datum=0&sc=0"; $map = "Map"; if ((scalar @alts)&&($alts[0][1])) { my $alt = $alts[0][0]/$alts[0][1]; $map = "$map (Altitude ${alt}m)"; } #$map = $j->set($map)->utf8; $description = "$map
$description"; #print "$map\n"; } } } if ($returl =~ /\.(jpe?g|gif|png)$/i) { use Image::Size; my ($imgx, $imgy) = imgsize($file); if ($imgx > $maxx) { $imgy = sprintf("%d", $imgy * ($maxx / $imgx)); $imgx = $maxx; } $description = "\"$orgfilename\"
$description"; } else { $description = "$orgfilename
$description"; } } my $ret = $rpc->call('metaWeblog.newPost', $blog_id, $username, $password, { 'title' => $title, 'description' => $description, 'mt_text_more' => $text_more }, 1 ); #print Dumper $ret->result; #print Dumper $ret->fault; #$parser->filer->purge; sub arrange_parts { my ($pn, $cnttype, $cntpath, $cntname) = @_; if ($cnttype =~ /^text\/plain$/i) { # plain text push(@texts, $cntpath); } else { # attachment my ($filename, $filepath, $suffix) = fileparse($cntpath, '\..+'); my $orgsuffix; $orgsuffix = $1 if ($cntname =~ /.+(\..+)/); if ("$filename$suffix" ne $cntname) { rename($cntpath, "$filepath${message_id}_$pn$orgsuffix"); $cntpath = "$filepath${message_id}_$pn$orgsuffix"; } unshift(@attachs, [ $cntpath, $cntname ]); if ($orgsuffix eq '.amc') { amc2thumb($cntpath); } } } sub amc2thumb { my $amc = shift; my ($filename, $filepath, $suffix) = fileparse($amc, '\.amc'); my $mpg = "$filepath$filename.mpg"; my $jpg = "$filepath${filename}_mpg\%d.jpg"; my $jpg1 = "$filepath${filename}_mpg1.jpg"; system("/usr/local/bin/ffmpeg -f m4v -i $amc -f jpeg -t 0.01 $jpg"); unshift(@attachs, [ $jpg1, "$filename" ]); 1; } sub deg2dms { my($d) = @_; my($m, $s, $sf); $sf = int($d*360000 + 0.5); $s = sprintf("%02d", $sf / 100 % 60); $m = sprintf("%02d", $sf / 6000 % 60); $d = int($sf/360000); $sf %= 100; return ($d, $m, "$s.$sf"); }