-
-
Save pookjw/a910c1bc40eb4c8905e23a1dfeae6baa to your computer and use it in GitHub Desktop.
[Scriptable] 국내 코로나 확진자 현황
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
const source = 'http://ncov.mohw.go.kr' | |
let webView = new WebView() | |
await webView.loadURL(source) | |
let covid = await webView.evaluateJavaScript(` | |
const baseSelector = 'div.mainlive_container div.liveboard_layout ' | |
let date = document.querySelector(baseSelector + 'h2 span.livedate').innerText | |
let domestic = document.querySelector(baseSelector + 'div.liveNum_today_new ul li:nth-child(1) span.data').innerText | |
let overseas = document.querySelector(baseSelector + 'div.liveNum_today_new ul li:nth-child(2) span.data').innerText | |
completion({date, count: { | |
domestic: domestic.replace(",", ""), overseas | |
}, wpsData: WPS_data }) | |
`, true) | |
let count = parseInt(covid.count.domestic) + parseInt(covid.count.overseas) | |
let date = covid.date.replace(/\(|\)/g, '').split(',')[0] | |
//console.log(covid.wpsData) | |
const getIconSize = () => Device.isPhone() ? new Size(12, 12) : new Size(16, 16) | |
const getTitleSize = () => Device.isPhone() ? 17 : 20 | |
const getCountSize = (count) => { | |
if (count >= 1000) { | |
return Device.isPhone() ? 45 : 55 | |
} else { | |
return Device.isPhone() ? 55 : 70 | |
} | |
} | |
const getLevelColor = (count) => { | |
let randomIdx = Math.floor(Math.random() * 9) | |
if (randomIdx == 0) return '#007AFF' | |
else if (randomIdx == 1) return '#28CD41' | |
else if (randomIdx == 2) return '#5856D6' | |
else if (randomIdx == 3) return '#FF9500' | |
else if (randomIdx == 4) return '#FF2D55' | |
else if (randomIdx == 5) return '#AF52DE' | |
else if (randomIdx == 6) return '#FF3B30' | |
else if (randomIdx == 7) return '#55BEF0' | |
else if (randomIdx == 8) return '#FFCC00' | |
else return '#000000' | |
} | |
const getIcon = async (iconName, color = 'white') => { | |
let fm = FileManager.local() | |
let dir = fm.documentsDirectory() | |
let path = fm.joinPath(`${dir}`, `${iconName}.png`) | |
if (fm.fileExists(path)) { | |
return fm.readImage(path) | |
} else { | |
let iconImage = await loadImage(`https://iconsdb.com/icons/download/${color}/${iconName}.png`) | |
fm.writeImage(path, iconImage) | |
return iconImage | |
} | |
} | |
const loadImage = async (imageUrl) => { | |
let request = new Request(imageUrl) | |
return await request.loadImage() | |
} | |
const main = async () => { | |
let widget = new ListWidget() | |
widget.setPadding(0, 0, 0, 0) | |
widget.url = source | |
widget.backgroundColor = new Color(getLevelColor(count)) | |
widget.refreshAfterDate = new Date(Date.now() + 1000 * 600) // 600 Second | |
let titleRow = widget.addStack() | |
let titleStack = titleRow.addStack() | |
titleStack.layoutHorizontally() | |
titleStack.centerAlignContent() | |
titleStack.addSpacer() | |
let imageIco = titleStack.addImage(await getIcon('star-11-32')) | |
imageIco.imageSize = getIconSize() | |
imageIco.centerAlignImage() | |
titleStack.addSpacer(2) | |
let titleTxt = titleStack.addText('코로나-19') | |
titleTxt.centerAlignText() | |
titleTxt.textColor = Color.white() | |
titleTxt.font = Font.boldRoundedSystemFont(getTitleSize()) | |
titleStack.addSpacer() | |
/* | |
let titleTxt = widget.addText('코로나-19') | |
titleTxt.centerAlignText() | |
titleTxt.textColor = Color.white() | |
titleTxt.font = Font.boldRoundedSystemFont(20) | |
*/ | |
let countTxt = widget.addText(count.toString()) | |
countTxt.url = source | |
countTxt.centerAlignText() | |
countTxt.textColor = Color.white() | |
countTxt.font = Font.thinSystemFont(getCountSize(count)) | |
let dateTxt = widget.addText(date) | |
dateTxt.centerAlignText() | |
dateTxt.textColor = Color.white() | |
dateTxt.font = Font.thinSystemFont(15) | |
return widget | |
} | |
// Main Start | |
let widget = await main() | |
if (config.runsInWidget) { | |
Script.setWidget(widget) | |
} else { | |
// for Test | |
widget.presentSmall() | |
} | |
Script.complete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment