Changeset 174

Show
Ignore:
Timestamp:
01/14/06 20:36:25 (3 years ago)
Author:
ogawa
Message:

Now updating hatena entries when title and keywords are changed.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • AddToHatenaBookmark/trunk/AddToHatenaBookmark.pl

    r169 r174  
    1313use base 'MT::Plugin'; 
    1414use vars qw($VERSION); 
    15 $VERSION = '0.01'; 
     15$VERSION = '0.02'; 
    1616 
    1717my $plugin = MT::Plugin::AddToHatenaBookmark->new({ 
     
    3434                 5, $plugin, \&post); 
    3535 
    36 use MT::Util qw(encode_html); 
    3736use MT::Log; 
    3837use MT::I18N; 
     
    4241sub post { 
    4342    my ($eh, $app, $obj) = @_; 
    44     return if !UNIVERSAL::isa($obj, 'MT::Entry') || $obj->status != MT::Entry::RELEASE(); 
     43    return unless $obj->isa('MT::Entry') && ($obj->status == MT::Entry::RELEASE()); 
    4544 
    4645    my $blog_id = $obj->blog_id; 
    4746 
    48     my $config = $plugin->get_config_hash("blog:$blog_id") or return; 
     47    my $config = $plugin->get_config_hash('blog:' . $blog_id) or return; 
    4948    my $username = $config->{hatena_username} or return; 
    5049    my $password = $config->{hatena_password} or return; 
    51  
    52     my $comment = $obj->keywords ? keywords2comment($obj->keywords) : ''; 
    53     if ($comment) { 
    54         my $enc = MT::ConfigMgr->instance->PublishCharset || 'utf-8'; 
    55         $comment = MT::I18N::encode_text($comment, $enc, 'utf-8'); 
    56     } 
    5750 
    5851    my $link = XML::Atom::Link->new; 
     
    6457    $entry->title('dummy'); 
    6558    $entry->add_link($link); 
    66     $entry->summary($comment) if $comment; 
    6759 
    6860    my $hatena = XML::Atom::Client->new; 
    6961    $hatena->username($username); 
    7062    $hatena->password($password); 
     63 
    7164    my $editURI = $hatena->createEntry('http://b.hatena.ne.jp/atom/post', $entry); 
     65    unless ($editURI) { 
     66        add_log($blog_id, 'createEntry failed: ' . $hatena->errstr); 
     67        return; 
     68    } 
    7269 
     70    my $entry_old = $hatena->getEntry($editURI); 
     71    unless ($entry_old) { 
     72        add_log($blog_id, 'getEntry failed: ' . $hatena->errstr); 
     73        return; 
     74    } 
     75 
     76    my $title_old = $entry_old->title; 
     77    my $summary_old = extract_summary($entry_old); 
     78 
     79    my $title_new = $obj->blog->name . ': ' . $obj->title; 
     80    my $summary_new = keywords2summary($obj->keywords) || ''; 
     81 
     82    my $enc = MT::ConfigMgr->instance->PublishCharset || 'utf-8'; 
     83    $title_new = MT::I18N::encode_text($title_new, $enc, 'utf-8') 
     84        if $title_new; 
     85    $summary_new = MT::I18N::encode_text($summary_new, $enc, 'utf-8') 
     86        if $summary_new; 
     87 
     88    my $msg; 
     89    if ($title_old eq $title_new && $summary_old eq $summary_new) { 
     90        $msg = 'updateEntry skipped: ' . $editURI; 
     91    } else { 
     92        my $entry_new = XML::Atom::Entry->new; 
     93        $entry_new->title($title_new); 
     94        $entry_new->summary($summary_new) if $summary_new; 
     95 
     96        $msg = $hatena->updateEntry($editURI, $entry_new) ? 
     97            'updateEntry suceeded: ' . $editURI : 
     98            'updateEntry failed: ' . $hatena->errstr; 
     99    } 
     100    add_log($blog_id, $msg); 
     101} 
     102 
     103sub add_log { 
     104    my ($blog_id, $message) = @_; 
    73105    my $log = MT::Log->new; 
    74106    $log->blog_id($blog_id); 
    75     $log->message($editURI ? 
    76                   'Hatena request suceeded: ' . $editURI : 
    77                   'Hatena request failed: ' . $hatena->errstr); 
     107    $log->message('[' . $plugin->name . '] ' . $message); 
    78108    $log->save or die $log->errstr; 
    79109} 
    80110 
    81 sub keywords2comment { 
     111# extract summary text from a hatena entry 
     112sub extract_summary { 
     113    my ($entry) = @_; 
     114    my $summary = ''; 
     115    my $dc = XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/'); 
     116    for my $subject ($entry->getlist($dc, 'subject')) { 
     117        $summary .= '[' . $subject . ']'; 
     118    } 
     119    $summary; 
     120} 
     121 
     122# convert MT keywords to summary text 
     123sub keywords2summary { 
    82124    my ($str) = @_; 
    83125    return '' unless $str; 
     
    86128    return '' unless $str; 
    87129 
    88     my $comment = ''; 
     130    my $summary = ''; 
    89131    if ($str =~ m/[;,|]/) { 
    90         # tags separated by non-whitespaces 
     132        # separated by non-whitespaces 
    91133        while ($str =~ m/(\[[^]]+\]|"[^"]+"|'[^']+'|[^;,|]+)/g) { 
    92134            my $tag = $1; 
    93135            $tag =~ s/(^[\["'\s;,|]+|[\]"'\s;,|]+$)//g; 
    94             $comment .= '[' . $tag . ']' if $tag; 
     136            $summary .= '[' . $tag . ']' if $tag; 
    95137        } 
    96138    } else { 
    97         # tags separated by whitespaces 
     139        # separated by whitespaces 
    98140        while ($str =~ m/(\[[^]]+\]|"[^"]+"|'[^']+'|[^\s]+)/g) { 
    99141            my $tag = $1; 
    100142            $tag =~ s/(^[\["'\s]+|[\]"'\s]+$)//g; 
    101             $comment .= '[' . $tag . ']' if $tag; 
     143            $summary .= '[' . $tag . ']' if $tag; 
    102144        } 
    103145    } 
    104     $comment; 
     146    $summary; 
    105147} 
    106148