Skip to content

Instantly share code, notes, and snippets.

@coolniikou
Created January 19, 2012 06:36
Show Gist options
  • Select an option

  • Save coolniikou/1638436 to your computer and use it in GitHub Desktop.

Select an option

Save coolniikou/1638436 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use Config::Pit;
use Getopt::Long;
use File::Slurp::Unicode qw(slurp);
use Text::Hatena;
use HTML::Entities;
use Data::Dumper;
my $config = pit_get('webdesignblog');
my %options;
GetOptions( \%options, qw/ --cat=s / );
run( \%options, @ARGV );
sub run {
my ( $opt, @args ) = @_;
my $field = &setup_files(@args);
my $mt = MTPOS->new($config);
my $post = $mt->newPost( $field, 0 );
my %cats = (
perl => { categoryId => '155' },
js => { categoryId => '156'}
);
my $catId = $cats{$opt->{cat}};
$mt->newCategories( $post, $catId );
# return $mt->publishPost( $post);
}
sub setup_files {
my $args = shift;
my @content = slurp( $args, { encoding => 'utf8' } );
my $title = shift(@content);
my $body = join( '', @content );
my ( $main, $more ) = $body =~ /(.*?)(?:>\|\|)(.*?)(?:\|\|<)?$/is;
$more = ">||". encode_entities($more). "||<";
my $fields = {
title => $title,
description => Text::Hatena->parse($main),
mt_text_more => Text::Hatena->parse($more)
};
}
package MTPOS;
use strict;
use warnings;
use XMLRPC::Lite;
sub new {
my ( $class, $args ) = @_;
bless {
proxy => $args->{proxy_url},
username => $args->{username},
password => $args->{password},
blogid => $args->{blogid}
}, $class;
}
sub call {
my ( $self, $method, @args ) = @_;
my $proxy_url = $self->{proxy};
my $proxy = XMLRPC::Lite->proxy($proxy_url);
return $proxy->call( $method, @args )->result;
}
sub newPost {
my ( $self, $content, $publish ) = @_;
return $self->call( 'metaWeblog.newPost', $self->{blogid},
$self->{username}, $self->{password}, $content, $publish );
}
sub newCategories {
my ( $self, $postid, $cats ) = @_;
unless ( ref $cats eq 'ARRAY' ) {
$cats = [$cats];
}
return $self->call( 'mt.setPostCategories', $postid, $self->{username},
$self->{password}, $cats );
}
sub publishPost {
my ( $self, $postid ) = @_;
return $self->call( 'mt.publishPost', $postid, $self->{username},
$self->{password} );
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment