Delhi Assembly Results 2015 – JSON

To see authentic Delhi Assembly Election 2015 results, I visited Election Commission of India website. Being a web developer I was very disappointed to see how website is created. I think the NIC developers have taken an oath for not following any of web standards. It is full of inline CSS and JavaScript and endless nested tables for layout. In the current mobile-era, nobody bothered to make if mobile friendly.

For every constituency the page was getting refreshed. The only thing that impressed me, was page load time. The result pages are only HTML files, no external resource dependency. Then I thought that they could have used JSON and then showed result by filtering data. This gave me an idea to create a JSON result of all records and then play with it.

TL;DR

I noticed that the url of results are in following format:

http://eciresults.nic.in/ConstituencywiseU05[constituency_code]

Luckily, It was easy to get code and name for all Delhi assembly constituencies as it was provided in Select dropdown to choose. Now, I only have to create a associated Array and fetch html for each url. Once I have the content, I can parse the html and extract the relevant data for each url and then join them together for comprehensive result data.

For HTML parsing I chose php-html-parser, an HTML DOM parser. It allows you to manipulate HTML. Find tags on an HTML page with selectors just like jQuery.

The PHP script is as follows:

Gist @GitHub

<?php 

//https://github.com/mohdovais/Delhi-Elections-2015


spl_autoload_register(
    function($className)
    {
        $className = str_replace("_", "\\", $className);
        $className = ltrim($className, '\\');
        $fileName = '';
        $namespace = '';
        if ($lastNsPos = strripos($className, '\\'))
        {
            $namespace = substr($className, 0, $lastNsPos);
            $className = substr($className, $lastNsPos + 1);
            $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
        }
        $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

        require $fileName;
        
    }
);

use PHPHtmlParser\Dom;

$Constituencies = array(
		"4"=>"ADARSH NAGAR",
		"48"=>"AMBEDKAR NAGAR",
		"67"=>"BABARPUR",
		"53"=>"BADARPUR",
		"5"=>"BADLI",
		"22"=>"BALLIMARAN",
		"7"=>"BAWANA",
		"36"=>"BIJWASAN",
		"2"=>"BURARI",
		"20"=>"CHANDNI CHOWK",
		"46"=>"CHHATARPUR",
		"38"=>"DELHI CANTT",
		"47"=>"DEOLI",
		"33"=>"DWARKA",
		"61"=>"GANDHI NAGAR",
		"66"=>"GHONDA",
		"68"=>"GOKALPUR",
		"50"=>"GREATER KAILASH",
		"28"=>"HARI NAGAR",
		"30"=>"JANAKPURI",
		"41"=>"JANGPURA",
		"51"=>"KALKAJI",
		"70"=>"KARAWAL NAGAR",
		"23"=>"KAROL BAGH",
		"42"=>"KASTURBA NAGAR",
		"9"=>"KIRARI",
		"56"=>"KONDLI",
		"60"=>"KRISHNA NAGAR",
		"58"=>"LAXMI NAGAR",
		"26"=>"MADIPUR",
		"43"=>"MALVIYA NAGAR",
		"12"=>"MANGOL PURI",
		"21"=>"MATIA MAHAL",
		"34"=>"MATIALA",
		"45"=>"MEHRAULI",
		"18"=>"MODEL TOWN",
		"25"=>"MOTI NAGAR",
		"8"=>"MUNDKA",
		"69"=>"MUSTAFABAD",
		"35"=>"NAJAFGARH",
		"11"=>"NANGLOI JAT",
		"1"=>"NERELA",
		"40"=>"NEW DELHI",
		"54"=>"OKHLA",
		"37"=>"PALAM",
		"24"=>"PATEL NAGAR",
		"57"=>"PATPARGANJ",
		"44"=>"R K PURAM",
		"39"=>"RAJINDER NAGAR",
		"27"=>"RAJOURI GARDEN",
		"6"=>"RITHALA",
		"13"=>"ROHINI",
		"64"=>"ROHTAS NAGAR",
		"19"=>"SADAR BAZAR",
		"49"=>"SANGAM VIHAR",
		"65"=>"SEELAMPUR",
		"63"=>"SEEMA PURI",
		"62"=>"SHAHDARA",
		"15"=>"SHAKUR BASTI",
		"14"=>"SHALIMAR BAGH",
		"10"=>"SULTANPUR MAJRA",
		"29"=>"TILAK NAGAR",
		"3"=>"TIMARPUR",
		"16"=>"TRI NAGAR",
		"55"=>"TRILOKPURI",
		"52"=>"TUGHLAKABAD",
		"32"=>"UTTAM NAGAR",
		"31"=>"VIKASPURI",
		"59"=>"VISHWAS NAGAR",
		"17"=>"WAZIRPUR"
	);


$result = array();

foreach ($Constituencies as $code => $constituency_name) {

	$constituency = array();

	$constituency['code'] = $code;
	$constituency['name'] = ucwords(strtolower($constituency_name));

	$dom = new Dom;

	$dom->loadFromUrl("http://eciresults.nic.in/ConstituencywiseU05$code.htm");

	$rows = $dom->find(' table table table table tr');

	$candidates = array();

	foreach ($rows as $row){

	    $tds = $row->find('td');

	    if(count($tds) === 3){
	    	$candidate = array();
	    	$candidate['candidate'] = ucwords(strtolower($tds[0]->innerHtml));
	    	$candidate['party'] = $tds[1]->innerHtml;
	    	$candidate['votes'] = $tds[2]->innerHtml;
	    	$candidates[] = $candidate;
	    }
	    
	}

	$constituency['result'] = $candidates;

	$result[] = $constituency;
}

header('content-type: application/json; charset=utf-8');
echo json_encode($result);

Now that I have the consolidated result, the next step is to play with the data and create some UI. I will update the post when I have something substantial to share.

Leave a Reply

Your email address will not be published. Required fields are marked *