Skip to content

Instantly share code, notes, and snippets.

@yinyin
Created May 17, 2010 16:15
Show Gist options
  • Save yinyin/403930 to your computer and use it in GitHub Desktop.
Save yinyin/403930 to your computer and use it in GitHub Desktop.
Perl script to post message to Plurk
#!/usr/bin/perl
# Usage: ./msgout-plurk.pl "Message going to Plurk"
#
# please make sure $MSGOUT_PLRK_CONFIG_FILE is modified to point to your configuration file.
# for configuration file example, please see "msgout-plurk.txt".
use LWP::UserAgent;
use URI::Escape;
use HTTP::Request::Common;
use POSIX qw(strftime);
$MSGOUT_PLRK_CONFIG_FILE = '/%PATH_TO_YOUR_CONFIG_HOME%/msgout-plurk.txt';
open CONF, "< ${MSGOUT_PLRK_CONFIG_FILE}" || die "ERR: Cannot open configuration file for read. ${MSGOUT_PLRK_CONFIG_FILE}";
%MSGOUT_PLRK_CONF = ();
%MSGOUT_PLRK_CONF_ORIG = ();
my $line = undef;
while($line = <CONF>) {
if($line =~ /^([A-Z]+)\s*=\s*(.+)$/)
{
my $cfgname = $1;
my $cfgvalue = $2;
$cfgvalue =~ s/^\s+//;
$cfgvalue =~ s/\s+$//;
if( ('APIKEY' eq $cfgname) or ('USER' eq $cfgname) or ('PASSWORD' eq $cfgname) )
{
$MSGOUT_PLRK_CONF{$cfgname} = uri_escape($cfgvalue);
$MSGOUT_PLRK_CONF_ORIG{$cfgname} = $cfgvalue;
}
elsif('COOKIE' eq $cfgname)
{
$MSGOUT_PLRK_CONF{$cfgname} = $cfgvalue;
}
}
}
close CONF;
my $msg_to_plrk = $ARGV[0];
$msg_to_plrk =~ s/^\s+//;
$msg_to_plrk =~ s/\s+$//;
$msg_to_plrk = uri_escape($msg_to_plrk);
my $ua = LWP::UserAgent->new;
my $res = undef;
my $max_retry = 3;
while($max_retry > 0) {
if(!defined($MSGOUT_PLRK_CONF{'COOKIE'}))
{
$res = $ua->request(GET "https://www.plurk.com/API/Users/login?api_key=${MSGOUT_PLRK_CONF{'APIKEY'}}&username=${MSGOUT_PLRK_CONF{'USER'}}&password=${MSGOUT_PLRK_CONF{'PASSWORD'}}");
if($res->is_success)
{
my $status_msg = $res->status_line;
my $resulted_cookie = $res->header('Set-Cookie');
my $aux;
($resulted_cookie, $aux) = split(/; /, $resulted_cookie, 2);
$MSGOUT_PLRK_CONF{'COOKIE'} = $resulted_cookie;
print "OK ${status_msg} ${resulted_cookie}\n";
}
else
{
my $status_msg = $res->status_line;
print "ERR ${status_msg}\n";
}
}
if(0 < length($MSGOUT_PLRK_CONF{'COOKIE'}))
{
my $req = HTTP::Request->new(GET => "http://www.plurk.com/API/Profile/getOwnProfile?api_key=${MSGOUT_PLRK_CONF{'APIKEY'}}");
$req->header('Cookie' => $MSGOUT_PLRK_CONF{'COOKIE'});
$res = $ua->simple_request($req);
if($res->is_success)
{
my $status_msg = $res->status_line;
my $resulted_cookie = $res->header('Set-Cookie');
my $aux;
($resulted_cookie, $aux) = split(/; /, $resulted_cookie, 2);
if(length($resulted_cookie) > 0)
{ $MSGOUT_PLRK_CONF{'COOKIE'} = $resulted_cookie; }
$max_retry = 0;
print "OK ${status_msg} ${resulted_cookie}\n";
}
else
{
my $status_msg = $res->status_line;
$MSGOUT_PLRK_CONF{'COOKIE'} = undef;
print "ERR ${status_msg}\n";
}
}
else
{
$MSGOUT_PLRK_CONF{'COOKIE'} = undef;
}
$max_retry--;
}
if(0 < length($MSGOUT_PLRK_CONF{'COOKIE'}))
{
my $posted_time = strftime('%Y-%m-%dT%H:%M:%S', gmtime());
my $req = HTTP::Request->new(GET => "http://www.plurk.com/API/Timeline/plurkAdd?api_key=${MSGOUT_PLRK_CONF{'APIKEY'}}&posted=${posted_time}&qualifier=%3A&content=${msg_to_plrk}&lang=tr_ch&no_comments=0");
$req->header('Cookie' => $MSGOUT_PLRK_CONF{'COOKIE'});
$res = $ua->simple_request($req);
if($res->is_success)
{
my $status_msg = $res->status_line;
my $resulted_cookie = $res->header('Set-Cookie');
my $aux;
($resulted_cookie, $aux) = split(/; /, $resulted_cookie, 2);
if(length($resulted_cookie) > 0)
{ $MSGOUT_PLRK_CONF{'COOKIE'} = $resulted_cookie; }
$max_retry = 0;
print "OK ${status_msg} ${resulted_cookie}\n";
}
else
{
my $status_msg = $res->status_line;
$MSGOUT_PLRK_CONF{'COOKIE'} = undef;
print "ERR ${status_msg}\n";
}
}
else
{
print "Empty authentication cookie.\n";
}
open CONF, "> ${MSGOUT_PLRK_CONFIG_FILE}" || die "ERR: Cannot open configuration file for write. ${MSGOUT_PLRK_CONFIG_FILE}";
print CONF "APIKEY = ${MSGOUT_PLRK_CONF_ORIG{'APIKEY'}}\n";
print CONF "USER = ${MSGOUT_PLRK_CONF_ORIG{'USER'}}\n";
print CONF "PASSWORD = ${MSGOUT_PLRK_CONF_ORIG{'PASSWORD'}}\n";
print CONF "COOKIE = ${MSGOUT_PLRK_CONF{'COOKIE'}}\n";
close CONF;
print "done.\n\n";
APIKEY = %YOUR_API_KEY%
USER = %YOUR_USERID%
PASSWORD = %YOUR_PASSWORD%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment