In this tutorial I'll show you how to post new entry to LiveJournal with perl.
It's quite simple: we should open a socket and create HTTP request to send it to a LiveJournal server. First of all, we'll get current date and time:
($sec, $Gmin, $Ghour, $mday, $Gmon,
$Gyear, $wday, $yday, $isdst) = localtime;
$Gyear += 1900;
$Gmon += 1;
Then define all variables:
use IO::Socket qw(:DEFAULT :crlf);
use CGI;
$c = new CGI;
$year = $c->param("year");
$mon = $c->param("mon");
$day = $c->param("day");
$subject = $c->param("subject");
$hour = $c->param("hour");
$min = $c->param("min");
$event = $c->param("event");
$act = $c->param("act");
$post="mode=postevent&user=USER&auth_method=clear";
$post.="&auth_response=&lineendings=pc";
$post.="&allowmask=&auth_challenge=&password=PASSWORD";
$post.="&year=$year&mon=$mon&day=$day&hour=$hour&min=$min";
$post.="&subject=$subject&event=$event";
Change USER and PASSWORD for your LiveJournal username and password. Variables $subject and $event must contain subject of an entry and entry text correspondingly. Read this for more information about arguments of such HTTP request.
Note that agrument password is plain-text, so, if you worry about security, use alternative argument hpassword to send yor password as an MD5 hex digest.
Also if text of your subject and entry not in UTF-8 codepage, you should encode it with something like this:
use Encode;
Encode::from_to($subject, "cp1251", "utf8");
Encode::from_to($event, "cp1251", "utf8");
and add to variable $post (wich contains HTTP request) argument &ver=1.
To post in community add to variable $post argument &usejournal=CMNT with the name of community you want to post instead CMNT.
And now we're ready to make the HTTP request to LiveJournal server. Take a look at the example:
if ($act eq "post")
{
# open socket
my $sock = IO::Socket::INET->new(
PeerAddr => 'www.livejournal.com',
PeerPort => '80',
Proto => 'tcp'
);
# get the length of request
$l = length($post);
# print in socket request
print $sock join("\r\n" =>
"POST /interface/flat HTTP/1.0",
"Host: www.livejournal.com",
"Content-type: application/x-www-form-urlencoded",
"Content-length: $l",
"",
$post
);
# get answer from server
print <$sock>;
close($sock);
}
That's all! To know more, read the Flat Client/Server Protocol Reference on livejournal.com.
|