Skip to content

Instantly share code, notes, and snippets.

@gslin
Created March 31, 2011 19:40
Show Gist options
  • Save gslin/897085 to your computer and use it in GitHub Desktop.
Save gslin/897085 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use DBI;
use LWP::Simple;
use WWW::Mechanize;
use XML::RSS;
use encoding ':locale';
use utf8;
use constant APIKEY => 'xxx';
use constant PLURKUSER => 'user';
use constant PLURKPASS => 'pass';
use constant TWITTERUSER => 'user';
my $ua;
INIT {
$ua = WWW::Mechanize->new;
$ua->ssl_opts(SSL_ca_file => '/usr/local/share/certs/ca-root-nss.crt');
my $dbi = DBI->connect('dbi:SQLite:plurk.sqlite');
my $sth_query = $dbi->prepare('SELECT * FROM entry WHERE guid = ?;');
my $sth_insert = $dbi->prepare('INSERT INTO entry (guid, updated) VALUES (?, 0);');
my $rss = get sprintf 'http://twitter.com/statuses/user_timeline/%s.rss', TWITTERUSER;
say 'System: Got Twitter timeline.';
my $feed = XML::RSS->new;
$feed->parse($rss);
foreach my $entry (@{$feed->{items}}) {
my $guid = $entry->{guid};
my $link = $entry->{link};
my $body = $entry->{description} || $entry->{title};
$body .= " # $link";
$dbi->begin_work;
$sth_query->execute($guid);
my @a = $sth_query->fetchrow_array();
if (0 == scalar @a) {
if (pushToPlurk($body) < 0) {
$dbi->rollback;
next;
}
$sth_insert->execute($guid);
$dbi->commit;
} else {
$dbi->rollback;
}
}
}
sub loginPlurk {
my $uri = URI->new('https://www.plurk.com/API/Users/login');
$uri->query_form(api_key => APIKEY, username => PLURKUSER, password => PLURKPASS);
my $res = $ua->get($uri);
if ($res->code > 300) {
say 'Plurk: ', $res->content;
return -1;
}
return 0;
}
sub pushToPlurk {
my $body = shift;
state $loginedPlurk = 0;
if (0 == $loginedPlurk) {
return -1 if loginPlurk() < 0;
$loginedPlurk = 1;
}
my $patt = sprintf '^%s: ', TWITTERUSER;
$body =~ s/$patt//;
my $uri = URI->new('https://www.plurk.com/API/Timeline/plurkAdd');
$uri->query_form(api_key => APIKEY, content => $body, qualifier => 'says', lang => 'tr_ch');
my $res = $ua->get($uri);
if ($res->code >= 300) {
say $res->content;
return -1;
}
say "System: Posted to Plurk: $body";
return 0;
}
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment