Created
July 18, 2021 15:12
-
-
Save seoh/4b1136e2db1a1d8b55ad5c97d1790554 to your computer and use it in GitHub Desktop.
Custom Clearance Progress
This file contains hidden or 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
// Variables used by Scriptable. | |
// These must be at the very top of the file. Do not edit. | |
// icon-color: gray; icon-glyph: magic; | |
// depend on `Cache` | |
// https://github.com/evandcoleman/scriptable/blob/main/src/lib/cache.js | |
// inline `Req` module | |
// https://gist.github.com/yoav-lavi/11e6b9e8e5b807ff20ddbb7d9d515229 | |
const Req = { | |
form: async ({ url, body, headers = {} }) => { | |
const request = new Request(url); | |
request.body = body; | |
request.method = methods.post; | |
request.headers = { | |
...defaultHeaders, | |
...headers | |
}; | |
return await request.loadJSON(); | |
}, | |
post: async ({ url, body, headers = {} }) => | |
this.form({ url, body: JSON.stringify(body), headers }), | |
put: async ({ url, body, headers = {} }) => { | |
const request = new Request(url); | |
request.body = JSON.stringify(body); | |
request.method = methods.put; | |
request.headers = { | |
...defaultHeaders, | |
...headers | |
}; | |
return await request.loadJSON(); | |
}, | |
get: async ({ url, headers = {} }) => { | |
const request = new Request(url); | |
request.method = methods.get; | |
request.headers = { | |
...defaultHeaders, | |
...headers | |
}; | |
return await request.loadJSON(); | |
} | |
}; | |
const defaultHeaders = { | |
Accept: "application/json", | |
} | |
const methods = { | |
get: "GET", | |
post: "POST", | |
put: "PUT" | |
}; | |
// end | |
const Cache = importModule('Cache'); | |
const cache = new Cache('customs'); | |
function queryFromHBL(hbl) { | |
const url = | |
'https://unipass.customs.go.kr/csp/myc/bsopspptinfo/cscllgstinfo/ImpCargPrgsInfoMtCtr/retrieveImpCargPrgsInfoLst.do'; | |
const params = | |
'firstIndex=0&page=1&pageIndex=1&pageSize=10&pageUnit=10&recordCountPerPage=10&qryTp=2&cargMtNo=&mblNo=&blYy=2021&hblNo='; | |
return Req.form({ | |
url, | |
body: params + hbl | |
}).then(json => json.resultList[0]) | |
} | |
function queryFromCargoNo(cargo) { | |
const url = | |
'https://unipass.customs.go.kr/csp/myc/bsopspptinfo/cscllgstinfo/ImpCargPrgsInfoMtCtr/retrieveImpCargPrgsInfoDtl.do'; | |
const params = | |
'firstIndex=0&recordCountPerPage=10&page=1&pageIndex=1&pageSize=10&pageUnit=10&cargMtNo='; | |
return Req.form({ | |
url, | |
body: params + cargo | |
}) | |
} | |
let id = args.widgetParameter || '' | |
if(config.runsInApp) { | |
const a = new Alert(); | |
a.title = 'Input HBL to be queried'; | |
a.addTextField('hbl', '000000000'); | |
a.addAction("Query"); | |
a.addCancelAction("Cancel"); | |
await a.present() | |
id = a.textFieldValue(0) | |
} | |
if(!id) return | |
let result = cache.read(id) | |
if(!result || Object.keys(result).length == 0) { | |
// console.log('call', id) | |
const cargoDetail = await queryFromHBL(id) | |
result = null | |
if(cargoDetail && cargoDetail.cargMtNo) | |
result = await queryFromCargoNo(cargoDetail.cargMtNo) | |
cache.write(id, result) | |
} | |
let widget = new ListWidget() | |
const stack = widget.addStack(); | |
stack.layoutVertically(); | |
const idT = stack.addText(id); | |
idT.font = Font.lightSystemFont(10); | |
// https://unipass.customs.go.kr/csp/js/csp/myc/bsopspptinfo/cscllgstinfo/MYC0405102Q.js?_=1606844678437:822-922 | |
const codes = ['CAGA01', 'CAGA10', 'CAGB01', 'CAGE01', 'CAGG01', 'IMPA01', 'IMPA19'] | |
const mesgs = ['적하목록제출', '적하목록 심사완료', '하선/하기신고', '물품반입', '보세운송', '수입신고', '수입신고수리']; | |
if(result && result.resultListM.cargTpcdCd == 'I') { | |
// 'I' import only supported. 'E', 'R', 'T' not supported. | |
const uprCdNm = result.impCargPrgsInfoRsltVo.englAbrtNm.substring(0,6); | |
const status = codes.map(c => c == uprCdNm ? '●': '○').join('') | |
const message = mesgs[codes.indexOf(uprCdNm)]; | |
stack.addText(status); | |
stack.addText(message); | |
stack.addText(''); | |
const infoT = stack.addText('Updated'); | |
infoT.font = Font.lightSystemFont(10); | |
stack.addText(result.resultListL[0].prcsDttm.replace(/(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/, '$2/$3 $4:$5:$6')); | |
} else if(!result) { | |
stack.addText("조회결과가 존재하지 않습니다.") | |
} | |
if (config.runsInWidget) { | |
Script.setWidget(widget) | |
} else if(config.runsInApp) { | |
widget.presentSmall() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/evandcoleman/scriptable/blob/main/src/lib/cache.js
를 Cache.js로 저장하고 위의 js 파일도 저장