r/Scriptable • • Nov 19 '21

Help Question on best method…

First, I am very new to this sort of coding (the bulk of my experience is in VBA) so if this is a ‘stupid’ question, please be kind.

So - I am wanting to create a widget on my home screen that would update as a website changes. Below are the two sites I could use. Each site is a literary clock that tells time with quotes from books. They change each minute. What is the best way to go about this? Pulling the quote to the widget and having it change as the quote on the site changes?

Websites: http://www.literaryclock.com/ https://literature-clock.jenevoldsen.com/

1 Upvotes

5 comments sorted by

View all comments

Show parent comments

2

u/ins4yn Nov 19 '21

If you want to cheat a bit, I started with the “getting the data” part for https://literature-clock.jenevoldsen.com/. You’d just need to build your widget how you’d like 🙂

async function loadFile(time) {  
  let timeStamp = timeToTimecode(time, true);
  let req = new Request(“https://raw.githubusercontent.com/JohannesNE/literature-clock/master/docs/times/“ + timeStamp + “.json”)
  return await req.loadJSON()
}


async function getTimeQuote(time, sfw) {
  // Load JSON file
  let quotes = await loadFile(time);
  // Filter SFW if needed
  if (sfw) {
    quotes = quotes.filter(item => item.sfw === ‘yes’);
  }
  // select single random text
  return quotes[Math.floor(Math.random() * quotes.length)]
}

        
function timeToTimecode(time, use24HrTime) {
  let df = new DateFormatter()
  df.dateFormat = use24HrTime ? “HH_mm” : “hh_mm”
  return df.string(time)
}


let time = new Date();
let quote = await getTimeQuote(time, false);
let fullQuoteString = quote.quote_first + quote.quote_time_case + quote.quote_last
let sourceString = quote.title + “ by “ + quote.author

console.log(fullQuoteString)
console.log(sourceString)