#!/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 warnings import filterwarnings
filterwarnings('ignore', category=DeprecationWarning)

import re
from urllib2 import urlopen
from subprocess import call
from time import sleep

from html5lib import HTMLParser, treebuilders

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

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

facts = []
batch = 20

for i in xrange(100):
	resultsf = urlopen('http://spinach.omnia.za.net/message?m=search+%s+facts+compass+from+%s' % (batch, i*batch))
	entries = resultsf.read()
	resultsf.close()
	facts.extend([fact.rsplit(' ', 1)[0].strip() for fact in entries.split(';') if '.political_compass' in fact])
	sleep(5)
	if len(entries.split(';')) != batch:
		break

for fact in facts:
	f = urlopen('http://spinach.omnia.za.net/message?m=%s' % (fact,))
	factoid = f.read()
	f.close()

	who = fact.split('.')[0]
	value = factoid.split(' is ')[1]
	numbers = values.search(value).groups()
	output.write(numbers[0] + '\t' + numbers[1] + '\t' + who + '\n')
	sleep(5)

output.close()

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