#!/usr/bin/perl -w
# ps2011.cgi: show results of election 2011 demo
# usage: ps2011.cgi
# 20110227 erikt(at)xs4all.nl

BEGIN { open(STDERR, ">>/WWW/t/tjongkim/tmp/errorlog"); }

$baseDir = "/WWW/t/tjongkim/htdocs/twitter";
$tableFile = "$baseDir/table.html";
($min,$sec) = (0,0);
if (open(INFILE,"date '+%M:%S'|")) {
   $line = <INFILE>;
   chomp($line);
   close(INFILE);
   ($min,$sec) = split(/:/,$line);
}
@parties = qw(cda christenunie d66 groenlinks pvda pvdd pvv sgp sp vvd 50plus);
#@articles = ("het","de","","","de","de","de","de","de","de","","","");
my %fancy = qw(cda CDA vvd VVD pvda PvdA sp SP christenunie ChristenUnie groenlinks GroenLinks sgp SGP d66 D66 pvdd PvdD overige overige pvv PVV 50plus 50Plus);
$command = $0;
$url = "http://ifarm.nl/twitter/cgi-bin/$command";

# read arguments
$line = <STDIN>;
if (not defined $line) { $line = ""; }
if (defined $ENV{"QUERY_STRING"}) { $line .= '&' . $ENV{"QUERY_STRING"}; }

chomp($line);
@args = split(/\&+/,$line);
%args = ();
for ($i=0;$i<=$#args;$i++) {
   ($field,$value) = split(/=+/,$args[$i]);
   if (not defined $field or $field eq "") { next; }
   if (not defined $value) { $value = 0; }
   $args{$field} = $value;
   $args{$field} =~ s/\++$//; # remove trailing spaces
   $args{$field} =~ s/^\++$//; # remove initial spaces
}
$party = $#parties;
# get previously shown party id
if (defined $args{"PARTY"}) { $party = $args{"PARTY"}; }
# compute next party
$party++;
if ($party > $#parties) { $party = 0; }
$partyName = $parties[$party];

$tableWidth = 260;
$tableHeight = 130;
$tdWidth = $tableWidth+10;
$tdHeight = $tableHeight+20;
$graphs = "";
foreach $p qw(cda christenunie d66 groenlinks) {
   $graphs .= "<tr><td align=\"center\" width=\"$tdWidth\" height=\"$tdHeight\"><strong><a href=\"/twitter/demo.$partyName,$p.50.svg\" style=\"color:black; text-decoration:none;\">";
   $graphs .= "<font color=\"red\">$fancy{$partyName}</font>";
   if ($p ne $partyName) {
      $graphs .= " vs <font color=\"blue\">$fancy{$p}</font>";
   }
   $graphs .= "</a></strong>\n<br><embed src=\"/twitter/demo.$partyName,$p.50.svg\" type=\"image/svg+xml\" width=\"$tableWidth\" height=\"$tableHeight\" /></td></tr>";
}

# standard lines for output
print "Content-type: text/html\n\n";

print <<THEEND;
<html>
<head>
<title>Informatiekunde voorspelt de Statenverkiezingen via Twitter</title>
<META HTTP-EQUIV="refresh" CONTENT="24; URL=$url?PARTY=$party">
</head>
<body style="font-family:verdana,sans-serif;">
<center>

<!-- table for vertical alignment of RUG logo with heading -->
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td valign="center">
<img src="/twitter/ruglogo.jpg" width="240" height="56" alt="logo RUG" border="0">
</td>
<td valign="center">
<font style="font-size:32px;"><strong> &nbsp; <a href="http://www.rug.nl/let/onderwijs/bachelor/informatiekunde/index" style="color:black; text-decoration:none;">Informatiekunde</a> voorspelt de Statenverkiezingen met Twitter</strong></font>
</td>
<td valign="center" align="center" width="150">
<script src="http://connect.facebook.net/nl_NL/all.js#xfbml=1"></script>
<fb:like href="http://ifarm.nl/ps2011" layout="box_count" show_faces="true" width="100" height="70"></fb:like>
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
</td>
</tr></table>
<p>

<!-- main table -->
<table cellpadding="0" cellspacing="0" width="1620">
<tr>
<td valign="top" align="center" width="260" height="620">
<table>
$graphs
</table>
</td>
<td valign="top" align="center" width="600" height="620">
THEEND

if (open(INFILE,$tableFile)) {
   while (<INFILE>) { 
      s/COMMAND/$command/;
      print;  
   }
   close(INFILE);
}

$graphs = "<tr>";
foreach $p qw(pvda pvdd pvv sgp sp vvd) {
   $graphs .= "<td align=\"center\" width=\"$tdWidth\" height=\"$tdHeight\"><strong><a href=\"/twitter/demo.$partyName,$p.50.svg\" style=\"color:black; text-decoration:none;\">";
   $graphs .= "<font color=\"red\">$fancy{$partyName}</font>";
   if ($p ne $partyName) {
      $graphs .= " vs <font color=\"blue\">$fancy{$p}</font>";
   }
   $graphs .= "</a></strong>\n<br><embed src=\"/twitter/demo.$partyName,$p.50.svg\" type=\"image/svg+xml\" width=\"$tableWidth\" height=\"$tableHeight\" /></td>";
   # if ($p eq "") { $graphs .= "</tr><tr>"; }
}
$graphs .= "</tr>";

print <<THEEND;
<p>
<div style="text-align:left;">
<table cellpadding="30">
<tr><td width="550">
De grafieken geven aan hoe vaak de partijen werden genoemd in
Nederlandstalige berichten op Twitter in de afgelopen 24 uur. 
Voor het bepalen van de zetelaantallen is hooguit 1  bericht 
per gebruiker per partij per dag meegenomen en zijn punten 
afgetrokken voor berichten die negatief waren over de partij.
(<a href="/twitter/ps2011uitleg.html">meer informatie</a>;
<a href="/twitter/ps2011uitleg.html#voorspelling">voorspelling</a>;
<a href="/blog/2011/03/03/verkiezingen-voorspellen-met-twitter/">blogpost</a>)
</td></tr></table>
</div>
</td>
<td width="700">
<p>
<!-- source: http://twitter.com/about/resources/widgets/widget_search -->
<script src="http://widgets.twimg.com/j/2/widget.js"></script>
<script>
new TWTR.Widget({
  version: 2,
  type: 'search',
  search: '$partyName',
  interval: 3000,
  title: 'Twitter over:',
  subject: 'De Provinciale Statenverkiezingen 2011: $fancy{$partyName}',
  width: 700,
  height: 500,
  theme: {
    shell: {
      background: '#000000',
      color: '#ffffff'
    },
    tweets: {
      background: '#ffffff',
      color: '#444444',
      links: '#1985b5'
    }
  },
  features: {
    scrollbar: false,
    loop: true,
    live: true,
    hashtags: true,
    timestamp: true,
    avatars: false,
    toptweets: true,
    behavior: 'default'
  }
}).render().start();
</script>
</td>
</tr>
<tr>
<td colspan="3" align="center" height="$tdHeight">
<table>
$graphs
</table>
<p>
<hr>
<p>
<center>
<address>
Contact: <a href="http://ifarm.nl/erikt/">e.f.tjong.kim.sang(at)rug.nl</a> en <a href="http://www.meaningfactory.com/bos">johan.bos(at)rug.nl</a> (050 - 363 6642), <a href="http://www.rug.nl/let/onderwijs/bachelor/informatiekunde/index">Informatiekunde</a>, Rijksuniversiteit Groningen, <a href="http://ifarm.nl/ps2011">http://ifarm.nl/ps2011</a>
</address>
</center>
</td>
</tr>
</table>
</center>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-20613920-5']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

</body>
</html>
THEEND

exit(0);
