Cached Astronomy Picture of the Day

This flow will download the latest Astronomy Picture of the Day (APOD) from Nasa.gov and caches in memory.

It hits the API once every 24 hours, so you don't have to worry about reaching your quota.

This flow demonstrates a technique in caching data that can be used for other sites as well - for example, you can create your own "xxx of the day" from any site that offers a random API by simply calling that random API once every 24 hours (you can change this interval) and caching the result. Calls to the API will read from the cached version.

NOTE: to use this flow you must obtain a free API key from data.nasa.gov.

[
    {
        "id": "e50a0494.7b3488",
        "type": "http in",
        "z": "87767553.7027d8",
        "name": "",
        "url": "/api/apod",
        "method": "get",
        "swaggerDoc": "",
        "x": 133,
        "y": 811,
        "wires": [
            [
                "20635998.ae8176"
            ]
        ]
    },
    {
        "id": "8dc420cc.a5825",
        "type": "http response",
        "z": "87767553.7027d8",
        "name": "",
        "x": 687,
        "y": 838,
        "wires": []
    },
    {
        "id": "ad882564.cc52b8",
        "type": "inject",
        "z": "87767553.7027d8",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 140,
        "y": 912,
        "wires": [
            [
                "20635998.ae8176"
            ]
        ]
    },
    {
        "id": "4a591a74.c05b14",
        "type": "debug",
        "z": "87767553.7027d8",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 719,
        "y": 926,
        "wires": []
    },
    {
        "id": "85b43620.7cc438",
        "type": "http request",
        "z": "87767553.7027d8",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "url": "https://api.nasa.gov/planetary/apod?api_key=YOUR_APIKEY_HERE",
        "tls": "",
        "x": 419,
        "y": 1037,
        "wires": [
            [
                "d449502a.c980b"
            ]
        ]
    },
    {
        "id": "d449502a.c980b",
        "type": "function",
        "z": "87767553.7027d8",
        "name": "Store latest APOD",
        "func": "var latestApod = global.get('latestApod') || {};\nglobal.set('latestApod', msg.payload);\nmsg.reset = true;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 644.5,
        "y": 1038,
        "wires": [
            []
        ]
    },
    {
        "id": "20635998.ae8176",
        "type": "function",
        "z": "87767553.7027d8",
        "name": "Get APOD from cache",
        "func": "msg.payload = global.get('latestApod');\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 450.5,
        "y": 861,
        "wires": [
            [
                "8dc420cc.a5825",
                "4a591a74.c05b14"
            ]
        ]
    },
    {
        "id": "5e9ee18e.748ab",
        "type": "inject",
        "z": "87767553.7027d8",
        "name": "Trigger APOD",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "86400",
        "crontab": "",
        "once": true,
        "x": 164.5,
        "y": 1039,
        "wires": [
            [
                "85b43620.7cc438"
            ]
        ]
    }
]
sax1johno

Flow Info

created 10 months, 3 weeks ago

Node Types

Core
  • debug (x1)
  • function (x2)
  • http in (x1)
  • http request (x1)
  • http response (x1)
  • inject (x2)

Tags

  • nasa
  • cache
  • astronomy
  • images
  • daily
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option