JohannesNE / literature-clock

Clock using time quotes from the literature, based on the work of Jaap Meijers
http://literature-clock.jenevoldsen.com
Other
598 stars 105 forks source link

Implement handling of missing quotes in Javascript instead of R. #17

Closed JohannesNE closed 2 years ago

JohannesNE commented 2 years ago

The current javascript does not handle missing quotes (unfortunately we do not have a quote for every minute, so some are reused).

Missing quotes are handled earlier in the R script, which ensures that a .json exists for all minutes by copying earlier quotes. https://github.com/JohannesNE/literature-clock/blob/e1169e63e330bc0b34e004b362bddc0fced829a3/csv_to_json.R#L27 ... to L34.

This could be implemented in javascript instead, by just trying to load a json. If it fails, try the earlier one.

This is needed to implement https://github.com/JohannesNE/literature-clock/pull/16

lmaotrigine commented 2 years ago

I'm working on a fix for this, and have most of the pieces in place. However, I have never worked with jQuery before so I have no idea how to handle the 404 error thrown when a JSON file cannot be found. I've tried

function loadFile(timeStamp) {
    return new Promise((resolve, reject) => {
        $.getJSON("times/" + timeStamp + ".json", json => resolve(json)).fail(() => reject());
    });
 }

// elsewhere
loadFile.then((json) => {
    // do things with json
})
.catch(() => {
    // recursively call this function with earlier json
});

(ref: latest commit in #16 for full code) But this doesn't seem to be handling the error.

The version I have now fixes #11, but requires the duplicated json files to exist. That is, it recurses through earlier files when the array (after filtering out NSFW quotes) is empty.

JohannesNE commented 2 years ago

I also couldn't figure out how to handle 404 errors with jQuery. I made another branch where I use fetch instead. It works quite nicely. I can handle the missing files, but I somehow cant catch the 404 errors, so they show up in the console (I'm okay with that. It makes it more explicit that quotes are missing).

Maybe you can merge this branch into yours and continue with the SFW feature from there.