Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as request from "request"
- import * as cheerio from "cheerio"
- import * as fs from "fs"
- import * as Q from "q"
- // https://www.google.com/search?q=2016+US+election+results&hl=en#eob=enn/p//0/0////////////
- const url0 = "https://www.google.com/async/eob_en_v?async=v:p,prst:1,pvst:0,isf:1,_id:eob-enav,_pms:s&hl=en&yv=2"
- const url1 = "https://www.google.com/async/eob_en_v?async=v:p,prst:1,pvst:1,isf:1,_id:eob-enav,_pms:s&hl=en&yv=2"
- // function qRequest(url: string): Q.Promise<any> {
- // const result = Q.defer()
- // request(url, (error, res, body) => {
- // if (error) {
- // result.reject({error: error})
- // }
- // else {
- // result.resolve({res: res, body: body})
- // }
- // })
- // return result.promise
- // }
- export function generateJson(callback: (string) => void) {
- request(url0, (error, res, body) => {
- const json = JSON.parse((body))
- const html = json[1][1]
- const $ = cheerio.load(html)
- const qElectoralOneName = "._Lzm ._kAm"
- const qElectoralOneVotes = "._Lzm ._Azm"
- const qElectoralOneNeeded = "._BBm ._pRo"
- const qElectoralTwoName = "._Kzm ._kAm"
- const qElectoralTwoVotes = "._Kzm ._Azm"
- const qElectoralTwoNeeded = "._BBm ._sTo"
- const text0 = ["electoral vote",
- $(qElectoralOneName).text() + " " + $(qElectoralOneVotes).text() +
- " (" + $(qElectoralOneNeeded).text() + ")" + " — " +
- $(qElectoralTwoName).text() + " " + $(qElectoralTwoVotes).text() +
- " (" + $(qElectoralTwoNeeded).text() + ")"
- ]
- // console.log(text0)
- request(url1, (error, res, body) => {
- const json = JSON.parse((body))
- const html = json[1][1]
- const $ = cheerio.load(html)
- const qCandidate = "._Hdm"
- const qName = "._V6l"
- const qVotes = "._tfm"
- const qVotesPercent = "._aem"
- const topTwoCandidates = $(qCandidate).slice(0, 2)
- const candidate0 = topTwoCandidates[0]
- const candidate1 = topTwoCandidates[1]
- const text1 = ["popular vote",
- $(candidate0).find(qName).text().split(" ")[1] + " " + $(candidate0).find(qVotesPercent).text() +
- " (" + $(candidate0).find(qVotes).text() + ")" + " — " +
- $(candidate1).find(qName).text().split(" ")[1] + " " + $(candidate1).find(qVotesPercent).text() +
- " (" + $(candidate1).find(qVotes).text() + ")"
- ]
- // console.log(text1)
- const text2 = ["progress", $("._eLo").text()]
- const outputJson = JSON.stringify([text0, text1, text2])
- callback(outputJson)
- })
- })
- }
- if (require.main === module) {
- generateJson((json) => {
- console.log(json)
- fs.writeFileSync("./data/us-2016-elections.json", json)
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment