Skip to content

Instantly share code, notes, and snippets.

@julio-kim
Last active February 27, 2021 06:34

Revisions

  1. julio-kim revised this gist Dec 13, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -110,6 +110,6 @@ if (config.runsInWidget) {
    Script.setWidget(widget)
    } else {
    // for Test
    widget.presentMedium()
    widget.presentSmall()
    }
    Script.complete()
  2. julio-kim revised this gist Dec 13, 2020. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -9,13 +9,15 @@ let covid = await webView.evaluateJavaScript(`
    let overseas = document.querySelector(baseSelector + 'div.liveNum_today_new ul li:nth-child(2) span.data').innerText
    completion({date, count: {
    domestic, overseas
    }})
    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
    @@ -56,6 +58,7 @@ const loadImage = async (imageUrl) => {

    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
  3. julio-kim revised this gist Dec 1, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -105,8 +105,8 @@ const main = async () => {
    let widget = await main()
    if (config.runsInWidget) {
    Script.setWidget(widget)
    Script.complete()
    } else {
    // for Test
    widget.presentMedium()
    }
    }
    Script.complete()
  4. julio-kim revised this gist Nov 25, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,8 @@ const getCountSize = (count) => {
    }

    const getLevelColor = (count) => {
    if (count >= 300) return '#dc143c'
    if (count >= 500) return '#222831'
    else if (count < 500 && count >= 300) return '#dc143c'
    else if (count < 300 && count >= 100) return '#f05454'
    else return '#0099ff'
    }
  5. julio-kim revised this gist Nov 25, 2020. 1 changed file with 8 additions and 17 deletions.
    25 changes: 8 additions & 17 deletions CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -40,10 +40,8 @@ const getIcon = async (iconName, color = 'white') => {
    let path = fm.joinPath(`${dir}`, `${iconName}.png`)

    if (fm.fileExists(path)) {
    console.log(`file is exists: ${path}`)
    return fm.readImage(path)
    } else {
    console.log(`file is not exists: ${path}`)
    let iconImage = await loadImage(`https://iconsdb.com/icons/download/${color}/${iconName}.png`)
    fm.writeImage(path, iconImage)
    return iconImage
    @@ -55,8 +53,7 @@ const loadImage = async (imageUrl) => {
    return await request.loadImage()
    }

    // Main Start
    if (config.runsInWidget) {
    const main = async () => {
    let widget = new ListWidget()
    widget.url = source
    widget.backgroundColor = new Color(getLevelColor(count))
    @@ -99,22 +96,16 @@ if (config.runsInWidget) {
    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)
    Script.complete()
    } else {
    // for Test
    let table = new UITable()

    let row = new UITableRow()
    row.isHeader = true
    row.addText(`코로나 확진자 현황 (${date})`)
    table.addRow(row)

    row = new UITableRow()
    row.addText('확진자수')
    row.addText(count.toString()).rightAligned()
    table.addRow(row)

    table.present()
    widget.presentMedium()
    }
  6. julio-kim revised this gist Nov 25, 2020. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,10 @@ let covid = await webView.evaluateJavaScript(`
    let count = parseInt(covid.count.domestic) + parseInt(covid.count.overseas)
    let date = covid.date.replace(/\(|\)/g, '').split(',')[0]

    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
    @@ -66,15 +70,15 @@ if (config.runsInWidget) {
    titleStack.addSpacer()

    let imageIco = titleStack.addImage(await getIcon('star-11-32'))
    imageIco.imageSize = new Size(16, 16)
    imageIco.imageSize = getIconSize()
    imageIco.centerAlignImage()

    titleStack.addSpacer(2)

    let titleTxt = titleStack.addText('코로나-19')
    titleTxt.centerAlignText()
    titleTxt.textColor = Color.white()
    titleTxt.font = Font.boldRoundedSystemFont(20)
    titleTxt.font = Font.boldRoundedSystemFont(getTitleSize())

    titleStack.addSpacer()

  7. julio-kim revised this gist Nov 25, 2020. 1 changed file with 44 additions and 0 deletions.
    44 changes: 44 additions & 0 deletions CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -30,16 +30,60 @@ const getLevelColor = (count) => {
    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)) {
    console.log(`file is exists: ${path}`)
    return fm.readImage(path)
    } else {
    console.log(`file is not exists: ${path}`)
    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()
    }

    // Main Start
    if (config.runsInWidget) {
    let widget = new ListWidget()
    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 = new Size(16, 16)
    imageIco.centerAlignImage()

    titleStack.addSpacer(2)

    let titleTxt = titleStack.addText('코로나-19')
    titleTxt.centerAlignText()
    titleTxt.textColor = Color.white()
    titleTxt.font = Font.boldRoundedSystemFont(20)

    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
  8. julio-kim revised this gist Nov 25, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -32,6 +32,7 @@ const getLevelColor = (count) => {

    if (config.runsInWidget) {
    let widget = new ListWidget()
    widget.url = source
    widget.backgroundColor = new Color(getLevelColor(count))
    widget.refreshAfterDate = new Date(Date.now() + 1000 * 30) // 30 Second

  9. julio-kim revised this gist Nov 24, 2020. No changes.
  10. julio-kim revised this gist Nov 24, 2020. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -5,12 +5,15 @@ 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 count = document.querySelector(baseSelector + 'div.liveNum_today_new ul li:nth-child(1) span.data').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})
    completion({date, count: {
    domestic, overseas
    }})
    `, true)

    let count = parseInt(covid.count)
    let count = parseInt(covid.count.domestic) + parseInt(covid.count.overseas)
    let date = covid.date.replace(/\(|\)/g, '').split(',')[0]

    const getCountSize = (count) => {
  11. julio-kim revised this gist Nov 23, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ const getLevelColor = (count) => {
    if (config.runsInWidget) {
    let widget = new ListWidget()
    widget.backgroundColor = new Color(getLevelColor(count))
    widget.refreshAfterDate = new Date(Date.now() + 1000 * 60 * 60) // 1 Hour
    widget.refreshAfterDate = new Date(Date.now() + 1000 * 30) // 30 Second

    let titleTxt = widget.addText('코로나-19')
    titleTxt.centerAlignText()
  12. julio-kim revised this gist Nov 23, 2020. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ if (config.runsInWidget) {
    titleTxt.textColor = Color.white()
    titleTxt.font = Font.boldRoundedSystemFont(20)

    let countTxt = widget.addText(covid.count)
    let countTxt = widget.addText(count.toString())
    countTxt.url = source
    countTxt.centerAlignText()
    countTxt.textColor = Color.white()
    @@ -51,6 +51,7 @@ if (config.runsInWidget) {
    Script.setWidget(widget)
    Script.complete()
    } else {
    // for Test
    let table = new UITable()

    let row = new UITableRow()
    @@ -60,7 +61,7 @@ if (config.runsInWidget) {

    row = new UITableRow()
    row.addText('확진자수')
    row.addText(covid.count).rightAligned()
    row.addText(count.toString()).rightAligned()
    table.addRow(row)

    table.present()
  13. julio-kim created this gist Nov 23, 2020.
    67 changes: 67 additions & 0 deletions CovidStat.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    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 count = document.querySelector(baseSelector + 'div.liveNum_today_new ul li:nth-child(1) span.data').innerText
    completion({date, count})
    `, true)

    let count = parseInt(covid.count)
    let date = covid.date.replace(/\(|\)/g, '').split(',')[0]

    const getCountSize = (count) => {
    if (count >= 1000) {
    return Device.isPhone() ? 45 : 55
    } else {
    return Device.isPhone() ? 55 : 70
    }
    }

    const getLevelColor = (count) => {
    if (count >= 300) return '#dc143c'
    else if (count < 300 && count >= 100) return '#f05454'
    else return '#0099ff'
    }

    if (config.runsInWidget) {
    let widget = new ListWidget()
    widget.backgroundColor = new Color(getLevelColor(count))
    widget.refreshAfterDate = new Date(Date.now() + 1000 * 60 * 60) // 1 Hour

    let titleTxt = widget.addText('코로나-19')
    titleTxt.centerAlignText()
    titleTxt.textColor = Color.white()
    titleTxt.font = Font.boldRoundedSystemFont(20)

    let countTxt = widget.addText(covid.count)
    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)

    Script.setWidget(widget)
    Script.complete()
    } else {
    let table = new UITable()

    let row = new UITableRow()
    row.isHeader = true
    row.addText(`코로나 확진자 현황 (${date})`)
    table.addRow(row)

    row = new UITableRow()
    row.addText('확진자수')
    row.addText(covid.count).rightAligned()
    table.addRow(row)

    table.present()
    }