Skip to content

Instantly share code, notes, and snippets.

@gslin
Last active August 10, 2017 03:49
Show Gist options
  • Save gslin/982195 to your computer and use it in GitHub Desktop.
Save gslin/982195 to your computer and use it in GitHub Desktop.
Plurk 新版 OAuth Core 1.0a 的 twitter to plurk
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use Data::UUID;
use DBI;
use HTTP::Request::Common;
use LWP::Simple;
use Net::OAuth;
use Web::Query;
use open ':std', ':locale';
use constant PLURK_ACCESS_TOKEN => 'xxx';
use constant PLURK_ACCESS_SECRET => 'xxx';
use constant PLURK_CONSUMER_KEY => 'xxx';
use constant PLURK_CONSUMER_SECRET => 'xxx';
use constant TWITTERUSER => 'gslin';
use vars qw{$ug};
BEGIN {
$Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
$ug = Data::UUID->new;
}
INIT {
my $dbi = DBI->connect('dbi:SQLite:gslin-plurk.sqlite');
my $sth_query = $dbi->prepare('SELECT * FROM entry WHERE url = ?;');
my $sth_insert = $dbi->prepare('INSERT INTO entry (url, updated_at) VALUES (?, ?);');
my $timeline = get sprintf 'https://twitter.com/%s', TWITTERUSER;
say 'System: Got Twitter timeline.';
my $dom = Web::Query->new($timeline);
$dom->find('li.stream-item')->each(sub {
my $pk = $_->find('a.tweet-timestamp')->first->attr('href');
my $link = 'https://twitter.com' . $pk;
my $body = $_->find('p.tweet-text')->first->text;
$body =~ s/\s*$//;
$body .= " # $link";
say $body;
$dbi->begin_work;
$sth_query->execute($pk);
my @a = $sth_query->fetchrow_array();
if (0 == scalar @a) {
if (pushToPlurk($body) < 0) {
$dbi->rollback;
return;
}
$sth_insert->execute($pk, time);
$dbi->commit;
} else {
$dbi->rollback;
}
});
}
sub pushToPlurk {
my $body = shift;
my $patt = sprintf '^%s: ', TWITTERUSER;
$body =~ s/$patt//;
my $req = Net::OAuth->request('protected resource')->new(
consumer_key => PLURK_CONSUMER_KEY,
consumer_secret => PLURK_CONSUMER_SECRET,
token => PLURK_ACCESS_TOKEN,
token_secret => PLURK_ACCESS_SECRET,
request_url => 'http://www.plurk.com/APP/Timeline/plurkAdd',
request_method => 'POST',
signature_method => 'HMAC-SHA1',
timestamp => time,
nonce => $ug->to_b64string($ug->create),
extra_params => {
content => $body,
qualifier => 'says',
lang => 'tr_ch',
},
);
$req->sign;
my $ua = LWP::UserAgent->new;
my $res = $ua->request(POST $req->to_url);
if (!$res->is_success) {
say $res->decoded_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