Advertisement
Guest User

Covid Stats Widget

a guest
Apr 8th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Version 1.2a
  2. //This widget shows daily current statistics of COVID-19 in Ukraine.
  3. //Displayed (for today):
  4. //- the number of identified patients today (updated in the morning around 9:00);
  5. //- the total number of patients;
  6. //- number of deaths;
  7. //- number of recovered.
  8. //Ukraine language.
  9.  
  10. //This Scriptable Wodget is coded by eXtendedZero.
  11. //Very Thanks all known and unknown people whose codes parts I used. :-)
  12.  
  13. // Variables used by Scriptable.
  14. // These must be at the very top of the file. Do not edit.
  15.  
  16. //For country(language) setting use Parameter when Run Script.
  17. //UA  - Ukraine and ukrainian language,
  18. //UK - United Kingdom and english language,
  19. //IT - Italy and italy language,
  20. //PL - Poland and poland language,
  21. //DE - Germany and germany language.
  22.  
  23. var langId = args.widgetParameter
  24. var id = 1 //default UK
  25.  
  26. switch(langId)
  27.   {
  28.   case "UA":
  29.     id = 0
  30.     break;    
  31.   case "UK":
  32.     id = 1
  33.     break;
  34.   case "IT":
  35.     id = 2
  36.     break;
  37.   case "PL":
  38.     id = 3
  39.     break;
  40.   case "DE":
  41.     id = 4
  42.     break;
  43.   }
  44.  
  45. var lang = [{
  46.     country: "Ukraine",
  47.     cName: "Україна",
  48.     total: "Всього",
  49.     today: "Сьогодні",
  50.     cases: "Захворіло",
  51.     death: "Померло",
  52.     recovered: "Одужало",
  53.     statistic: "Статистика"
  54.     },    
  55.    {country: "UK",
  56.     cName: "England",
  57.     total:"Total",
  58.     today: "Today",
  59.     cases: "Cases",
  60.     death: "Deaths",
  61.     recovered: "Recovered",
  62.     statistic: "Statistic"
  63.     },
  64.     {country: "Italy",
  65.     cName: "Italia",
  66.     total: "Totale",
  67.     today: "Oggi",
  68.     cases: "Contagiati",
  69.     death: "Decessi",
  70.     recovered: "Guarriti",
  71.     statistic: "Statistica"
  72.     },
  73.     {
  74.     country: "Poland",
  75.     cName: "Polska",
  76.     total: "Ogólem",
  77.     today: "Dzisiaj",
  78.     cases: "Zakażenia",
  79.     death: "Zgony",
  80.     recovered: "Wyzdrowienia",
  81.     statistic: "Statystyka"
  82.     },
  83.     {
  84.     country: "Germany",
  85.     cName: "Deutschland",
  86.     total:"Gesamt",
  87.     today: "Heute",
  88.     cases: "Fälle",
  89.     death: "Todesfälle",
  90.     recovered: "Genesene",
  91.     statistic: "Statistik"
  92.     }
  93.     ];
  94.  
  95. const url = `https://coronavirus-19-api.herokuapp.com/countries/${lang[id].country}`
  96. const req = new Request(url)
  97. const res = await req.loadJSON()
  98.  
  99. if (config.runsInWidget) {
  100.   // create and show widget
  101.   let widget = await createWidget(
  102.   "COVID-19",
  103.   lang[id].cName,
  104.  
  105.   lang[id].cases+' '+ ' '+new Intl.NumberFormat(`en-GB`).format(res.todayCases),
  106.  
  107.   lang[id].death+' '+new Intl.NumberFormat(`en-GB`).format(res.todayDeaths),
  108.  
  109.   lang[id].cases+' '+new Intl.NumberFormat(`en-GB`).format(res.cases),
  110.  
  111.   lang[id].death+ ' '+new Intl.NumberFormat(`en-GB`).format(res.deaths),
  112.  
  113.   lang[id].recovered+ ' '+new Intl.NumberFormat(`en-GB`).format(res.recovered),
  114.  
  115.   "#000000")
  116.  
  117.   Script.setWidget(widget)
  118.   Script.complete()
  119. } else {
  120.   // make table
  121.   let table = new UITable()
  122.  
  123.   // add header
  124.   let row = new UITableRow()
  125.   row.isHeader = true
  126.   row.addText(lang[id].statistic+` Covid-19 - ${lang[id].cName}`)
  127.   table.addRow(row)
  128.  
  129.   // fill data
  130.   table.addRow(createRow(lang[id].today,""))
  131.   table.addRow(createRow(lang[id].cases, res.todayCases))
  132.   table.addRow(createRow(lang[id].death, res.todayDeaths))
  133.  
  134.   table.addRow(createRow(lang[id].total, ""))
  135.   table.addRow(createRow(lang[id].cases, res.cases))
  136.   table.addRow(createRow(lang[id].death, res.deaths))
  137.   table.addRow(createRow(lang[id].recovered, res.recovered))
  138.  
  139.   if (config.runsWithSiri)
  140.     Speech.speak(`There are ${res.cases} cases in ${lang[id].country}, and ${res.todayCases} cases today.`)
  141.  
  142.   // present table
  143.   table.present()
  144. }
  145.  
  146. function createRow(title, number) {
  147.   let row = new UITableRow()
  148.   row.addText(title)
  149.   row.addText(number.toString()).rightAligned()
  150.   return row
  151. }
  152.  
  153. async function createWidget(pretitle, title, today, todayDead, total, dead, recov, color) {
  154.  
  155.   let w = new ListWidget()
  156.      
  157.   w.backgroundColor = new Color(color)
  158.   w.setPadding(0, 14, 0, 10)
  159.  
  160.   let row = w.addStack()
  161.  
  162.   let column = row.addStack()
  163.   column.layoutVertically()
  164.  
  165.   let preText = column.addText(pretitle)
  166.   preText.textColor = Color.white()
  167.   preText.textOpacity = 0.8
  168.   preText.font=Font.systemFont(14)
  169.   row.addSpacer(1)
  170.  
  171.   let titleTxt = column.addText(title)
  172.   titleTxt.textColor = Color.white()
  173.   titleTxt.textOpacity = 0.8
  174.   titleTxt.font=Font.systemFont(16)
  175.   row.addSpacer(1)
  176.  
  177.   let req = new Request("https://i.imgur.com/OZLYIp5.png")
  178.   let icon = await req.loadImage()
  179.  
  180.   row.layoutHorizontally()
  181.   row.addSpacer(16)
  182.  
  183.   let iconImg = row.addImage(icon)
  184.   iconImg.imageSize = new Size(36, 36)
  185.  
  186.   w.addSpacer(0)
  187.   let todayTxtLabel = w.addText(lang[id].today)
  188.   todayTxtLabel.textColor = Color.white()
  189.   todayTxtLabel.textOpacity = 1.0
  190.   todayTxtLabel.font=Font.boldSystemFont(12)
  191.   todayTxtLabel.centerAlignText()
  192.  
  193.   w.addSpacer(0)
  194.   let todayTxt = w.addText(today)
  195.   todayTxt.textColor = Color.white()
  196.   todayTxt.textOpacity = 1.0
  197.   todayTxt.font=Font.systemFont(12)
  198.  
  199.   w.addSpacer(0)
  200.   let todayTxtDead = w.addText(todayDead)
  201.   todayTxtDead.textColor = Color.white()
  202.   todayTxtDead.textOpacity = 1.0
  203.   todayTxtDead.font=Font.systemFont(12)
  204.  
  205.   w.addSpacer(0)
  206.   let totalTxtLabel = w.addText(lang[id].total)
  207.   totalTxtLabel.textColor = Color.white()
  208.   totalTxtLabel.textOpacity = 0.8
  209.   totalTxtLabel.font=Font.boldSystemFont(12)
  210.   totalTxtLabel.centerAlignText()
  211.  
  212.   w.addSpacer(0)
  213.   let totalTxt = w.addText(total)
  214.   totalTxt.textColor = Color.white()
  215.   totalTxt.textOpacity = 0.8
  216.   totalTxt.font = Font.systemFont(12)
  217.  
  218.   w.addSpacer(0)
  219.   let deadTxt = w.addText(dead)
  220.   deadTxt.textColor = Color.white()
  221.   deadTxt.textOpacity = 0.8
  222.   deadTxt.font = Font.systemFont(12)
  223.  
  224.   w.addSpacer(0)
  225.   let recovTxt = w.addText(recov)
  226.   recovTxt.textColor = Color.white()
  227.   recovTxt.textOpacity = 0.8
  228.   recovTxt.font = Font.systemFont(12)
  229.  
  230.   return w
  231. }
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement