Skip to content

Instantly share code, notes, and snippets.

@spacelatte
Last active January 15, 2017 16:14
Show Gist options
  • Save spacelatte/bc1891cea5fd256cc3b6f784a08991d5 to your computer and use it in GitHub Desktop.
Save spacelatte/bc1891cea5fd256cc3b6f784a08991d5 to your computer and use it in GitHub Desktop.
pull winners from turkish lottery official page
#!/usr/bin/env node
'use strict';
const fs = require("fs")
const dns = require("dns")
const url = require('url')
const http = require("http")
const https = require("https")
const userAgentString = "Mozilla/5.0 (custom)"
const target = "http://millipiyango.gov.tr/sonuclar/listCekilisleriTarihleri.php?tur=piyango"
const actual = "http://millipiyango.gov.tr/sonuclar/cekilisler/piyango/{id}.json"
const amount = "amount"
if(require.main === module) {
if(process.argv.length < 3) {
console.log("usage:", process.argv.join(" "), "file")
console.log("\t" + "file to store data")
return
}
let file = process.argv[2]
let store = {}
try {
store = JSON.parse(fs.readFileSync(file))
} catch(e) {
console.log("[fail] file read & parse:", e)
}
process.on("beforeExit", function(e) {
console.log("[info] writing to:", file)
try {
fs.writeFileSync(file, JSON.stringify(store, null, 4))
console.log("[ ok ] sync done")
} catch(err) {
console.log("[fail] writing!")
}
return
})
let body = /*new*/ String()
let req = http.request({
method: "GET",
host: url.parse(target).host,
//port: 80,
//auth: credentials,
path: url.parse(target).path,
headers: {
"user-agent": userAgentString,
},
}, function(res) {
if(!res) {
console.log("[warn] returning:", "request.result")
return
}
res.on("data", function(d) {
body += d
return
})
return
}) // -http.request
req.on("error", function(e) {
console.log("[fail] error:", endpoint, "msg:", e)
return
}) // -req.on error
req.on("close", function(e) {
if(!req.res || !body) {
console.log("[warn] returning:", "request.on.close")
return
}
let json = JSON.parse(body)
json.forEach(function(x, i, o) {
let body = ""
let req = http.request({
//auth: credentials,
//port: 80,
host: url.parse(actual).host,
path: url.parse(actual.replace("{id}", x.tarih)).path,
method: "GET",
headers: {
"user-agent": userAgentString,
},
}, function(res) {
if(!res) {
return
}
res.on("data", function(d) {
body += d
return
})
return
})
req.on("error", function(e) {
console.log("[fail] sub-request:", e)
return
})
req.on("close", function(e) {
if(!req.res || !body) {
return
}
//console.log(req, body)
let json = JSON.parse(body)
json.sonuclar.forEach(function(s, i, o) {
if(store[x.tarih] === undefined) {
store[x.tarih] = {}
}
if(store[x.tarih][s.tip] === undefined) {
store[x.tarih][s.tip] = {}
}
if(store[x.tarih][s.tip][s.ikramiye] === undefined) {
store[x.tarih][s.tip][s.ikramiye] = []
}
if(store[x.tarih][s.tip][s.ikramiye].length >= s.numaralar.length) {
return
}
s.numaralar.forEach(function(n, i, o) {
store[x.tarih][s.tip][s.ikramiye].push(n)
return
})
return
})
return
}) //-req.on close
req.end()
return
})
return
}) // -req.on close
req.end()
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment