|
// Variables used by Scriptable. |
|
// These must be at the very top of the file. Do not edit. |
|
// icon-color: light-brown; icon-glyph: magic; |
|
const SAP_ORDER_NO = 'YOUR ORDER NO' |
|
const POSTAL_CODE = 'YOUR POSTAL CODE' |
|
const UPDATE_INTERVAL = 1000*60*60 // 60 minutes |
|
|
|
const USE_CACHED_DATA = true |
|
|
|
let BG_COLOR = '#000000' |
|
|
|
let fm = FileManager.iCloud() |
|
let dir = fm.documentsDirectory() |
|
const path = fm.joinPath(dir, "vodafone.json") |
|
|
|
const JSONObject = await handleFile(fm, path) |
|
const widget = await createWidget(JSONObject) |
|
|
|
// Check if the script is running in |
|
// a widget. If not, show a preview of |
|
// the widget to easier debug it. |
|
if (!config.runsInWidget) { |
|
await widget.presentSmall() |
|
} |
|
|
|
// Tell the system to show the widget. |
|
Script.setWidget(widget) |
|
Script.complete() |
|
|
|
async function createWidget(JSONObject) { |
|
|
|
const bg = new Color(BG_COLOR) |
|
|
|
const w = new ListWidget() |
|
w.useDefaultPadding() |
|
w.backgroundColor = bg |
|
// w.refreshAfterDate = new Date(nextRefresh) |
|
w.url = `https://www.vodafone.de/meinvodafone/bestellstatus-ergebnis.html?SAPorderNo=${SAP_ORDER_NO}&postalcode=${POSTAL_CODE}` |
|
|
|
const row = w.addStack() |
|
row.layoutHorizontally() |
|
row.addSpacer() |
|
|
|
if (typeof JSONObject.REQUESTSTATUS !== 'undefined' && JSONObject.REQUESTSTATUS == "Error") { |
|
|
|
const _errorCode = JSONObject.ERRORDETAILS.ERRORCODE |
|
const _errorMessage = JSONObject.ERRORDETAILS.ERRORMESSAGE |
|
|
|
const code = w.addText("Error: " + _errorCode) |
|
code.font = Font.regularSystemFont(20) |
|
code.textColor = Color.white() |
|
|
|
w.addSpacer() |
|
|
|
const message = w.addText(_errorMessage) |
|
message.font = Font.regularSystemFont(12) |
|
message.textColor = Color.white() |
|
|
|
} else if(typeof JSONObject.REQUESTSTATUS !== 'undefined' && JSONObject.REQUESTSTATUS == "Success") { |
|
|
|
const _historytexts = JSONObject.ORDERSTATUSDETAILS.ORDERDATA[0].ORDERSTATUSINFORMATION.STATUSHISTORYTEXTS |
|
const _statusid = JSONObject.ORDERSTATUSDETAILS.ORDERDATA[0].ORDERSTATUSINFORMATION.CURRENTSTATUSID |
|
const _name = JSONObject.ORDERSTATUSDETAILS.ORDERDATA[0].PRODUCTDETAILS.DEVICE.PRODUCTNAME |
|
const _leadtime = JSONObject.ORDERSTATUSDETAILS.ORDERDATA[0].PRODUCTDETAILS.DEVICE.LEADTIME |
|
const _historytext = _historytexts[_statusid-1] |
|
|
|
let fontSize = 20 |
|
let spacing = 4 |
|
|
|
if (_leadtime.length <= 5) { |
|
fontSize = 30 |
|
spacing = 2 |
|
} |
|
|
|
let lastUpdateString = new Date(JSONObject.lastUpdate) |
|
lastUpdateString = lastUpdateString.toLocaleTimeString() |
|
const lastUpdate = w.addText("Last Update: " + lastUpdateString) |
|
lastUpdate.font = Font.regularSystemFont(6) |
|
lastUpdate.textColor = Color.white() |
|
|
|
w.addSpacer(6) |
|
|
|
const name = w.addText(_name) |
|
name.font = Font.regularSystemFont(10) |
|
name.textColor = Color.white() |
|
|
|
w.addSpacer(spacing) |
|
|
|
const leadtime = w.addText(_leadtime) |
|
leadtime.font = Font.mediumRoundedSystemFont(fontSize) |
|
leadtime.textColor = Color.white() |
|
|
|
w.addSpacer(spacing) |
|
|
|
if (typeof JSONObject.ORDERSTATUSDETAILS.ORDERDATA[0].SHIPPINGINFORMATION !== 'undefined') { |
|
const _logisticStatus = JSONObject.ORDERSTATUSDETAILS.ORDERDATA[0].SHIPPINGINFORMATION.LOGISTICSTATUS |
|
const _lastLogisticStatus = _logisticStatus[0] |
|
|
|
const _logisticsLocation = _lastLogisticStatus.LOCATION |
|
const _logisticsText = _lastLogisticStatus.STATUSTEXT |
|
|
|
const logisticsLocation = w.addText(_logisticsLocation) |
|
logisticsLocation.font = Font.regularSystemFont(10) |
|
logisticsLocation.textColor = Color.white() |
|
|
|
const logisticsText = w.addText(_logisticsText) |
|
logisticsText.font = Font.regularSystemFont(10) |
|
logisticsText.textColor = Color.white() |
|
} else { |
|
const historytext = w.addText(_historytext) |
|
historytext.font = Font.regularSystemFont(10) |
|
historytext.textColor = Color.white() |
|
} |
|
|
|
} else if(typeof JSONObject.REQUESTSTATUS === 'undefined' && typeof JSONObject.PRODUCTDETAILS.DEVICE.LEADTIME !== 'undefined') { |
|
// delete file |
|
const err = w.addText("please delete vodafone.json") |
|
err.font = Font.regularSystemFont(10) |
|
err.textColor = Color.white() |
|
} |
|
|
|
return w |
|
} |
|
|
|
async function handleFile(fm, path) { |
|
let JSONObject = JSON.parse('{}') |
|
|
|
if (fm.fileExists(path)) { // if file exists |
|
JSONObject = readJSON(path) // read file |
|
if (typeof JSONObject.lastUpdate === 'undefined' || JSONObject.lastUpdate + UPDATE_INTERVAL < Date.now()) { |
|
// update needed |
|
console.log('updating') |
|
let json = await fetchJSON() |
|
if (json === false) { |
|
console.log('update failed') |
|
} else { |
|
JSONObject = json |
|
} |
|
saveJSON(path, JSONObject) |
|
} |
|
|
|
// to update old file versions |
|
if(typeof JSONObject.REQUESTSTATUS !== 'undefined') { |
|
return JSONObject |
|
} |
|
} |
|
// no file available |
|
console.log('updating') |
|
let json = await fetchJSON() |
|
// await QuickLook.present(json) |
|
if (json === false) { |
|
console.log('update failed') |
|
} else { |
|
JSONObject = json |
|
} |
|
// only save to file when request is success |
|
if (typeof JSONObject.REQUESTSTATUS !== 'undefined' && JSONObject.REQUESTSTATUS == "Success") { |
|
saveJSON(path, JSONObject) |
|
} |
|
|
|
return JSONObject |
|
} |
|
|
|
async function fetchJSON() { |
|
try { |
|
const url = `https://www.vodafone.de/orderStatusService/?function=getOrderStatus&SAPorderNo=${SAP_ORDER_NO}&postalcode=${POSTAL_CODE}` |
|
const req = new Request(url) |
|
let json = await req.loadJSON() |
|
|
|
if (typeof json.vfAjaxResponseParams !== 'undefined') { |
|
//request returned JSON |
|
json = json.vfAjaxResponseParams |
|
return json |
|
} |
|
//request didn't return valid JSON |
|
return false |
|
} |
|
catch(err) { |
|
console.log(err.message) |
|
return false |
|
} |
|
} |
|
|
|
function saveJSON(JSONpath, JSONObject) { |
|
JSONObject.lastUpdate = Date.now() |
|
fm.writeString(JSONpath, JSON.stringify(JSONObject)) |
|
} |
|
|
|
function readJSON(JSONpath) { |
|
return JSON.parse(fm.readString(JSONpath)) |
|
} |
Hey hey,
vielen Dank für das super script, nur irgendwie tut das nicht mehr... mach ich da irgendwie was falsch?
Ich habe lediglich oben die Bestellnummer und meine Postleizahl eingetragen. Es schlägt irgendwie immer bei Zeile 70 fehl.