#!/usr/bin/perl # # hate2blogger.pl - make it possible to write Blogger like HatenaDiary # # ver 0.1 # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. use strict; use warnings; use LWP::UserAgent; use HTTP::Headers; use HTTP::Request::Common; use HTTP::Cookies; use Encode; #---設定項目-------------------------------------------- # Bloggerでのハンドルネーム my $name=""; # Bloggerアカウントのメールアドレス my $email=""; # Bloggerアカウントのパスワード my $password=""; # BlogのURL my $url=""; # 続きを読む 記法で表示する文字(!日本語使う時はUTF-8に変換すること!) my $readmore_string="続きを読む"; #------------------------------------------------------ my $debug=0; Encode::from_to($readmore_string,"euc-jp","utf-8"); my $ua=LWP::UserAgent->new; my $google_login_url="https://www.google.com/accounts/ClientLogin"; my $google_post_id=""; my $google_post_url="http://www.blogger.com/feeds//posts/default"; my $auth; my $blog_id; my $post_content=< <!-- subject --> $name $email EOM sub debug{ print shift if($debug); } sub login{ return if(defined $auth); my %form=( 'accountType' => 'HOSTED_OR_GOOGLE', 'Email' => $email, 'Passwd' => $password, 'service' => 'blogger', 'source' => 'nawota-hate2blogger-test',); my $res=$ua->request( HTTP::Request::Common::POST("$google_login_url", \%form)); $res->content() =~ m/^Auth=(.*)$/m; $auth=$1; debug "auth:$auth\n"; } sub get_bid{ return if(defined $blog_id); # get blog id my $res=$ua->request( HTTP::Request::Common::GET($url."feeds/posts/default")); $res->content() =~ m!tag:blogger.com,1999:blog-(\d+)!; $blog_id=$1; debug "blog_id:$blog_id\n"; } sub post{ my $method=shift; # post $google_post_url=~s/$google_post_id/$blog_id/; my $head=new HTTP::Headers('Content_Type' => 'application/atom+xml; charset=UTF-8', 'Authorization' => "GoogleLogin auth=$auth",); my $req; if($method eq "POST"){ $req=HTTP::Request->new(POST => "$google_post_url",$head,$post_content); }else{ $req=HTTP::Request->new(PUT => "$google_post_url",$head,$post_content); } debug $req->as_string,"\n"; my $res=$ua->request($req); return $res->as_string; } # load diary my $file=shift or die "Usage:$0 "; my $body=""; open DIARY,"< $file"; my $subject=;chomp $subject; my ($in_pre,$in_deflist,$in_table,$list_level,$readmore,@list_type)=(0,0,0,0,0,()); while(){ if($in_pre){ m/^\|\|<$/ and $in_pre=0,$body.="\n",next; s/&/&/g; s//>/g; $body.="$_"; next; } if($in_deflist && $_!~m/^:/){$body.="\n";$in_deflist=0;} if($in_table && $_!~m/^\|/) {$body.="\n";$in_table=0;} #isbn check if(/^([-+]*)(.*)$/){ my $curlv=length($1); my $cur_type=(pop @list_type); my $tag= (substr($1,0,1) eq "-")?"ul":"ol"; if(defined $cur_type){ $curlv=0 if($tag ne $cur_type); push @list_type,$cur_type; } while($list_level>$curlv){ $body.="\n\n"; $list_level--; } $curlv=length($1); if($curlv){ if($curlv > $list_level){ chomp $body; $body.="\n<$tag>\n"; push @list_type,$tag; $list_level++; }else{ $body.="\n"; } my $item=$2; unless(m/^(.*)\|<$/) {$body.="
  • $item";} else { $body.="
  • $1"; while($list_level>$curlv){ $body.="
  • \n\n"; $list_level--; } $body.="\n"; } next; } } m/^>>$/ and $body.="
    \n" ,next; m/^<<$/ and $body.="
    \n" ,next; m/^>\|\|$/ and $in_pre=1,$body.="
    \n",next;
        m/^>\|$/        and $body.="
    \n"         ,next;
        m/^(.*)\|<$/    and $body.="$1
    \n" ,next; m/^\*\*\*(.*)$/ and $body.="
    $1
    \n" ,next; m/^\*\*(.*)$/ and $body.="

    $1

    \n" ,next; if(m/^====$/ && !$readmore){ $body.="
    \n\n"; $readmore=1; next; } if(m/^:(.*?):(.*)$/){ $body.="
    \n" unless($in_deflist); $in_deflist=1; $body.="
    $1
    \n
    $2
    \n"; next; } if(m/^(\|.*\|)$/){ my $text=$1; $body.="\n" unless($in_table); $in_table=1; while($text=~m/\|.+$/){ $text=~s/\|(.*?)\|/$text"; $text=~s/\|$/<\/tr>\n/; $body.=$text; next; } s/ \*/*/;s/ -/-/;s/ \+/+/; chomp; $body.="$_\n"; } while($list_level){ $body.="\n\n"; $list_level--; } #$body.="\n"; close DIARY; get_bid; login; my $category=""; $category.="" while($subject=~s/\[(.*?)\]//); my $published=""; if($readmore){ $body.="\n
    \n"; $post_content=~s//$subject/; my $res=post "POST"; debug $res,"\n"; $res =~ m!(.*?).*?.*?!; $published=$1; $body.="$readmore_string\n
    "; $google_post_url=$3; } for($post_content){ s//$subject/; s//$body/; s//$category/; #s//$published/; } debug post "POST";
    $1<\/td>|/ unless($text=~s/\|\*(.*?)\|/$1<\/th>|/); } $text="