Skip to content

Instantly share code, notes, and snippets.

@starenka starenka/komunal.py
Last active Aug 29, 2015

Embed
What would you like to do?
komunal.py
#!/usr/bin/env python
# coding=utf-8
import time
from pyquery import PyQuery as pq
def to_float(text):
return float(to_str(text).replace(',', '.').strip())
def to_str(el):
return el.text.encode('utf8')
def do(url, banner='', head=None):
doc = pq(url)
ns, vs, ps, cs = ('table td[headers="t2sa2 t2sb2"]',
'table td[headers="t2sa3 t2sb3"]',
'table td[headers="t2sa3 t2sb4"]',
'table td[headers="t2sa7"]',
)
names, percs, votes, chairs = doc.find(ns), doc.find(ps), doc.find(vs), doc.find(cs)
chairs = map(to_str, chairs) if chairs else ['n/a']*len(names)
ret = [(to_str(n), to_float(p), to_str(v), c) for n,p,v,c in zip(names, percs, votes, chairs)]
ret.sort(key=lambda one: one[1], reverse=True)
print '\n============= %s ==============' % banner.upper()
print '\nSECTENO: %s%%\n' % to_float(doc.find('td[headers="sa3 sb3"]')[0])
for name, perc, votes, chairs in ret if not head else ret[:head+1]:
print '%-50s%s%% (%s mandatu, %s hlasu)' % (name, perc, chairs, votes)
while True:
do('http://volby.cz/pls/kv2014/kv1111?xjazyk=CZ&xid=1&xdz=5&xnumnuts=1100&xobec=500186&xstat=0&xvyber=0', 'p7', head=8)
do('http://volby.cz/pls/kv2014/kv1111?xjazyk=CZ&xid=1&xdz=4&xnumnuts=1100&xobec=554782&xstat=0&xvyber=0', 'magistrat', head=8)
time.sleep(30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.