Created
June 30, 2011 15:32
-
-
Save amackey/1056482 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use WordPress::API::MediaObject; | |
use WordPress::API::Post; | |
use YAML qw(LoadFile); | |
use Getopt::Long qw(GetOptions); | |
my ($hostname, $username, $password); | |
if (-e "$ENV{HOME}/.blogpost") { | |
my ($defs) = LoadFile("$ENV{HOME}/.blogpost"); | |
($hostname, $username, $password) = | |
@$defs{qw(hostname username password)}; | |
} | |
my @pdfs; | |
my $title = ""; | |
GetOptions("hostname:s" => \$hostname, | |
"username:s" => \$username, | |
"password:s" => \$password, | |
"title:s"=> \$title | |
); | |
die "missing hostname, username, and password (check ~/.blogpost)\n" | |
unless $hostname && $username && $password; | |
die "usage: blogpost [--title \"my new post\"] file1.pdf [file2.pdf] [...]!\n" | |
unless @ARGV; | |
my $login = { proxy => $hostname, | |
username => $username, | |
password => $password | |
}; | |
my @urls; | |
for my $pdf (@ARGV) { | |
my $o = WordPress::API::MediaObject->new($login) | |
or die "couldn't generate a new media object; check credentials.\n"; | |
$o->load_file($pdf); | |
my $name = $pdf; | |
$name =~ s! ^.*/ | \.pdf$ !!xg; # same as `basename $pdf .pdf` | |
$o->name($name); | |
$o->upload(); | |
push @urls, [ $name, $o->url() ]; | |
} | |
if ($title) { | |
my $p = WordPress::API::Post->new($login); | |
$p->publish(0); | |
$p->title($title); | |
my $content = ""; | |
for my $url (@urls) { | |
$content .= <<EOHTML; | |
<h2>$url->[0]</h2> | |
<p>[gview file="$url->[1]"] | |
EOHTML | |
} | |
$p->description($content); | |
$p->save(); | |
print $p->url(), "\n"; | |
} else { | |
for my $url (@urls) { | |
print "[gview file=\"$url->[1]\"]\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment