-
Notifications
You must be signed in to change notification settings - Fork 0
/
hatenaSync.pl
100 lines (81 loc) · 2.86 KB
/
hatenaSync.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package MT::Plugin::OMV::hatenaSync;
# $Id$
use strict;
use MT::Entry;
use XML::Atom::Entry;
use XML::Atom::Client;
use vars qw( $VENDOR $MYNAME $VERSION );
($VENDOR, $MYNAME) = (split /::/, __PACKAGE__)[-2, -1];
(my $revision = '$Rev$') =~ s/\D//g;
$VERSION = '0.02'. ($revision ? ".$revision" : '');
use base qw( MT::Plugin );
my $plugin = __PACKAGE__->new({
name => $MYNAME,
id => lc $MYNAME,
key => lc $MYNAME,
version => $VERSION,
author_name => 'Open MagicVox.net',
author_link => 'http://www.magicvox.net/',
doc_link => 'http://www.magicvox.net/archive/2009/02111447/',
description => <<PERLHEREDOC,
<__trans phrase="Synchronize the posted entry with Hatena diary">
PERLHEREDOC
# l10n_class => $MYNAME. '::L10N',
blog_config_template => 'config.tmpl',
settings => new MT::PluginSettings([
[ 'htn_username', { Default => undef, scope => 'blog' } ],
[ 'htn_password', { Default => undef, scope => 'blog' } ],
]),
});
MT->add_plugin ($plugin);
sub instance { $plugin; }
MT->add_callback ('BuildFile', 5, $plugin, \&_entry_post_save);
sub _entry_post_save {
my ($eh, %opt) = @_;
my $ctx = $opt{Context};
my $entry = $ctx->stash ('entry')
or return 1;
my $blog_id = $entry->blog_id;
my $scope = "blog:$blog_id";
my $htn_username = &instance->get_config_value ('htn_username', $scope);
my $htn_password = &instance->get_config_value ('htn_password', $scope);
defined $htn_username and defined $htn_password
or return;# no settings
my $pdata = load_plugindata (key_name ($entry->id)) || {};
if (!$pdata->{EditURI} && $entry->status == MT::Entry::RELEASE()) {
my $atom_client = XML::Atom::Client->new;
$atom_client->username ($htn_username);
$atom_client->password ($htn_password);
my $atom_entry = XML::Atom::Entry->new;
$atom_entry->title ($entry->title);
$atom_entry->content (
sprintf "[%s:title=%s]\n\n%s",
$entry->permalink, $entry->title, $entry->text);
my $PostURI = "http://d.hatena.ne.jp/$htn_username/atom/blog";
$pdata->{EditURI} = $atom_client->createEntry ($PostURI, $atom_entry)
or return $atom_client->errstr;
save_plugindata (key_name ($entry->id), $pdata);
}
}
########################################################################
sub key_name { 'entry_id:'. $_[0]; }
use MT::PluginData;
sub save_plugindata {
my ($key, $data_ref) = @_;
my $pd = MT::PluginData->load({ plugin => &instance->id, key=> $key });
if (!$pd) {
$pd = MT::PluginData->new;
$pd->plugin( &instance->id );
$pd->key( $key );
}
$pd->data( $data_ref );
$pd->save;
}
sub load_plugindata {
my ($key) = @_;
my $pd = MT::PluginData->load({ plugin => &instance->id, key=> $key })
or return undef;
$pd->data;
}
1;
__END__