Author Topic: Get the NASA Pic of the Day with NASA's API!  (Read 3717 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Get the NASA Pic of the Day with NASA's API!
« on: July 28, 2020, 11:07:39 am »
NASA has some pretty awesome photographs and I'm sure you all would like to see them. Below is some code that will allow you to grab the pic of the day from NASA! Could be good for someone's screensaver application written in QB64. Here is a screenshot:
 
nasaapi.png


And here is the code using their DEMO API key which is good for 30 requests per hour per IP address:
Code: QB64: [Select]
  1. _TITLE "NASA Pic of the Day API"
  2.  
  3. DIM i&
  4.  
  5. i& = NASApod
  6.  
  7. IF i& <> 0 THEN
  8.     SCREEN i&
  9.  
  10. FUNCTION NASApod&
  11.     DIM URL AS STRING
  12.     DIM URLFile AS STRING
  13.     DIM nasapic AS STRING
  14.     DIM a%
  15.     URLFile = "nasapod"
  16.     URL = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"
  17.     a% = API_request(URL, URLFile)
  18.     DIM U AS INTEGER
  19.     U = FREEFILE
  20.     OPEN URLFile FOR BINARY AS #U
  21.     IF LOF(U) <> 0 THEN
  22.         nasapic = SPACE$(LOF(U))
  23.         GET #U, , nasapic
  24.     ELSE
  25.         CLOSE #U
  26.         KILL URLFile
  27.         NASApod = 0
  28.         EXIT FUNCTION
  29.     END IF
  30.     CLOSE #U
  31.     KILL URLFile
  32.     nasapic = MID$(nasapic, INSTR(nasapic, CHR$(34) + "url" + CHR$(34) + ":") + 7)
  33.     nasapic = LEFT$(nasapic, LEN(nasapic) - 3)
  34.     URLFile = "nasapod.jpg"
  35.     URL = nasapic
  36.     a% = API_request(URL, URLFile)
  37.     OPEN URLFile FOR BINARY AS #U
  38.     IF LOF(U) <> 0 THEN
  39.         NASApod = _LOADIMAGE(URLFile, 32)
  40.     ELSE
  41.         CLOSE #U
  42.         KILL URLFile
  43.         NASApod = 0
  44.     END IF
  45.     CLOSE #U
  46.     KILL URLFile
  47.  
  48.  
  49.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  50.  
  51. FUNCTION API_request (URL AS STRING, File AS STRING)
  52.     API_request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
« Last Edit: July 28, 2020, 11:10:40 am by SpriggsySpriggs »
Shuwatch!

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Get the NASA Pic of the Day with NASA's API!
« Reply #1 on: July 28, 2020, 11:22:07 am »
Perhaps you could keep all these API demos in the one thread? It's just the same code over and over with slight variations.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Get the NASA Pic of the Day with NASA's API!
« Reply #2 on: July 28, 2020, 12:00:36 pm »
This is awesome! But I suggest putting an Inkey$ loop at the end so it doesn't say to press any key to continue, which takes away part of the picture. In the _TITLE you can say "Press Esc to end." or something like that. Great job though!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get the NASA Pic of the Day with NASA's API!
« Reply #3 on: July 28, 2020, 12:04:33 pm »
Perhaps you could keep all these API demos in the one thread? It's just the same code over and over with slight variations.
@luke What one thread? I'm unaware of any specific place to put these. And yes, they are largely the same code over and over because I'm reusing the same functions over and over but each API has different requirements for Headers and different response types that have to be handled differently.
« Last Edit: July 28, 2020, 12:14:38 pm by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Get the NASA Pic of the Day with NASA's API!
« Reply #4 on: July 28, 2020, 12:11:07 pm »
This is awesome! But I suggest putting an Inkey$ loop at the end so it doesn't say to press any key to continue, which takes away part of the picture. In the _TITLE you can say "Press Esc to end." or something like that. Great job though!

SLEEP is perfect for that, then you might say "Press any to end." in title or you could give SLEEP an argument to stop sleeping (and shut down) after so many seconds.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get the NASA Pic of the Day with NASA's API!
« Reply #5 on: July 28, 2020, 12:11:56 pm »
This is awesome! But I suggest putting an Inkey$ loop at the end so it doesn't say to press any key to continue, which takes away part of the picture. In the _TITLE you can say "Press Esc to end." or something like that. Great job though!
@SierraKen I figured that if anyone wanted to they could edit as they pleased. I was just giving the absolute most basic example I could.
Shuwatch!

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Get the NASA Pic of the Day with NASA's API!
« Reply #6 on: July 29, 2020, 04:35:27 am »
You can put them in a thread called "Calling random API services" or similar. I'm sure you can come up with a name.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Get the NASA Pic of the Day with NASA's API!
« Reply #7 on: July 29, 2020, 07:16:31 am »
You can put them in a thread called "Calling random API services" or similar. I'm sure you can come up with a name.

@luke @SpriggsySpriggs @bplus bplus has suggested that these API posts of Spriggsy could be in the Library (Informatics), and I have these on my ToDoList as possibles.  I could create a post there with the URL references to all of these posts.  Is this a solution that people would favour?

Marked as best answer by SpriggsySpriggs on July 29, 2020, 10:45:24 am

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Get the NASA Pic of the Day with NASA's API!
« Reply #8 on: July 29, 2020, 02:45:18 pm »
Here is code for a screensaver using this API and my new JSON parsing function. Change the extension from exe to scr, right click, and press Install. Then change the settings in the screensaver settings menu in Windows to whatever timeout you want. Enjoy!

Code: QB64: [Select]
  1.  
  2. _TITLE "NASA Pic of the Day"
  3.  
  4. DIM i&
  5. i& = NASApod
  6. IF i& <> 0 THEN
  7.     _PUTIMAGE , i&, 0
  8. 'day = 86399
  9. DIM start AS LONG
  10.     start = TIMER
  11.     IF start = 18000 THEN
  12.         i& = NASApod
  13.         IF i& <> 0 THEN
  14.             _PUTIMAGE , i&, 0
  15.             _ICON _DISPLAY
  16.         END IF
  17.         _DELAY 1
  18.     END IF
  19.     _LIMIT 30
  20.  
  21. FUNCTION NASApod&
  22.     DIM URL AS STRING
  23.     DIM URLFile AS STRING
  24.     DIM nasapic AS STRING
  25.     DIM picdate AS STRING
  26.     DIM a%
  27.     picdate = MID$(DATE$, 7) + "-" + MID$(DATE$, 1, 2) + "-" + MID$(DATE$, 4, 2)
  28.     URLFile = "nasapod.json"
  29.     URL = "https://api.nasa.gov/planetary/apod?date=" + picdate + "&api_key=DEMO_KEY"
  30.     a% = API_request(URL, URLFile)
  31.     DIM U AS INTEGER
  32.     U = FREEFILE
  33.     OPEN URLFile FOR BINARY AS #U
  34.     IF LOF(U) <> 0 THEN
  35.         nasapic = SPACE$(LOF(U))
  36.         GET #U, , nasapic
  37.     ELSE
  38.         CLOSE #U
  39.         KILL URLFile
  40.         NASApod = 0
  41.         EXIT FUNCTION
  42.     END IF
  43.     CLOSE #U
  44.     KILL URLFile
  45.     nasapic = GetKey("hdurl", nasapic)
  46.     URLFile = "nasapod.jpg"
  47.     URL = nasapic
  48.     a% = API_request(URL, URLFile)
  49.     OPEN URLFile FOR BINARY AS #U
  50.     IF LOF(U) <> 0 THEN
  51.         NASApod = _LOADIMAGE(URLFile, 32)
  52.     ELSE
  53.         CLOSE #U
  54.         KILL URLFile
  55.         NASApod = 0
  56.     END IF
  57.     CLOSE #U
  58.     KILL URLFile
  59.  
  60.  
  61.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  62.  
  63. FUNCTION API_request (URL AS STRING, File AS STRING)
  64.     API_request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
  65.  
  66. FUNCTION GetKey$ (keyname AS STRING, JSON AS STRING)
  67.     DIM jkey AS STRING
  68.     IF INSTR(JSON, CHR$(34) + keyname + CHR$(34)) THEN
  69.         jkey = MID$(JSON, INSTR(JSON, CHR$(34) + keyname + CHR$(34)) + LEN(keyname))
  70.         jkey = MID$(jkey, INSTR(jkey, ":") + 1)
  71.         IF MID$(jkey, 1, 1) = CHR$(34) THEN
  72.             jkey = MID$(jkey, 2)
  73.         END IF
  74.         jkey = MID$(jkey, 1, INSTR(jkey, CHR$(34)) - 1)
  75.         IF RIGHT$(jkey, 1) = "," THEN
  76.             jkey = MID$(jkey, 1, LEN(jkey) - 1)
  77.         END IF
  78.     ELSE
  79.         GetKey = ""
  80.     END IF
  81.     GetKey = jkey
  82.  
  83. SUB GetAllKey (keyname AS STRING, JSON AS STRING, ParseKey() AS STRING)
  84.     DIM jkey AS STRING
  85.     DIM x
  86.     DO
  87.         IF INSTR(JSON, CHR$(34) + keyname + CHR$(34)) THEN
  88.             x = x + 1
  89.             REDIM _PRESERVE ParseKey(x) AS STRING
  90.             JSON = MID$(JSON, INSTR(JSON, CHR$(34) + keyname + CHR$(34)) + LEN(keyname))
  91.             jkey = JSON
  92.             jkey = MID$(jkey, INSTR(jkey, ":") + 1)
  93.             IF MID$(jkey, 1, 1) = CHR$(34) THEN
  94.                 jkey = MID$(jkey, 2)
  95.             END IF
  96.             jkey = MID$(jkey, 1, INSTR(jkey, CHR$(34)) - 1)
  97.             IF RIGHT$(jkey, 1) = "," THEN
  98.                 jkey = MID$(jkey, 1, LEN(jkey) - 1)
  99.             END IF
  100.             ParseKey(x) = jkey
  101.         END IF
  102.     LOOP UNTIL INSTR(JSON, CHR$(34) + keyname + CHR$(34)) = 0
« Last Edit: July 29, 2020, 09:10:54 pm by SpriggsySpriggs »
Shuwatch!