Guest User

us-2016-election.ts

a guest
Nov 9th, 2016
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as request from "request"
  2. import * as cheerio from "cheerio"
  3. import * as fs from "fs"
  4. import * as Q from "q"
  5.  
  6. // https://www.google.com/search?q=2016+US+election+results&hl=en#eob=enn/p//0/0////////////
  7. 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"
  8. 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"
  9.  
  10. // function qRequest(url: string): Q.Promise<any> {
  11. //   const result = Q.defer()
  12. //   request(url, (error, res, body) => {
  13. //     if (error) {
  14. //       result.reject({error: error})
  15. //     }
  16. //     else {
  17. //       result.resolve({res: res, body: body})
  18. //     }
  19. //   })
  20. //   return result.promise
  21. // }
  22.  
  23. export function generateJson(callback: (string) => void) {
  24.   request(url0, (error, res, body) => {
  25.     const json = JSON.parse((body))
  26.     const html = json[1][1]
  27.     const $ = cheerio.load(html)
  28.  
  29.     const qElectoralOneName = "._Lzm ._kAm"
  30.     const qElectoralOneVotes = "._Lzm ._Azm"
  31.     const qElectoralOneNeeded = "._BBm ._pRo"
  32.     const qElectoralTwoName = "._Kzm ._kAm"
  33.     const qElectoralTwoVotes = "._Kzm ._Azm"
  34.     const qElectoralTwoNeeded = "._BBm ._sTo"
  35.  
  36.     const text0 = ["electoral vote",
  37.       $(qElectoralOneName).text() + " " + $(qElectoralOneVotes).text() +
  38.         " (" + $(qElectoralOneNeeded).text() + ")" + " — " +
  39.       $(qElectoralTwoName).text() + " " + $(qElectoralTwoVotes).text() +
  40.         " (" + $(qElectoralTwoNeeded).text() + ")"
  41.     ]
  42.     // console.log(text0)
  43.  
  44.     request(url1, (error, res, body) => {
  45.       const json = JSON.parse((body))
  46.       const html = json[1][1]
  47.       const $ = cheerio.load(html)
  48.  
  49.       const qCandidate = "._Hdm"
  50.       const qName = "._V6l"
  51.       const qVotes = "._tfm"
  52.       const qVotesPercent = "._aem"
  53.  
  54.       const topTwoCandidates = $(qCandidate).slice(0, 2)
  55.       const candidate0 = topTwoCandidates[0]
  56.       const candidate1 = topTwoCandidates[1]
  57.  
  58.       const text1 = ["popular vote",
  59.         $(candidate0).find(qName).text().split(" ")[1] + " " + $(candidate0).find(qVotesPercent).text() +
  60.         " (" + $(candidate0).find(qVotes).text() + ")" + " — " +
  61.         $(candidate1).find(qName).text().split(" ")[1] + " " + $(candidate1).find(qVotesPercent).text() +
  62.         " (" + $(candidate1).find(qVotes).text() + ")"
  63.       ]
  64.       // console.log(text1)
  65.       const text2 = ["progress", $("._eLo").text()]
  66.  
  67.       const outputJson = JSON.stringify([text0, text1, text2])
  68.       callback(outputJson)
  69.     })
  70.   })
  71. }
  72.  
  73. if (require.main === module) {
  74.   generateJson((json) => {
  75.     console.log(json)
  76.     fs.writeFileSync("./data/us-2016-elections.json", json)
  77.   })
  78. }
Advertisement
Add Comment
Please, Sign In to add comment