Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

ljer.pl - command-line LiveJournal client

by Amoe (Friar)
on Oct 20, 2001 at 15:43 UTC ( #120236=sourcecode: print w/ replies, xml ) Need Help??

Category: Web Stuff
Author/Contact Info Amoe - subvert_you(at)hotmail(dot)com
Description: Very simple LiveJournal console client. You need to make a file called 'ljer.rc' in the same dir as the script, containing the following lines: user: my_username password: my_password No command line options required. Yes, I am aware that there are other Perl LJ clients. The problem was, one was appallingly-coded (lots of manually parsed command-line options, hundreds of warnings, no strict) and the other was using Socket (which I dislike - and not even IO::Socket) when LWP is practically built for the task, and it was an 800-line behemoth. This is just a quick script for the impatient. Feel free to cuss as you feel appropriate.
#!/usr/bin/perl
use strict;
use warnings;

use LWP::UserAgent;
use HTTP::Request::Common;

my $agent = 'Amoe\'s LiveJournaller/0.01';
my $cgi   = 'http://www.livejournal.com/cgi-bin/log.cgi';
my $journaller = LWP::UserAgent->new();
$journaller->agent($agent);
my %global_params;

my $rc_file = 'ljer.rc';
open(RC, "<$rc_file") or die("couldn't open $rc_file: $!");
while (<RC>)
    {
    chomp;
    my ($key, $value) = split /\s*:\s*/, $_, 2;
    $global_params{$key} = $value;
    }
close(RC);

my %login = &login;

my $http_res = $journaller->request(POST $cgi, \%login);
die "Failed to login" unless ($http_res->is_success);
my %lj_res = split("\n", $http_res->content);
$lj_res{success} eq 'OK' ? print "logged in as $lj_res{name}\n" : die 
+"failed to log in: $lj_res{errmsg}\n";
print "server says: $lj_res{message}\n" unless (!$lj_res{message});

my %postevent = &postevent;

$http_res = $journaller->request(POST $cgi, \%postevent);
die "Failed to update journal" unless ($http_res->is_success);
%lj_res = split("\n", $http_res->content);
$lj_res{success} eq 'OK' ? print "updated journal successfully with en
+try $lj_res{itemid}\n" : print "failed to update journal: $lj_res{err
+msg}\n";
exit;

sub login
    {
    my %params = %global_params;
    $params{mode}          = 'login';
    $params{clientversion} = $agent;
    return %params;
    }

sub postevent
    {
    my %params = %global_params;
    $params{mode}        = 'postevent';
    $params{lineendings} = 'unix';
    $params{security}    = 'usemask';        # for public or private e
+ntry, change this to 'public' or 'private' and remove '$params{allowm
+ask} = 1' 
    $params{allowmask}   = 1;
    @params{'subject', 'event', 'prop_current_music', 'prop_current_mo
+od'} = read_text();
    @params{'min', 'hour', 'day', 'mon', 'year'} = (localtime(time))[1
+..5];
    $params{year} += 1900;
    $params{mon}  += 1;
    return %params;
    }

sub read_text
    {
    print "please enter your subject line:\n";
    chomp(my $subject = <STDIN>);
    my $text;
    print "please enter your journal entry (terminated by a period on 
+a single line):\n";
        {
        local $/;
        $/ = "\n.\n";
        chomp($text = <STDIN>);
        }
    print "please enter your current music: ";
    chomp(my $music = <STDIN>);
    print "please enter your current mood: ";
    chomp(my $mood  = <STDIN>);

    return $subject, $text, $music, $mood;
    }

Comment on ljer.pl - command-line LiveJournal client
Download Code
Re: ljer.pl - command-line LiveJournal client
by Anonymous Monk on May 25, 2007 at 12:26 UTC
    hi. Thanks for a nice program. I slightly modified it to support a proxy and send posts in utf-8 + windows russian console encoding (cp866). Right now it works well for me and I able to post my notes to LJ from win32 cmd in russian. that is my version. sorry, I did not extract my settings in rc file, but it is easy to do for everybody.
    #!/usr/bin/perl use strict; use warnings; use Encode; use LWP::UserAgent; use HTTP::Request::Common; my $agent = 'Amoe\'s LiveJournaller/0.01'; my $cgi = 'http://www.livejournal.com/cgi-bin/log.cgi'; my $journaller = LWP::UserAgent->new(); $journaller->agent($agent); # set up a proxy $journaller->proxy(['http'], 'http://127.0.0.1:8118'); my %global_params; my $rc_file = 'ljer.rc'; open(RC, "<$rc_file") or die("couldn't open $rc_file: $!"); while (<RC>) { chomp; my ($key, $value) = split /\s*:\s*/, $_, 2; $global_params{$key} = $value; } close(RC); my %login = &login; my $http_res = $journaller->request(POST $cgi, \%login); die "Failed to login" unless ($http_res->is_success); my %lj_res = split("\n", $http_res->content); $lj_res{success} eq 'OK' ? print "logged in as $lj_res{name}\n" : die +"failed to log in: $lj_res{errmsg}\n"; print "server says: $lj_res{message}\n" unless (!$lj_res{message}); my %postevent = &postevent; $http_res = $journaller->request(POST $cgi, \%postevent); die "Failed to update journal" unless ($http_res->is_success); %lj_res = split("\n", $http_res->content); $lj_res{success} eq 'OK' ? print "updated journal successfully with en +try $lj_res{itemid}\n" : print "failed to update journal: $lj_res{err +msg}\n"; exit; sub login { my %params = %global_params; $params{mode} = 'login'; $params{clientversion} = $agent; return %params; } sub postevent { my %params = %global_params; $params{mode} = 'postevent'; # $params{lineendings} = 'unix'; $params{lineendings} = 'pc'; $params{ver} = 1; # $params{security} = 'usemask'; # for public or private +entry, change this to 'public' or 'private' and remove '$params{allow +mask} = 1' # $params{allowmask} = 1; $params{security} = 'public'; @params{'subject', 'event', 'prop_current_music', 'prop_current_mo +od'} = read_text(); @params{'min', 'hour', 'day', 'mon', 'year'} = (localtime(time))[1 +..5]; $params{year} += 1900; $params{mon} += 1; # from internal representation to utf-8 $params{'subject'} = Encode::encode_utf8($params{'subject'}); $params{'event'} = Encode::encode_utf8($params{'event'}); $params{'prop_current_music'} = Encode::encode_utf8($params{'prop_ +current_music'}); $params{'prop_current_mood'} = Encode::encode_utf8($params{'prop_c +urrent_mood'}); return %params; } sub read_text { print "please enter your subject line:\n"; chomp(my $subject = <STDIN>); # from system representation to internal $subject = Encode::decode('cp866', $subject); my $text; print "please enter your journal entry (terminated by a period on +a single line):\n"; { local $/; $/ = "\n.\n"; chomp($text = <STDIN>); $text = Encode::decode('cp866', $text); } print "please enter your current music: "; chomp(my $music = <STDIN>); $music = Encode::decode('cp866', $music); print "please enter your current mood: "; chomp(my $mood = <STDIN>); $mood = Encode::decode('cp866', $mood); return $subject, $text, $music, $mood; }

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://120236]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-12-25 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls