#!/usr/bin/perl -w use strict; use LWP::UserAgent; use HTTP::Request::Common; use Getopt::Long; use IO::File; my $DEFAULT_SERVER = "www.livejournal.com"; my $LOG_PATH = "/cgi-bin/log.cgi"; my $MAINTAINER = "jaffray\@pobox.com"; my $VERSION = "0.9"; my ($username, $password, $keywords, $default, $filename, $server, $verbose); sub help { die <<"EOF"; Uploads the user picture IMAGE to the specified LiveJournal server. Reads password from stdin if it is not specified on the command line. -u, --username=USER upload as user USER; optionally set server if USER is of the form "user\@foo.com" -p, --password=PASS log in using password PASS; otherwise prompt -k, --keywords=KEY1,KEY2,... associate keywords KEY1, KEY2, ... with image -d, --default make this the default picture for USER -s, --server=SERVER connect to SERVER instead of $DEFAULT_SERVER -v, --verbose operate verbosely, give full HTML output -h, --help this message This is $0 $VERSION. Report bugs to <$MAINTAINER>. EOF } sub usage { die "usage: $0 [OPTIONS...] USER[\@SERVER] IMAGE\n"; } sub get_options { GetOptions( "username|u=s" => \$username, "password|p=s" => \$password, "keywords|keyword|k=s" => \$keywords, "default|d" => \$default, "server|s=s" => \$server, "verbose|v" => \$verbose, "help|h" => \&help, ) or usage(); $username ||= shift @ARGV; $filename ||= shift @ARGV; if ($username and $username =~ m(\@)) { ($username, $server) = split qr(\@), $username; } $server ||= $DEFAULT_SERVER; } sub read_password { my $pw; if (-t STDIN) { print "Password: "; system "stty -echo"; chomp($pw = ); system "stty echo"; print "\n"; } else { chomp($pw = ); } return $pw; } sub upload_pic { undef $/; my $image_file = new IO::File($filename) or die "Couldn't open $filename: $!\n"; my $image_data = $image_file->getline; length $image_data <= 40960 or die "$filename exceeds maximum file size of 40K."; $/ = "\n"; my @post_data = (); push @post_data, mode => "uploadpic"; push @post_data, user => $username; push @post_data, password => $password; push @post_data, data => $image_data; push @post_data, default => 1 if defined $default; push @post_data, keywords => $keywords if defined $keywords; my $ua = new LWP::UserAgent; my $response = $ua->request(POST("http://$server$LOG_PATH", Content => \@post_data)); # LJ returns 200 unless the server is having trouble. $response->is_success or return (0, $verbose ? $response->error_as_HTML : "Server error, try again later"); # Server output is of the form (variable\nvalue\n)* my %output = split /\n/, $response->content; $output{success} eq "OK" or return (0, $verbose ? $response->content : "Error uploading picture: $output{errmsg}"); return (1, $verbose ? $response->content : "Picture successfully uploaded."); } get_options(); $username and $server and $filename and not @ARGV or usage(); $password ||= read_password() or usage(); my ($result, $output) = upload_pic(); print "$output\n"; exit !$result;