Created
April 22, 2011 01:05
-
-
Save benvanstaveren/935811 to your computer and use it in GitHub Desktop.
A little example for using Mojolicious::Lite, Mojolicious::Plugin::Disqus and Net::Disqus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
use Try::Tiny; | |
plugin 'disqus', { 'api_secret' => 'getyourownsecretplease', pass_api_errors => 1 }; | |
# This, of course, is total and utter overkill for something like this, | |
# but I figured if I'm going to write demonstration code, might as well | |
# go the whole hog. | |
plugin 'mongodb', { 'database' => 'disqusbridge' }; | |
sub update_thread { | |
my $self = shift; | |
my $id = shift; | |
my $ok = 1; | |
my $res; | |
try { | |
$res = $self->disqus->threads->details(forum => 'thisisntcomedyitsreality', thread => sprintf('ident:%s', $id)); | |
} catch { | |
$ok = 0; | |
}; | |
if($res->{code} == 0 && $ok) { | |
my $upd = { updated => time() }; | |
$upd->{$_} = $res->{response}->{$_} || 0 for(qw(posts reactions likes dislikes)); | |
$upd->{update} = time(); | |
$self->coll('thread')->update({ _id => $id }, { '$set' => $upd }); | |
} | |
} | |
# This is sneaky. | |
sub handle_thread { | |
my $self = shift; | |
my $id = shift; | |
# let's assume for the sake of fun(tm) that we are going to | |
# bunk the thread view here | |
$self->coll('thread')->update({ _id => $id }, { '$inc' => { 'views' => 1 } }, { upsert => 1 }); | |
my $tobj = $self->coll('thread')->find_one({ _id => $id }); | |
if($tobj->{updated} + 300 < time()) { | |
update_thread($id); | |
$tobj = $self->coll('thread')->find_one({ _id => $id }); | |
} | |
for(qw(posts views likes dislikes reactions)) { | |
$tobj->{$_} ||= 0; | |
} | |
return $tobj; | |
} | |
get '/thread/:threadid/counts' => sub { | |
my $self = shift; | |
my $id = $self->stash('threadid'); | |
my $json = {}; | |
my @id = split(/,/, $id); | |
for(@id) { | |
my $t = handle_thread($self, $_); | |
if($t) { | |
$json->{$_} = $t; | |
} | |
} | |
$self->res->headers->header('Access-Control-Allow-Origin' => 'http://xirinet.com'); | |
$self->res->headers->header('Pragma' => 'no-cache'); | |
$self->res->headers->header('Cache-Control' => 'no-cache'); | |
$self->render(json => $json); | |
}; | |
get '/kicker' => sub { | |
my $self = shift; | |
$self->res->headers->header('Access-Control-Allow-Origin' => 'http://xirinet.com'); | |
$self->res->headers->header('Pragma' => 'no-cache'); | |
$self->res->headers->header('Cache-Control' => 'no-cache'); | |
$self->render(template => 'kicker'); | |
}; | |
app->start; | |
__DATA__ | |
@@ kicker.js.ep | |
var disqusBridge = 'http://disq.xirinet.com'; | |
var needCounts = new Array(); | |
function db_getNeeded() { | |
$('div.hentry').each(function(i, e) { | |
var id = $(e).attr('disqus-identifier'); | |
if(id) needCounts.push(id); | |
}); | |
} | |
function db_fetchCounts() { | |
$.getJSON(disqusBridge + '/thread/' + needCounts.join(',') + '/counts', {}, function(data, textStatus) { | |
for(i in data) { | |
$('span.db-item.db-' + i + '.posts').html(data[i].posts); | |
$('span.db-item.db-' + i + '.views').html(data[i].views); | |
$('span.db-item.db-' + i + '.likes').html(data[i].likes); | |
$('span.db-item.db-' + i + '.dislikes').html(data[i].dislikes); | |
$('span.db-item.db-' + i + '.reactions').html(data[i].reactions); | |
} | |
}); | |
} | |
$(document).ready(function() { | |
db_getNeeded(); | |
db_fetchCounts(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment