JSON data from API output to Shortcuts (or textfile via scriptable)

I would like to read game data for my team from the football-data.org API, and present that in a shortcut.

I can get the data out of the API in scriptable, but have no idea how to get that to shortcuts or display it / create an html file to show in a shortcut.

does anyone know a good example or a reference manual to read to get this to work?

1 Like

Did not know this service, but I think you just gave me something nice to play with tonight…

Thank you for mentioning this API!

let me know how you got on :slight_smile:

I plan to use Scriptable though…

:slight_smile: so was I, can get the API to give me data, just can’t get it to display it or save it to file. (just learning Javascript)

Any chance you’ve not read the Shortcuts documentation on utilising web APIs?

As for display, the Quick Look action is the go to for displaying a wide variety of content. Think of it like Preview.

I’ve been able to extract the data through the API, and display it using quicklook in scriptable. And I’ve been able to parse it into json output.

What I’d like to be able to do is run the scriptable script from shortcuts, and display the result in a webview or dialog box or something. I’ve just not found a way to get the result of the api call back to shortcuts.

Process:

  1. Kick of shortcut to get the team’s playing schedule.
  2. Run javascript from shortcut to call the api
  3. Display result in shortcuts (or tableview in scriptable f.e.)

I’ve checked the api docs for shortcuts, and have a working darksky (and other) shortcut. This API works a bit differently though, and I’ve not found a way to access it through a single url,

An example for a team that impressed me last year in the Champions League, AFC Ajax:

const url = 'http://api.football-data.org/v2/teams/678/matches?status=SCHEDULED'
const request = new Request(url)
request.headers = { 'X-Auth-Token': '<YOUR-API-TOKEN>' }
const response = await request.loadJSON()
const table = new UITable()

for (match of response.matches) {
    const row = new UITableRow()
    const date = match.utcDate
    const dateCell = UITableCell.text(date)    
    const game = match.homeTeam.name + " - " + match.awayTeam.name
    const gameCell = UITableCell.text(game)
    row.addCell(dateCell)
    row.addCell(gameCell)
    table.addRow(row)
}

QuickLook.present(table)

Of course the returned data should/can be modified (for example the UTC date of a match), but I hope this gives an idea of how to display the data, in a simple way (also the UI can be modified).

If you were calling a Scriptable script, then you could currently doing it by passing it back as the result of an x-callback-url, or via the clipboard.

If you were just calling the API in Shortcuts, it would just be like this.

But both apps can manipulate the data returned by the API and modify it for display so I would recommend just sticking to one app. Whichever you prefer. I see no benefit to switching between them for something like this.

I’ll check it out, I see where I made the mistake in shortcuts. Did not put in the X-Auth-Token!
Thanks @sylumer!

Exactly the team I wanted :slight_smile:
Thanks Rob

@simonbs Several of the team logo’s linked to in this API are SVG files (while others are PNG). It looks like UITableCell.imageAtURL can’t handle (those) SVG files? (PNG files are fine)

Example of a link that does not work:

https://upload.wikimedia.org/wikipedia/de/7/79/Ajax_Amsterdam.svg

Would it be hard to support SVG files as well?

It definitely wouldn’t be straightforward to support SVGs. I’ll note it down as something to look into in the future but for now I suggest to convert the SVGs. You can use an API such as CloudConvert to convert the images on demand.

1 Like

The free plan seems to be limited to 25 conversions in 1 day, but maybe I can convert them once and read them from a local cache within Scriptable, instead of downloading/converting them on demand.

Yet another “project” in my GTD system… :wink:

Yes, you should probably store them locally. That should be pretty easy to do with the FileManager.

Works for me! :smile:

1 Like

Or this:

Once again thank you for introducing me to this API!

I really enjoy(ed) experimenting with it.

Only wish the data were of a bit higher quality (many shirt numbers seem to be missing):

This is definitely not perfect, but it looks a bit better:

(Putting square transparent images in the cache instead of some rectangular ones from the database helps a bit as well)

Wow! Nice!
Now I will have to see if I can reproduce what you did! :wink:
Any pointers? (Or script examples?)

I did not know, but actually Siri already does some basics… (schedule, standings)

Did you try that?