September 24, 2020, 01:48 PM
hunter62Any java / json / api experts in here?
Dont ask my why, but I have been using shortcuts in iOS and just got into scriptable, another cool app to customize things.
The problem, I am no good at it.
I am trying to make a widget for my phone that updates based on an api with every county in the USA listed. I had this working with a different link that only had results for TN as a whole, so it was easy enough to sort. But now that I want it to just pull very specific data from the json, I am way out of my league.
Here is the NEW api.
https://covid19-us-api.herokuapp.com/countyHere is the widget I was using that was for a different api, but it worked. I want to update it to give me the total, confirmed, and last updated for my county from the new link. But I have no idea where to start, or what needs to go where. I am assuming I can use some kind of .filter. I know there are probably forums on this stuff, but I am so new I dont think I could even understand what they are telling me.
let covid = new Request('https://api.covidtracking.com/v1/states/tn/current.json')
let positive = parseInt(await covid.loadJSON().then(pos => pos.positive)).toLocaleString('en')
let increases = parseInt(await covid.loadJSON().then(pos => pos.positiveIncrease)).toLocaleString('en')
let d = new Date(await covid.loadJSON().then(update => update.lastUpdateEt))
let ff = new DateFormatter()
ff.dateFormat = 'MM-dd'
let frmt = ff.string(d)
var widget = new ListWidget()
let head = widget.addText('Tennessee ' + frmt)
head.font = Font.systemFont(30)
head.textColor = new Color('f77f00')
widget.addSpacer (20)
let covid19 = widget.addText('COVID-19')
covid19.font = Font.systemFont(18)
covid19.textColor = new Color('#ffffff')
widget.backgroundColor = new Color('#58595B')
let increase = widget.addText(increases + ' New Cases')
increase.font = Font.systemFont(18)
increase.textColor = new Color('#ffffff')
let cases = widget.addText( positive + ' Total Cases')
cases.font = Font.systemFont(18)
cases.textColor = new Color('#ffffff')
Script.setWidget(widget)
September 24, 2020, 02:13 PM
senza nomeThat looks like Java
script to me.
September 24, 2020, 08:11 PM
4x5I'm not familiar with scriptable, but this is how I would do it in javascript using jquery:
$(document).ready(function(){
$.get("https://covid19-us-api.herokuapp.com/county", function(data, status){
// this filters the returned data to get only the records for Shelby County, TN.
// the result of this filter operation is an array, which should only have one element in it
var county = data.message.filter(item => item.state_name == 'Tennessee' && item.county_name == 'Shelby');
// inspect the data in the first (and hopefully only) element in the filtered array
var confirmedCases = county[0].confirmed;
var newCases = county[0].new;
var lastUpdated = county[0].last_update;
alert('confirmed cases: ' + confirmedCases + ' new cases: ' + newCases + ' last updated: ' + lastUpdated);
});
});
The only part of this that is jquery-specific if the call to 'get' the data, and the document.ready function, which is executed when the page loads.