Skip to content

Instantly share code, notes, and snippets.

@QuadFlask
Forked from julio-kim/CovidStat.js
Last active December 22, 2020 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuadFlask/5ab7c6c66eb2ad7019648c8adfea3302 to your computer and use it in GitHub Desktop.
Save QuadFlask/5ab7c6c66eb2ad7019648c8adfea3302 to your computer and use it in GitHub Desktop.
[Scriptable] 코로나 확진자 현황
// 수정 내용: 페이지 대신 ajax 호출로 속도 향상, 데이터 사용량 감소
// 기본 색상 검은 배경, 흰 글씨, 빨간 아이콘으로 변경
const source = 'http://corona-live.com';
const request = new Request('https://apiv2.corona-live.com/stats.json?timestamp='+Date.now())
const response = await request.loadJSON()
const count = response.overview.current[0];
const now = new Date();
const date = `${now.getMonth()+1}월 ${now.getDate()}일 ${now.toLocaleString('kr', { hour: 'numeric', minute: 'numeric', hour12: true })}`;
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 : 68
}
}
const getDateSize = () => Device.isPhone() ? 11 : 14
const getLevelColor = (count) => {
return 'black';
}
const getIcon = async (iconName, color = 'black') => {
let fm = FileManager.local()
let dir = fm.documentsDirectory()
let path = fm.joinPath(`${dir}`, `${iconName}.png`)
if (fm.fileExists(path) && config.runsInWidget) {
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 * 300)
let titleRow = widget.addStack()
let titleStack = titleRow.addStack()
titleStack.layoutHorizontally()
titleStack.centerAlignContent()
titleStack.addSpacer()
let imageIco = titleStack.addImage(await getIcon('star-11-32', 'red'))
imageIco.imageSize = getIconSize()
imageIco.centerAlignImage()
titleStack.addSpacer(2)
let titleTxt = titleStack.addText('코로나LIVE')
titleTxt.centerAlignText()
titleTxt.textColor = Color.white()
titleTxt.font = Font.boldRoundedSystemFont(getTitleSize())
titleStack.addSpacer()
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(getDateSize())
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