Last active
March 11, 2019 11:24
-
-
Save gbidkar/62933a1d6674afddd19eb2feb929d1ec to your computer and use it in GitHub Desktop.
Node.js script to extract Haaretz's "survey of surveys", of Israeli Elections 2019 information as a JSON object
This file contains 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
/// | |
/// SURVEYS.JS | |
/// Extracts Haaretz's 2019 Elections survey JSON data. | |
/// Author: @gbidkar | |
/// | |
/// npm install --save request | |
/// | |
/// Final voting information is 'surveys' variable. | |
const request = require('request'); | |
const ElectionsDataURL = 'https://www.haaretz.co.il/news/elections/EXT-INTERACTIVE-1.6826451'; | |
function extractSurveysData(body) { | |
let votesRawBegin = body.substring(body.indexOf('var xls')); | |
let votesRaw = votesRawBegin.substring(votesRawBegin.indexOf('{'), votesRawBegin.indexOf(';')); | |
return JSON.parse(votesRaw); | |
} | |
function printStatistics(surveyInfo) { | |
console.log(`There are ${surveyInfo.Surveys.length} surveys, spanning ${surveyInfo.Parties.length} parties.`); | |
} | |
request(ElectionsDataURL, (err, res, body) => { | |
let surveys = extractSurveysData(body); | |
printStatistics(surveys); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment