Skip to content
This repository has been archived by the owner on Feb 7, 2018. It is now read-only.

Commit

Permalink
use authenticated graph api if available
Browse files Browse the repository at this point in the history
  • Loading branch information
compeak committed Mar 19, 2015
1 parent 8403c57 commit 78cefc6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
6 changes: 6 additions & 0 deletions heise-shariff.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
expires => 60,
},
domain => qr{\A www\.heise\.de \z}xms,
services => {
facebook => {
# app_id => 1234,
# secret => 'abcd123',
},
},
}
2 changes: 1 addition & 1 deletion lib/Heise/Shariff.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ sub startup {
for my $module (@{$loader->search($ns)}) {
my $e = $loader->load($module);
warn qq{Loading "$module" failed: $e} and next if ref $e;
push @services, $module->new;
push @services, $module->new(app => $self->app);
}
}
return \@services;
Expand Down
2 changes: 2 additions & 0 deletions lib/Heise/Shariff/Service.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use Mojo::Base -base;

use Mojo::URL;

has 'app';

has url => sub {};

sub request {
Expand Down
28 changes: 19 additions & 9 deletions lib/Heise/Shariff/Service/Facebook.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,33 @@ use Mojo::Util qw(dumper);
sub request {
my ($self, $url) = @_;

my $furl = Mojo::URL->new('https://api.facebook.com/method/fql.query');
$furl->query->param(format => 'json');
my $query = qq{select share_count from link_stat where url="$url"};
$furl->query->param(query => $query);

my $app_id = $self->app->config->{services}->{facebook}->{app_id};
my $secret = $self->app->config->{services}->{facebook}->{secret};

my $furl = q();
if ($app_id && $secret) {
$furl = Mojo::URL->new('https://graph.facebook.com/v2.2/');
$furl->query->param(access_token => $app_id.'|'.$secret);
$furl->query->param(id => $url);
} else {
$furl = Mojo::URL->new('https://api.facebook.com/method/fql.query');
$furl->query->param(format => 'json');
my $query = qq{select share_count from link_stat where url="$url"};
$furl->query->param(query => $query);
}
return [get => $furl];
}

sub extract_count {
my ($self, $res) = @_;

if (ref $res->json eq 'ARRAY') {
if (ref $res->json eq 'ARRAY' && exists $res->json->[0]->{share_count}) {
return $res->json->[0]->{share_count};
} else {
warn dumper($res);
return undef;
}
if (exists $res->json->{share} && $res->json->{share}->{share_count}) {
return $res->json->{share}->{share_count};
}
return undef;
}

sub get_name {
Expand Down

0 comments on commit 78cefc6

Please sign in to comment.