Trying to show only latest updates from a JSON url


Took and example of pharsing a JSON form a url and create a webview with links that open up and safari. Seems to work great but im trying to add a update/refresh timer and have it just show the items that have been added since last refresh? Can this be done via scriptable app?

Code added below :slight_smile:

    let url = ""
let req = new Request(url)
let json = await req.loadJSON()
let allItems = json.products
items = json.products
var insert = ""
for (item of items) {
  var title =
  var imageURL = item.thumbnailUrl
  var releaseDate = item.releaseDate
  var quote = item.productPullQuote
  var link = item.url
  var price = item.productWebsites[0].price
  var soldout = item.soldOut
  var raffleURL = item.urlKey

//   for (pw of item.productWebsites) {
//     var price = pw[0].price
//     }
var ts = new Date(releaseDate);
// console.log(ts.toDateString());

rdate = ts.toLocaleString()

if (soldout == false) {
  var status = "raffle open"
else {
  var status = "raffle over"
  insert += `${title}`
  insert += `<img src="${imageURL}" width="95%"><br>`
  insert += `£${price}<p>`
  insert += `${rdate}<p>`
  insert += `${status}<p>`
  insert += `<a href="${raffleURL}">Raffle Link</a>`
  insert += `<p>`
   insert += `<hr>`
//   insert += `${link}<p>`
//   insert += `<h3>${quote}</h3><p>`
//   insert += `${releaseDate}<p>`
//  console.log(ts)
// console.log(title)

let baseHTML = `
<!DOCTYPE html>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
body { white-space: pre; font-family: monospace; background-color: #fff;}
span { background-color: #fff; }
th { color: #000}
<table style="width:100%">
    <th><h1>END Raffles</h1></th>
// load the view
let view = new WebView();
// view.loadHTML(baseHTML);
// await view.present();
WebView.loadHTML(baseHTML, null, new Size(0, 1800))

Please surround your code with three backticks on their own line. Then the code will be easier to read :wink:

Like this:

Your code here
With multiple lines

Sorry was trying to that, clearly didn’t work the first time :man_facepalming:t2: Hope it formats better now.

Thank you

1 Like

Sorry, I completely forgot to welcome you here in this community. :man_facepalming:

Hi! :wave: :wink:

The first idea that comes to my mind would be to handle that refreshing in the browser itself.

Scriptable has currently no official timer API. There is a workaround using a WebView and setTimeout() but it’s not really reliable. the Timer class since version 1.4.
But as you use a WebView to display the result, you could put everything into that website and fetch the JSON every 10s or so and then directly display it.

1 Like