#!/usr/bin/perl

# 日本語EUC、LF
use XML::RSS;
use LWP::Simple;
use WWW::Mechanize;
use Unicode::Japanese qw(unijp);
use File::Basename;

my $PURL = 'http://purl.org/rss/1.0/modules/content/';
my $LOCK_DIR = basename( $0 ) . '.lock';
die
	if ( !mkdir( $LOCK_DIR ) );

my $url = '転載元のRSS';
my $mixiAccount = 'メールアドレス';
my $mixiPassword = 'パスワード';

sub SafeFilename {
	my $str = shift @_;
	$str =~ s/([^0-9a-zA-Z_.-])/'_' . unpack('H2', $1)/eg;
	return( $str );
}
my $storeDir = $url;
$storeDir =~ s#http://##;
$storeDir = SafeFilename( $storeDir );
mkdir( $storeDir )
	if ( ! -d $storeDir );

eval {
	my $rss = get( $url )
		or die;

	my $oParser = new XML::RSS;
	$oParser->parse( $rss );
	my $rArrayItems = $oParser->{items};
	foreach my $item ( reverse @{ $rArrayItems } ) {
	# 新着順→旧着順に。sortなんかするか！
		my $url = $item->{link};
		my $filename = $url;
		$filename =~ s#http://[^/]+/##;
		$filename = SafeFilename( $filename );
		next
			if ( -f "$storeDir/$filename" );
		my $eucTitle =
			unijp(
				'[' . $item->{dc}->{subject} . '] ' .
				$item->{title}
			)->euc;
		my $eucContent =
			Html2EucPlain( $item->{$PURL}->{encoded} );
		$eucContent .= "\n\n-- $url";
		open( OUT, ">$storeDir/$filename" )
			or die "Cannot write to log";
		print OUT $eucTitle . "\n";
		print OUT $eucContent . "\n";
		close( OUT );
		MixiPost(
			$mixiAccount,
			$mixiPassword,
			$eucTitle,
			$eucContent,
		) or
			do {
				unlink( "$storeDir/$filename" );
				die "Cannot post to mixi";
			}
		;
	}
};
warn $@
	if ( $@ );
rmdir( $LOCK_DIR );

sub Html2EucPlain {
	my $str = shift @_;
	local $_ = unijp( $str )->euc;
	s/\x0D\x0A|\x0D|\x0A/\n/g;
	s#<p>#\n#ig;
	s#<br>#\n#ig;
	s#<br />#\n#ig;
	# 画像はURLのみ（相対リンク？　知るか！）
	s#<img src="([^"]+)"[^>]*>#$1\n#ig;
	s#<embed src="([^"]+)"[^>]*>#$1\n#ig;
	s#</div>#\n#ig;
	s#<blockquote>#…#ig;
	s#</blockquote>#…#ig;
	# あとのタグは消す。複数行にわたるタグは？
	s#<[^>]*>##gs;
#	s#\&amp;#&#g;
#	s#\&gt;#>#g;
#	s#\&lt;#<#g;
	s#\t#    #g;
	s#  #　#g;
	return( $_ );
}

sub MixiPost {
	my(
		$mixiAccount,
		$mixiPassword,
		$eucTitle,
		$eucContent
	) = @_;

	my $mech = WWW::Mechanize->new( autocheck => 1 );
	$mech->agent_alias( 'Windows IE 6' );

	# 初期画面
	$mech->get( 'http://mixi.jp/' )
		or return undef;
		# 取得できないとdieされる(autocheck)

	# ログイン
	$mech->submit_form(
		fields =>
			{
				'email' =>		$mixiAccount,
				'password' =>	$mixiPassword,
			}
	) or return undef;

	while (
		$mech->response()->header('refresh') =~ /url=([^\s;]+)/i
	) {
		$mech->get( $1 )
			or return undef;
	}

	# 投稿用ページ
	$mech->follow_link(
		url_regex => qr/\/add_diary\.pl/
	) or return undef;

	# 投稿
	$mech->submit_form(
		form_name => 'diary',
		fields =>
			{
				'diary_title' =>	$eucTitle,
				'diary_body' =>		$eucContent,
			}
	) or return undef;

	# 確認
	$mech->submit_form(
		form_number => 1,
	) or return undef;
}
