#!/usr/bin/env python

#	Retriever for Political Compass scores stored in Spinach
#	Copyright (C) 2008 Michael Gorven
#	
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU Affero General Public License as
#	published by the Free Software Foundation, either version 3 of the
#	License, or (at your option) any later version.
#	
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU Affero General Public License for more details.
#	
#	You should have received a copy of the GNU Affero General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.

from urllib2 import urlopen
from BeautifulSoup import BeautifulSoup
import re
from subprocess import call

split = re.compile('\.')
values = re.compile(' *([-0-9.]*) */? *([-0-9.]*).*')

output = open('/home/mgorven/public_html/clug-political.txt', 'w')

resultsf = urlopen("http://pepper.omnia.za.net/spinach/spinach.php?fact=.political_compass&searchfact=plain&verb=&value=&searchvalue=full")
results = BeautifulSoup(resultsf.read())
resultsf.close()

table = results.find('table', attrs={'class':'factoids'})
this = table.findAll('tr', attrs={'class':'this'})
that = table.findAll('tr', attrs={'class':'that'})
this.extend(that)
for row in this:
	who = split.split(row.find('td', attrs={'class':'fact'}).contents[0])[0]
	value = row.find('td', attrs={'class':'value'}).contents[0].split(',')[0]
	numbers = values.search(value).groups()
	output.write(numbers[0] + '\t' + numbers[1] + '\t' + who + '\n')

output.close()

call(["gnuplot", "/home/mgorven/bin/clug-political.p"], env={"GDFONTPATH":"/usr/share/fonts/truetype/ttf-bitstream-vera/"})
