Skip to content

Instantly share code, notes, and snippets.

@julio-kim
Last active February 27, 2021 06:34
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save julio-kim/26d64d6241fe17970884675ec314bf55 to your computer and use it in GitHub Desktop.
Save julio-kim/26d64d6241fe17970884675ec314bf55 to your computer and use it in GitHub Desktop.
[Scriptable] 코로나 확진자 현황
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) => {
if (count >= 500) return '#222831'
else if (count < 500 && count >= 300) return '#dc143c'
else if (count < 300 && count >= 100) return '#f05454'
else return '#0099ff'
}
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 * 30) // 30 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()
@julio-kim
Copy link
Author

기존 국내만 보여주던 현황에서 국내 + 해외유입을 포함한 현황으로 변경했습니다.

@julio-kim
Copy link
Author

위젯 등록시 When InteractingRun Script로 설정할 경우, 위젯 클릭시 해당 사이트로 이동처리 추가

@julio-kim
Copy link
Author

위젯 타이틀에 아이콘 추가

@julio-kim
Copy link
Author

아이폰용 타이틀 사이즈 조정

@julio-kim
Copy link
Author

  • Test 코드 방식 변경
  • small widget의 사이즈가 달라서 medium 사이즈로 설정

@julio-kim
Copy link
Author

500명 이상인 경우 그레이 색상 추가

@julio-kim
Copy link
Author

julio-kim commented Dec 13, 2020

  • 1000명 이상일 경우 오류 수정
  • widget padding 조정

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment