Skip to content

Instantly share code, notes, and snippets.

@ysasaki
Created July 12, 2010 07:54
Show Gist options
  • Save ysasaki/472230 to your computer and use it in GitHub Desktop.
Save ysasaki/472230 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
package MT::Feed;
use strict;
use warnings;
sub new {
my $class = shift;
bless { entries => [], delimiter => "--------\n" }, $class;
}
sub add_entry {
my $self = shift;
push @{ $self->{entries} }, shift;
}
sub as_text {
my $self = shift;
join( $self->{delimiter}, map $_->as_text, @{ $self->{entries} } )
. $self->{delimiter};
}
package MT::Entry;
use strict;
use warnings;
use base 'Class::Accessor::Fast';
my @meta = qw/
author title basename status allow_comments allow_pings convert_breaks
date tags
/;
my @multi = qw/body/;
__PACKAGE__->mk_accessors( @meta, @multi );
sub as_text {
my $self = shift;
my @line;
foreach my $method (@meta) {
next unless $self->$method;
my $key = _to_key($method);
push @line, sprintf( "%s: %s", $key, $self->$method );
}
push @line, "-----";
foreach my $method (@multi) {
next unless $self->$method;
my $key = _to_key($method);
push @line, sprintf( "%s:", $key );
push @line, $self->$method;
push @line, "-----";
}
return join( "\n", @line ) . "\n";
}
sub _to_key {
my $method = shift;
uc join( ' ', split /_/, $method );
}
package main;
use strict;
use warnings;
use XML::RPC;
use Web::Scraper;
use HTTP::Date;
use DateTime;
use Encode qw(find_encoding);
my $blogid = 'xxxxxx';
my $username = 'yourname';
my $password = 'yourpasswd';
my $number = shift || 10;
my $endpoint = 'http://my.opera.com/yourname/blog/api/';
# fetch blog posts using XML RPC
my $x = XML::RPC->new($endpoint);
my $data = $x->call( 'metaWeblog.getRecentPosts',
$blogid, $username, $password, $number, );
# fetch content in HTML, and tags in TEXT
my $scraper = scraper {
process '//div[@class="content"]', 'content' => 'HTML';
process '//p[@class="tags"]', 'tags' => 'TEXT';
};
my $utf8 = find_encoding('utf8');
my $feed = MT::Feed->new;
foreach my $d (@$data) {
warn $d->{link};
my $entry = MT::Entry->new(
{ author => 'aloelight',
title => $d->{title},
basename => 'a.file',
status => 'Publish',
allow_comments => 1,
allow_pings => 1,
convert_breaks => 'richtext',
}
);
my $date = do {
my $dt = DateTime->from_epoch(
epoch => str2time( $d->{dateCreated} ),
time_zone => 'Asia/Tokyo',
);
$dt->strftime('%m/%d/%Y %I:%M:%S %p');
};
$entry->date($date);
my $ret = $scraper->scrape( URI->new( $d->{link} ) );
$ret->{content} =~ s/<pre>/<pre class="prettyprint">/g;
$entry->body( $utf8->encode( $ret->{content} ) );
$entry->tags( $utf8->encode( $ret->{tags} ) ) if $ret->{tags};
$feed->add_entry($entry);
}
print $feed->as_text;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment