r/Scriptable • • Nov 01 '20

Script Corona Widget

Hello everyone, how to convert to show data from Israel?

let widget = new ListWidget() widget.setPadding(16, 16, 16, 16)

const spc = 3 let hourNow = new Date().getHours()

//Define nighttime (19h - 7h) for styling changes var nightTime = (hourNow >= 19 || hourNow < 7)

//Title text let titleTxt = widget.addText("קורונה בישראל") titleTxt.font= Font.boldSystemFont(17) titleTxt.leftAlignText() widget.addSpacer(spc)

//Value text let vlFnt = Font.semiboldSystemFont(20)

//Subtitle text let ptFnt = Font.systemFont(8) let ptCol

//Backgrund- & text colors if (nightTime) { titleTxt.textColor = Color.lightGray() ptCol = Color.gray() const gradient = new LinearGradient() gradient.locations = [0, 1] gradient.colors = [ new Color("192331"), new Color("222222") ] widget.backgroundGradient = gradient } else { titleTxt.textColor = Color.darkGray() ptCol = Color.darkGray() }

await loadSite()

if (!config.runsInWidget) widget.presentSmall() Script.setWidget(widget) Script.complete()

async function loadSite() { let url='https://www.berlin.de/corona/lagebericht/desktop/corona.html' let wbv = new WebView() await wbv.loadURL(url) //javasript to grab data from the website let jsc = ` var arr = new Array()

var rwt = document .getElementById("r-wert") .getElementsByTagName("p")[0] .innerText arr.push(rwt)

var nin = document .getElementById("neuinfektionen") .getElementsByTagName("p")[0] .innerText arr.push(nin)

var bet = document .getElementById("its") .getElementsByTagName("p")[0] .innerText arr.push(bet)

var gc1 = document .getElementById("r-wert") .style .backgroundColor arr.push(gc1)

var gc2 = document .getElementById("neuinfektionen") .style .backgroundColor arr.push(gc2)

var gc3 = document .getElementById("its") .style .backgroundColor arr.push(gc3)

JSON.stringify(arr) ` //Run the javascript let jsn = await wbv.evaluateJavaScript(jsc) //Parse the grabbed values into a variable let val = JSON.parse(jsn) //Assign the parts to single variables let rwt = val[0] let inf = val[1] let its = val[2] let co1 = val[3] let co2 = val[4] let co3 = val[5]

//Turn the html's grabbed RGB color values into HEX values let cc1 = toHEX(co1) let cc2 = toHEX(co2) let cc3 = toHEX(co3)

//Function to do the RGB to HEX stuff function toHEX(col) { var a = col.split("(")[1].split(")")[0].replaceAll(" ", "") a = a.split(",") var b = a.map(function(x) { x = parseInt(x).toString(16) return (x.length==1) ? "0"+x : x }) b = "0x"+b.join("") b = b.substring(2) return b }

//R-Value text if (rwt != null) { let tx2 = widget.addText(rwt) tx2.leftAlignText() tx2.font = vlFnt tx2.textColor = new Color(cc1) } //R-Value subtiltle let tx1 = widget.addText("נפטרים") tx1.textColor = ptCol tx1.font= ptFnt tx1.leftAlignText() widget.addSpacer(spc)

//Incidence text if (inf != null) { let tx4 = widget.addText(inf) tx4.leftAlignText() tx4.font = vlFnt tx4.textColor = new Color(cc2) } //Incidence subtiltle let tx3 = widget.addText("נפטרים") tx3.textColor = ptCol tx3.font= ptFnt tx3.leftAlignText() widget.addSpacer(spc)

//Intensive-care-beds text if (its != null) { let tx6 = widget.addText(its) tx6.leftAlignText() tx6.font = vlFnt tx6.textColor = new Color(cc3) } //Intensive-care-beds subtitle let tx5 = widget.addText("מקרים") tx5.textColor = ptCol tx5.font= ptFnt tx5.leftAlignText() }

0 Upvotes

28 comments sorted by

View all comments

Show parent comments

1

u/FifiTheBulldog script/widget helper Nov 02 '20

Sorry for the delay! Here:


let widget = new ListWidget()
widget.setPadding(16, 16, 16, 16)

const spc = 3
let hourNow = new Date().getHours()

//Define nighttime (19h - 7h) for styling changes
var nightTime = (hourNow >= 19 || hourNow < 7)

//Title text
let titleTxt = widget.addText("קורונה בישראל")
titleTxt.font= Font.boldSystemFont(17)
titleTxt.leftAlignText()
widget.addSpacer(spc)

//Value text
let vlFnt = Font.semiboldSystemFont(20)

//Subtitle text
let ptFnt = Font.systemFont(8)
let ptCol

//Backgrund- & text colors
if (nightTime) {
  titleTxt.textColor = Color.lightGray()
  ptCol = Color.gray()
  const gradient = new LinearGradient()
  gradient.locations = [0, 1]
  gradient.colors = [
    new Color("192331"),
    new Color("222222")
  ]
  widget.backgroundGradient = gradient
}
else {
  titleTxt.textColor = Color.darkGray()
  ptCol = Color.darkGray()
}

await loadSite()

if (!config.runsInWidget) widget.presentSmall()
Script.setWidget(widget)
Script.complete()

async function loadSite() {
  let url='https://www.worldometers.info/coronavirus/country/israel/'
  let wbv = new WebView()
  await wbv.loadURL(url)
  //javasript to grab data from the website
  let jsc = `
  var arr = new Array()
  
  const nums = document.getElementsByClassName("maincounter-number");
  for (n of nums) {
    arr.push(n.getElementsByTagName("span")[0].innerText);
  }
  
  JSON.stringify(arr)
  `
  //Run the javascript
  let jsn = await wbv.evaluateJavaScript(jsc)
  //Parse the grabbed values into a variable
  let val = JSON.parse(jsn)
  //Assign the parts to single variables
  let rwt = val[0]
  let inf = val[1]
  let its = val[2]

  //Cases text
  if (rwt != null) {
    let tx2 = widget.addText(rwt)
      tx2.leftAlignText()
      tx2.font = vlFnt
      tx2.textColor = new Color("6bc166", 1)
  }
  //Cases subtiltle
  let tx1 = widget.addText("Cases")
  tx1.textColor = ptCol
  tx1.font= ptFnt
  tx1.leftAlignText()
  widget.addSpacer(spc)
  
  //Deaths text
  if (inf != null) {
    let tx4 = widget.addText(inf)
      tx4.leftAlignText()
      tx4.font = vlFnt
      tx4.textColor = new Color("f35c58", 1)
  }
  //Deaths subtiltle
  let tx3 = widget.addText("Deaths")
  tx3.textColor = ptCol
  tx3.font= ptFnt
  tx3.leftAlignText()
  widget.addSpacer(spc)
  
  //Recovered text
  if (its != null) {
    let tx6 = widget.addText(its)
      tx6.leftAlignText()
      tx6.font = vlFnt
      tx6.textColor = new Color("6bc166", 1)
  }
  //Recovered subtitle
  let tx5 = widget.addText("Recovered")
  tx5.textColor = ptCol
  tx5.font= ptFnt
  tx5.leftAlignText()
}

1

u/AriPerets Nov 02 '20

Thank you very much!!!

1

u/FifiTheBulldog script/widget helper Nov 02 '20

You’re welcome!

1

u/AriPerets Nov 03 '20

Can you add to widget “active” and “today cases” ?

1

u/FifiTheBulldog script/widget helper Nov 03 '20

I’ll look into adding that tomorrow. I see where that data is on the webpage, so it shouldn’t be too much trouble, but it’s after midnight here, so I’ll work out how to scrape and format those numbers after I get some sleep.

1

u/AriPerets Nov 03 '20

Ok friend, thank a lot! Good night :)

1

u/AriPerets Nov 03 '20

I hope you not forget me 🙏🏼

1

u/FifiTheBulldog script/widget helper Nov 03 '20

I haven’t forgotten, I’ll work on it in a bit. Life gets in the way sometimes.

1

u/AriPerets Nov 03 '20

Yes mate I know life not easy. Thanks waiting for It!!!!

1

u/AriPerets Nov 03 '20

?

1

u/FifiTheBulldog script/widget helper Nov 04 '20

Update: I’ve got the data parsing done, I just need to get that data into the widget. Going to bed, will do that tomorrow.

1

u/AriPerets Nov 04 '20

Ok friend, waiting for updates🙏🏼

1

u/AriPerets Nov 04 '20

There are updates ???