Skip to content

Instantly share code, notes, and snippets.

@tyru
Created March 4, 2009 07:31
Show Gist options
  • Select an option

  • Save tyru/73758 to your computer and use it in GitHub Desktop.

Select an option

Save tyru/73758 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Getopt::Long;
use Perl6::Say;
use Pod::Usage;
use DateTime;
use Digest::SHA1 qw( sha1 );
use HTTP::Request;
use MIME::Base64 qw( encode_base64 );
use LWP::UserAgent;
use Term::ReadPassword;
my ($username, $password, $needhelp, $filepath, $mode);
GetOptions(
'user=s' => \$username,
'pass=s' => \$password,
help => \$needhelp,
'output=s' => \$filepath,
'mode=s' => \$mode,
) or pod2usage( -verbose => 1 );
pod2usage( -verbose => 1 ) if $needhelp;
# set default
unless( defined $username ) {
print "put your hatena username:";
chomp( $username = <STDIN> );
}
until( defined $password ) {
$password = read_password( "put your password:" );
}
unless( defined $filepath ) {
$filepath = "./hatebu.txt";
}
if( defined $mode ) {
if( $mode !~ /(rss|bookmarks)?/ ) {
say "use default";
$mode = "bookmarks";
}
} else {
$mode = "bookmarks";
}
# wsse authentication
my $nonce = sha1( sha1( time() . {} . rand() . $$ ) );
my $now = DateTime->now->iso8601 . 'Z';
my $digest = encode_base64( sha1( $nonce . $now . $password || '' ), '' );
my $credentials =
sprintf( qq(UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"),
$username, $digest, encode_base64( $nonce, '' ), $now );
my $url = 'http://b.hatena.ne.jp/dump' . ( $mode eq "" ? "" : "?mode=$mode" );
my $req = HTTP::Request->new( GET => $url );
$req->header( Accept => 'application/x.atom+xml, application/xml, text/xml, */*' );
$req->header( 'X-WSSE' => $credentials );
# saving data
my $ua = LWP::UserAgent->new;
$ua->show_progress( 1 );
my $res = $ua->request( $req );
unless( $res->is_success ) {
die $res->status_line . "\n";
}
my $FH = FileHandle->new( "> $filepath" );
unless( defined $FH ) {
die "can't open '$filepath'.\n";
}
print $FH $res->content;
$FH->close;
say "Done.";
__END__
=head1 NAME
hatebu-backup - backup hatena bookmarks
=head1 SYNOPSIS
hatebu-backup [-u user] [-p password] [-o output-file] [-m mode] [-h]
=head1 OPTIONS
=over 5
=item -u, --user
specify hatena username.
=item -p, --pass
specify hatena password.
=item -h, --help
show help.
=item -o, --output
specify output file path.
=item -m, --mode
specify hatena bookmark file mode.(r[ss], b[ookmarks] are allowed)
=back
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment