Hi,

I recently wrote on this list that I’m about to develop a script that posts 
recently catalogued books to a Facebook page. Well, I found some time faster 
then I thought, so here’s my simple script. I’ll run it using koha-foreach in a 
cronjob.

It does nothing fancy it just posts a list of recently catalogued books in the 
form 

author: title
http://yourlibrary.org/bib/number

in the time line of a Facebook page. I also tried uploading cover images, but 
that didn’t work that well.

Actually, the whole script is rather trivial. Figuring out how to get access 
tokens for Facebook having the correct permissions took much longer then 
developing the script. So, I tried to summarize the steps necessary to get the 
tokens in the script’s comments. I hope they are helpful.

Cheers,
  Stephan

#!/usr/bin/perl

use utf8;
use Modern::Perl;
use C4::Context;
use Facebook::Graph;

# This program posts a list of recently catalogued books to Facebook "Page". 
# To be able to get the required access tokens you have to have administrator
# rights for the page.
#
# 1. Register as developer on Facebook's developer site: 
#    https://developers.facebook.com
# 2. Add an app on Facebook's developer site. Use "advanced Setup", choose a 
#    name and the category "Apps for pages". You'll get an app id and an app 
#    secret
# 3. get an user access token on https://developers.facebook.com/tools/explorer 
#    by clicking "Get Access Token", set the "Extended Permissions" 
manage_pages and publish_actions (make sure that you do that for your app and 
not for Graph Explorer)
# 4. extend the expiration date of the access token by submitting the following
#    GET request: 
#    
/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={short-lived-token}
 
#    You can this using wget:
#    wget -O token "https://graph.facebook.com/wget -O token 
"https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={short-lived-token}
 
#    the file token will then contain your token
# 5. determine your user id e.g. by http://graph.facebook.com/yourusername
# 6. get the page access token by submitting the following GET request in 
#    Graph API explorer:
#      /{user-id}/accounts
#    You will get a data structure containing a number of key
#    value pairs for each page you administrate. Get the page id and page 
#    access token for the page you want to post to


my $fb_access_token = „"; # your page access token goes here

my $maxdays = 7;
my $base_url = 'http://yourlibrary.org';


my $fb = Facebook::Graph->new;
$fb->access_token($fb_access_token);

my $tmpdir = File::Temp->newdir();
my $dirname = $tmpdir->dirname;

my $dbh   = C4::Context->dbh;

my $query = "SELECT b.biblionumber,b.author,b.title,i.itemcallnumber FROM 
biblio b, items i WHERE b.biblionumber = i.biblionumber AND 
DATEDIFF(CURRENT_DATE,b.datecreated) < ? GROUP BY 
b.biblionumber,b.author,b.title,i.itemcallnumber ORDER BY 
i.itemcallnumber,b.author COLLATE utf8_unicode_ci";

my $sth   = $dbh->prepare($query);
$sth->execute($maxdays);

my @data;
my $message = "Recently catalogued books\n\n";
my $rows_returned = 0;

while (@data = $sth->fetchrow_array()) {
  my $biblionumber   = $data[0];
  my $author         = $data[1];
  my $title          = $data[2];
  my $itemcallnumber = $data[3];
   
  if ($author) {
    $message .= $author.": ";
  }
  $message .= $title."\n";
  $message .= $base_url.'/bib/.$biblionumber."\n\n";
  
  $rows_returned++;
}

if ($rows_returned) {
  my $response = $fb->add_post
    ->set_message($message)
    ->publish;

  # print $response->as_string()."\n";
  # my $msg_id = $response->as_hashref()->{id};
}

_______________________________________________
Koha mailing list  http://koha-community.org
Koha@lists.katipo.co.nz
http://lists.katipo.co.nz/mailman/listinfo/koha

Reply via email to