Sub Main(ByVal parameters As Object)
'Moskus 2020
Dim devID As Integer = parameters.ToString
Dim useHTMLtable As Boolean = True
Dim url As String = "https://www.vg.no/spesial/2020/corona-viruset/data/norway-table-overview/"
Dim source As String = ""
Try
Using client = New System.Net.WebClient
Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls12
client.Encoding = System.Text.Encoding.UTF8
source = client.DownloadString(url)
End Using
Catch ex As Exception
hs.WriteLog("CoronaScript", "Net Feil: " & ex.Message)
End Try
If source = "" Then
hs.WriteLog("CoronaScript", "Got no response from url: " & url)
Exit Sub
End If
Dim json = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Object)(source)
Dim output As String = ""
Try
Dim confirmed As Integer = json("totals")("confirmed")
If useHTMLtable Then
output &= "<table border=0 cellspacing=0 cellpadding=0>"
output &= "<tr><td><b>Totalt</b></td><td align='right'>" & json("totals")("confirmed") & "</td><td align='right'>" & json("totals")("dead") & "</td><td align='right'>" & json("totals")("recovered") & "</td></tr>"
For i As Integer = 0 To json("cases").Count - 1
output &= "<tr><td>" & json("cases")(i)("name") & "</td><td align='right'>" & json("cases")(i)("confirmed") & "</td><td align='right'>" & json("cases")(i)("dead") & "</td><td align='right'>" & json("cases")(i)("recovered") & "</td></tr>"
Next
output &= "</table>"
Else
output &= "<b>Totalt: " & json("totals")("confirmed") & " / " & json("totals")("dead") & " / " & json("totals")("recovered") & "</b><br>"
For i As Integer = 0 To json("cases").Count - 1
output &= "• " & json("cases")(i)("name") & ": " & json("cases")(i)("confirmed") & " / " & json("cases")(i)("dead") & " / " & json("cases")(i)("recovered") & "<br>"
Next
End If
hs.SetDeviceString(devID, output, False)
hs.SetDeviceValueByRef(devID, confirmed, True)
Catch ex As Exception
hs.WriteLog("CoronaScript", "Net Feil: " & ex.Message)
End Try
End Sub
Trigger: Kjøres hvert 5. minutt
Parameter: DeviceIDen til en virtuell device som skal holde verdien.
Du kan endre variabelen "useHTMLtable" til False hvis du vil ha det i HStouch, for HStouch viser ikke pene HTML-tabeller...
Oppdatert med data fra RapidAPI siden jeg ble lei av at VG er så dårlig på å oppdatere tallene sine:
Sub Main(ByVal parameters As Object)
'Moskus 2020
Dim devID As Integer = parameters.ToString
Dim url As String = "https://covid-193.p.rapidapi.com/statistics?country=Norway"
Dim source As String = ""
Try
Using client = New System.Net.WebClient
'TLS1.2 and encoding (UTF8)
Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls12
client.Encoding = System.Text.Encoding.UTF8
'Headers
client.Headers.Set("x-rapidapi-host", "covid-193.p.rapidapi.com")
client.Headers.Set("x-rapidapi-key", "DIN_RAPIDAPI_KEY_HER")
'GET the url
source = client.DownloadString(url)
End Using
Catch ex As Exception
hs.WriteLog("CoronaScript", "Net Feil: " & ex.Message)
End Try
If source = "" Then
hs.WriteLog("CoronaScript", "Got no response from url: " & url)
Exit Sub
End If
Try
Dim json = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Object)(source)
Dim confirmed As Integer = json("response")(0)("cases")("total")
Dim output As String = ""
output &= "<b>Aktive: " & json("response")(0)("cases")("active") & " (" & json("response")(0)("cases")("new") & ") " & "</b><br>"
output &= "Totalt: " & json("response")(0)("cases")("total") & "<br>"
output &= "Dødsfall: " & json("response")(0)("deaths")("total") & " (" & IIf(json("response")(0)("deaths")("new").ToString = "", "0", json("response")(0)("deaths")("new")) & ") " & "<br>"
output &= "Testede: " & json("response")(0)("tests")("total") & "<br>"
output &= "<i>Sist oppdatert: " & json("response")(0)("time") & "</i><br>"
hs.SetDeviceString(devID, output, False)
hs.SetDeviceValueByRef(devID, confirmed, True)
Catch ex As Exception
hs.WriteLog("CoronaScript Error", "Parsefeil: " & ex.Message)
End Try
End Sub
Kjøres nå hver time, jeg vet ikke begrensningene hos RapidAPI.