Zotero Miniserver

Here is a proof of principle project that can be further adjusted to your own needs – a simple server script that can look up your most recent Zotero database entries and display them to the world. When run in a DOS or Linux box as ZoteroServer path/zotero.sqlite it will display the most recent entries to clients listening at localhost:8888. The code should be pretty straightforward and may be replaced with even other items.

ZoteroServer.pl

 1:
 2:
 3:
 4:
 5:
 6:
 7:
 8:
 9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
82:
# ZoteroMiniServer: show recent Zotero entries
# m@wjst.de 27/1/08
#----------------------------------------------------------------------------------------------
#perl2exe_include "DBI.pm"
#perl2exe_include "DBD/SQLite.pm"
#perl2exe_include "CGI.pm"
#perl2exe_include "IO.pm"
my $dbfile = <$ARGV[0]>;
#----------------------------------------------------------------------------------------------
use DBI;
use CGI;
$dbh = DBI->connect(
"dbi:SQLite:$dbfile",
"",    # no user
"",    # no password
{ RaiseError => 1 },    
) || die "Cannot connect: $DBI::errstr";

my $sql = 'SELECT DISTINCT items.itemID
FROM items
ORDER BY items.dateAdded DESC
LIMIT 50';
$res0 = $dbh->selectall_arrayref($sql);
my $idlist="";
for $i ( 0 .. $#{$res0} ) {
    for $ii ( 0 .. $#{$res0->[$i]} ) {
        # print "$i:$ii:$res0->[$i][$ii]  ";
    }
    if ($i >0 ) {
        $idlist .= ',' . $res0->[$i][0];
    }
    else {
        $idlist = $res0->[0][0];
    }
}
my $itemlist="";
my $sql = 'SELECT DISTINCT itemDataValues.value
FROM items LEFT JOIN itemData ON items.itemID = itemData.itemID LEFT JOIN itemDataValues ON itemData.valueID = itemDataValues.valueID
WHERE items.itemID IN (' . $idlist . ') AND itemData.fieldID=110';
$res1 = $dbh->selectall_arrayref($sql);
my $itemlist1="";
my $itemlist2="";
my $tagA = '<span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&amp;rfr_id=info%3Asid%2Focoins.info%3Agenerator&amp;rft.genre=article&amp;rft.atitle=';
my $tagB = '"></span></br>';
for $i ( 0 .. $#{$res1} ) {
    $itemlist1 .= $tagA . CGI::escape($res1->[$i][0]) . $tagB;
    $itemlist2 .= '<li>' . $res1->[$i][0] . '</li>';
}
#----------------------------------------------------------------------------------------------
use IO::Socket;
my $myport=8888;
my $server=IO::Socket::INET->new(LocalPort=>$myport,
    Type=>SOCK_STREAM,
    Reuse=>1,
    Listen=>10);
my $client;
my $client_adress;
my $data;
print "Listening on port $myport ... \n";
while (  ($client,$client_adress) = $server->accept() ) { 
    my( $client_port,$client_iaddr) = sockaddr_in($client_adress);
    my $client_dot_ip = inet_ntoa($client_iaddr);
    my $client_name   = gethostbyaddr($client_iaddr,AF_INET);
    print "Accepting client $client_dot_ip [$client_name] port: $client_port\n";
    $client->recv($data,10000);
    # print "\nReceived from client:\n$data\n" ;
    my $site= "<html>\n";
    $site .= "<head>\n";
    $site .= "<title>ZoteroServer</title>\n";
    $site .= "</head> \n";
    $site .= "<body>\n";
    $site .= "<b>ZoteroMiniserver</b><BR/>\n";
    $site .= "just click the icon in URL bar above to import any of the following references<BR/>\n";
    $site .= '<ol>' . $itemlist2 . '</ol>';
    $site .= $itemlist1;
    $site .= "</body>\n";
    $site .= "\n</html>\n ";
    print $client $site;
    close($client);
}
close $server; 

Nachtrag

Comments are closed.