Created
October 29, 2012 02:56
-
-
Save metasov/3971225 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 | |
import curl | |
from datetime import datetime | |
connection = curl.Curl() | |
connection.set_timeout(20) | |
def get_document(url): | |
import time | |
from lxml.html import document_fromstring | |
html = "" | |
while not html: | |
html = "" | |
try: | |
data = connection.get(url) | |
html = data.decode('cp1251') | |
if not html: | |
raise Exception("Error: Blank Output") | |
if html.find(u"Інформація щодо цієї сторінки відсутня.")!=-1: | |
html = "" | |
raise Exception("Error: Інформація щодо цієї сторінки відсутня") | |
except Exception, e: | |
print e | |
time.sleep(5) | |
doc = document_fromstring(html) | |
return doc | |
def get_results_by_ovk(ovk_num): | |
results = {} | |
doc = get_document('http://cvk.gov.ua/vnd2012/wp304pt001f01=900pf7331=%d.html'%ovk_num) | |
rows = doc.xpath('//div[@id="content"]/table[@class="t2"][2]//tr')[1:] | |
for row in rows: | |
name, null, percent, count = map( lambda x:x.text_content().strip(), row.getchildren() ) | |
percent, count = float(percent)/100, int(count.replace(' ', '')) | |
results.setdefault(name, 0) | |
results[name] = percent | |
return results | |
doc = get_document('http://cvk.gov.ua/vnd2012/wp300pt001f01=900.html') | |
overall_percent = doc.xpath('//*[@id="content"]/table[4]//tr/td[2]')[0].text_content().replace(u' ', u'') | |
ovks = {} | |
doc = get_document('http://cvk.gov.ua/vnd2012/wp095_2pt001f01=900pt049f01=4.html') | |
rows = doc.xpath('//div[@id="content"]/table[@class="t2"][2]//tr')[1:-1] | |
for row in rows: | |
name, _all_count, real_count = map( lambda x:x.text_content().strip(), row.getchildren() ) | |
num = int(name.replace(u'ОВО №', u'')) | |
count = int(real_count.replace(u' ', u'')) | |
ovks[num] = count | |
ovks_results = {} | |
for ovk in ovks.keys(): | |
ovks_results[ovk] = get_results_by_ovk(ovk) | |
count_by_parties = {} | |
all_count = 0 | |
for ovk in ovks.keys(): | |
results = ovks_results[ovk] | |
for party in results.keys(): | |
count_by_parties.setdefault(party, 0) | |
count = results[party]*ovks[ovk] | |
count_by_parties[party] += count | |
all_count += count | |
results = [] | |
for party in count_by_parties.keys(): | |
results.append( (party, count_by_parties[party]/all_count*100) ) | |
results.sort(key = lambda x:x[1], reverse=True) | |
print u"<html><head><title>Выборы-Выборы...</title><meta http-equiv='Content-Type' content='text/html; charset=utf-8'/></head>" | |
print u"<body>" | |
print u"Обновлено: %s<br />"%datetime.now() | |
print u"Обработано протоколов: %s<br />"%overall_percent | |
print u"<table>" | |
for result in results: | |
print u"<tr><td>%s</td> <td>%f</td></tr>"%result | |
print u"</table></body></html>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment