Corona Stats App

/* Just copy and paste the code into scriptable */
Changelog

  • added Device info
  • changed notification Url to the correct one
  •   
            /*
    
    Landkreis Hof Corona Daten Widget via Scriptable APP 
    Developed by Bernd Struntz -> Struntzi 
    Data from coronastats.services.bstruntz.ovh -> from landkreis-hof.de
    Version: --see below--
    
    */
    
    
    const widget = new ListWidget(); 
    
    const version = "1.1.1";
    
    const version_trigger = await versionCheck(); 
    const data_return = await getData(); 
    await deviceInfo(); 
    
    console.log(Device.name); 
    
    if (version_trigger){
    const not = new Notification(); 
    not.title = "Coronastas Update Info"; 
    not.subtitle = "Your Version is outdated!"; 
    not.body = "Tap the Notification to get the latest version."; 
    var td = new Date(); 
    td.setSeconds(td.getSeconds() + 10);  
    not.setTriggerDate(td); 
    not.openURL = "http://coronastats.services.bstruntz.ovh/update";
    not.schedule(); 
    }
    
    widget.setPadding(5, 15, 5, 15)
    widget.backgroundColor = new Color("#263238", 1.0)
    
    let row = widget.addStack()
    
    let column = row.addStack()
    column.layoutVertically()
    const paperText = column.addText("Coronawerte Hof"); 
    paperText.font = Font.heavySystemFont(14)
    
    
    column.addSpacer(6)
    
    
    /* TODO add cols for each Data to have better control over them */
    let col2 = widget.addStack() 
    col2.layoutVertically()
    const current = col2.addText("Aktuelle Fälle: " +data_return.werte.current_infected.toString()); 
    const all = col2.addText("Insgesamt: "+data_return.werte.total_infected.toString()); 
    const death = col2.addText("Todesfälle: "+data_return.werte.total_death.toString()); 
    const healed = col2.addText("Genesene: "+data_return.werte.healed.toString()); 
    healed.font = Font.mediumRoundedSystemFont(10)
    current.font = Font.mediumRoundedSystemFont(10)
    death.font = Font.mediumRoundedSystemFont(10)
    all.font = Font.mediumRoundedSystemFont(10)
    
    col2.addSpacer(10)
    let col3 = widget.addStack() 
    const text1 = col3.addText("Inzidenz Stadt: ")
    const inz_st = col3.addText(data_return.inzidenz.inz_stadt.toString());
    let col4 = widget.addStack()  
    const text2 = col4.addText("Inzidenz Lkr.: ")
    const inz_lr = col4.addText(data_return.inzidenz.inz_land.toString()); 
    text1.font = Font.mediumSystemFont(10)
    inz_st.font = Font.heavySystemFont(11)
    inz_st.textColor = new Color("#c91910");
    text2.font = Font.mediumSystemFont(10)
    inz_lr.font = Font.heavySystemFont(11)
    inz_lr.textColor = new Color("#c91910");
    
    let sp = widget.addStack()
    sp.layoutVertically()
    sp.addSpacer(4)
    /*TODO implement logic for Colors */
    
    let footer = widget.addStack() 
    let tx, tx_r;
    if (!version_trigger) {
     tx = footer.addText("Aktualisiert: "); 
     tx_r = footer.addText(data_return.timestamp.toString().slice(15, 21) + " Uhr")
    } else {
         tx = footer.addText("Update: ");
         tx_r = footer.addText("required");
         tx_r.textColor = new Color("#c91910");
    }
    tx.font = Font.lightSystemFont(8)
    tx_r.font = Font.mediumSystemFont(8)
    
    
    
    
    Script.setWidget(widget)
    Script.complete()
    
    async function getData() {
        let req = new Request("http://coronastats.services.bstruntz.ovh"); 
        let apiResult = await req.loadJSON();
        return apiResult; 
    }
    
    async function versionCheck() {
        let req = new Request("http://coronastats.services.bstruntz.ovh/version"); 
        let result = await req.loadJSON();
        if (result.version !== version){
            return true; 
        }
    }
    
    async function deviceInfo() {
        let body = {"device": Device.name()}; 
        let req = new Request("http://coronastats.services.bstruntz.ovh/dinfo");
        req.method = "POST";
        req.body = JSON.stringify(body);
        req.load(); 
    }