Created
August 24, 2016 07:36
-
-
Save gabonator/9d4b36c22499aefa8822c2b134347efc to your computer and use it in GitHub Desktop.
nodejs API for requesting delivery changes on Slovenska posta online service (eph.posta.sk)
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
// http://eph.posta.sk/ | |
login = "YourLogin"; | |
password = "YourPassword"; | |
reportUrl = "http://api.mypage.com/postaservice/?"; | |
var request = require('request'); | |
var fs = require('fs'); | |
var crypto = require('crypto'); | |
var cookies; | |
var headers = { | |
"User-Agent":"Mozilla/5.0", | |
"Content-Type":"application/json", | |
"Accept":"application/json", | |
"X-Requested-With":"XMLHttpRequest" }; | |
var options_login = { | |
url:'https://mojezasielky.posta.sk/api/webapp/session', | |
method: 'PUT', | |
headers: headers, | |
body: '{"email":"'+login+'","password":"'+password+'"}' | |
}; | |
var options_logout = { | |
url:'https://mojezasielky.posta.sk/api/webapp/session', | |
method: 'DELETE', | |
headers: headers | |
}; | |
var options_list = { | |
url:'https://mojezasielky.posta.sk/api/webapp/parcels?po=0&pl=100', | |
method: 'GET', | |
headers: headers | |
}; | |
function parseCookies (headers) { | |
var list = ""; | |
headers["set-cookie"].forEach(function( cookie ) { | |
list += cookie.split(';')[0] + ";"; | |
}); | |
return list; | |
} | |
function load() | |
{ | |
request(options_login, function(error, response, body) { | |
if ( error || typeof(response) == "undefined" ) | |
{ | |
console.log("Error: " + error.code); | |
return; | |
} | |
_ASSERT(response.statusCode == 200); | |
_ASSERT(JSON.parse(body).ok == 1); | |
cookies = parseCookies(response.headers); | |
options_list.headers["Cookie"] = cookies; | |
options_logout.headers["Cookie"] = cookies; | |
request(options_list, function(error, response, body) { | |
if ( error || typeof(response) == "undefined" ) | |
{ | |
console.log("Error: " + error.code); | |
return; | |
} | |
processData(JSON.parse(body)); | |
request(options_logout, function(error, response, body) { | |
if ( error || typeof(response) == "undefined" ) | |
{ | |
console.log("Error: " + error.code); | |
return; | |
} | |
_ASSERT(JSON.parse(body).ok == 1); | |
}); | |
}); | |
}); | |
} | |
function _ASSERT(x) | |
{ | |
if (!x) | |
console.log("Assertion failed"); | |
} | |
function md5(str) | |
{ | |
return crypto.createHash('md5').update(str).digest("hex"); | |
} | |
function timestamp() | |
{ | |
return ("" + new Date()).split(" GM")[0]; | |
} | |
function log(msg) | |
{ | |
fs.appendFileSync('log.txt', msg + "\r\n"); | |
} | |
var cache = []; | |
function symbol(p) | |
{ | |
if (p.info && p.info.cod && p.info.cod.symbol ) | |
return p.info.cod.symbol; | |
return ""; | |
} | |
function recipient(p) | |
{ | |
return p.info.recipient.name + "," + p.info.recipient.street + "," + p.info.recipient.city; | |
} | |
var firstRun = true; | |
function processData(json) | |
{ | |
var newids = []; | |
for (var i in json.parcels) | |
{ | |
var parcel = json.parcels[i]; | |
var hash = md5(JSON.stringify(parcel)).substr(-8); | |
var info = ""; | |
newids[parcel.id] = 1; | |
if (typeof(cache[parcel.id]) == "undefined") | |
{ | |
cache[parcel.id] = {raw:parcel, state:parcel.tracking, hash:hash, recip:parcel.info.recipient.name}; | |
info = "new"; | |
if (!firstRun) | |
{ | |
url = reportUrl + "id="+parcel.id+"&number="+parcel.info.number+"&symbol="+symbol(parcel)+"&status="+parcel.status+"&tracking="+parcel.tracking+"&recipient="+recipient(parcel); | |
request(url, function() {}); | |
} | |
} else | |
if (cache[parcel.id].state == parcel.tracking && cache[parcel.id].hash == hash) | |
{ | |
continue; | |
} else | |
{ | |
info = "UPDATE"; | |
cache[parcel.id] = {raw:parcel, state:parcel.tracking, hash:hash, recip:parcel.info.recipient.name}; | |
url = reportUrl + "id="+parcel.id+"&number="+parcel.info.number+"&symbol="+symbol(parcel)+"&status="+parcel.status+"&tracking="+parcel.tracking+"&recipient="+recipient(parcel); | |
request(url, function() {}); | |
} | |
console.log(timestamp() + " " + info + "> " + parcel.id + ":" + parcel.info.number + "," + symbol(parcel) + "," + parcel.status + "/" + parcel.tracking + "," + hash + "," + recipient(parcel));// JSON.stringify(parcel.info.recipient.name)); | |
log(timestamp() + " " + info + "> " + parcel.id + ":" + parcel.info.number + "," + symbol(parcel) + "," + + parcel.status + "/" + parcel.tracking + "," + hash + "," + recipient(parcel)); //JSON.stringify(parcel.info.recipient.name)); | |
log(JSON.stringify(parcel)); | |
} | |
for (var i in cache) | |
{ | |
var parcel = cache[i].raw; | |
if ( typeof(newids[i]) == "undefined" ) | |
{ | |
console.log(timestamp() + " delete> " + parcel.id + ":" + parcel.info.number + "," + symbol(parcel) + "," + parcel.status +"/" + parcel.tracking + "," + hash + "," + recipient(parcel)); | |
log(timestamp() + " delete> " + parcel.id + ":" + parcel.info.number + "," + symbol(parcel) + "," + parcel.status +"/" + parcel.tracking + "," + hash + "," + recipient(parcel)); | |
log(JSON.stringify(parcel)); | |
delete cache[i]; | |
} | |
} | |
firstRun = false; | |
} | |
load(); | |
setInterval(load, 5*60*1000); // refresh in 5min |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Zdravím,
chcem sa spýtať ako to viem použiť? Nie som v javascripte veľmi zbehlý...ďakujem.