Author Topic: Spriggsy's API Collection  (Read 5917 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #15 on: August 19, 2020, 11:38:55 AM »
There is a public API for one of the well-known encyclopedias/dictionaries. I might look into that. It could be an interesting project.

Offline Dav

  • Forum Resident
  • Posts: 646
Re: Spriggsy's API Collection
« Reply #16 on: August 19, 2020, 12:18:46 PM »
Lol at "If you're API and you know it clap your hands". :)

- Dav

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #17 on: August 19, 2020, 06:40:04 PM »
Updated collection with Owlbot Dictionary API on 08/19/2020 at 6:37 PM. The API works very similar to the OMDB API in that I have a console window to display some info from the JSON response and a regular screen to show the image from the response. Below is a screenshot of the new API working:

 
« Last Edit: August 19, 2020, 06:44:02 PM by SpriggsySpriggs »

Offline bplus

  • Forum Resident
  • Posts: 6907
  • b = b + ...
Re: Spriggsy's API Collection
« Reply #18 on: August 20, 2020, 03:23:43 PM »
Now that I can get a pic from NASA, I worked on a better display of the description and image:
Code: QB64: [Select]
  1. _TITLE "NASA Pic of the Day, spacebar to toggle between image and description" ' b+ mod of Spriggsy's code
  2. DIM fl&, fr&, i&, d&, picTitle$, description$, toggle, k$, lp, scalePic
  3. DIM nlines, lineLength, yStart, startPlace, endPlace, markPlace, xoff, yoff, poff
  4. _DELAY .25
  5. PRINT "One moment please, accessing and assembling data for NASA picture of the day."
  6. i& = NASApod
  7. IF i& <> 0 THEN
  8.     fl& = _LOADFONT(ENVIRON$("SYSTEMROOT") + "\Fonts\bahnschrift.ttf", 40)
  9.     fr& = _LOADFONT(ENVIRON$("SYSTEMROOT") + "\Fonts\bahnschrift.ttf", 20)
  10.     _ICON i&
  11.     d& = _NEWIMAGE(_WIDTH, _HEIGHT, 32)
  12.     _DEST d&
  13.     COLOR , &H00000000 ' clear background
  14.     _FONT fr&
  15.     lineLength = _WIDTH - 2 * .1 * _WIDTH
  16.     nlines = _PRINTWIDTH(description$) / lineLength + 2
  17.     yStart = (_HEIGHT - _FONTHEIGHT(fr&) * nlines) / 2
  18.     startPlace = 1: endPlace = startPlace + 1
  19.     FOR lp = 0 TO nlines
  20.         WHILE _PRINTWIDTH(MID$(description$, startPlace, endPlace - startPlace + 1)) < lineLength
  21.             endPlace = endPlace + 1
  22.             IF endPlace > LEN(description$) THEN endPlace = LEN(description$): EXIT WHILE
  23.         WEND
  24.         markPlace = endPlace
  25.         WHILE MID$(description$, endPlace, 1) <> " "
  26.             endPlace = endPlace - 1
  27.             IF endPlace <= startPlace THEN endPlace = markPlace: EXIT WHILE
  28.         WEND
  29.         poff = .1 * _WIDTH + (lineLength - _PRINTWIDTH(MID$(description$, startPlace, endPlace - startPlace + 1))) / 2
  30.         _PRINTSTRING (poff, yStart + _FONTHEIGHT(fr&) * lp), MID$(description$, startPlace, endPlace - startPlace + 1)
  31.         startPlace = endPlace + 1
  32.         endPlace = startPlace + 2
  33.         IF endPlace > LEN(description$) THEN EXIT FOR
  34.     NEXT
  35.     _FONT fl&
  36.     _PRINTSTRING ((_WIDTH - _PRINTWIDTH(picTitle$)) / 2, 50), picTitle$
  37.  
  38.     _DEST 0 ' OK show time
  39.     IF _WIDTH / _WIDTH(i&) < _HEIGHT / _HEIGHT(i&) THEN
  40.         scalePic = _WIDTH / _WIDTH(i&)
  41.         yoff = (_HEIGHT - scalePic * _HEIGHT(i&)) / 2
  42.     ELSE
  43.         scalePic = _HEIGHT / _HEIGHT(i&)
  44.         xoff = (_WIDTH - scalePic * _WIDTH(i&)) / 2
  45.     END IF
  46.     DO
  47.         COLOR , &HFF000000: CLS
  48.         k$ = INKEY$
  49.         IF k$ = " " THEN toggle = 1 - toggle
  50.         IF toggle THEN
  51.             _PUTIMAGE (xoff, yoff)-STEP(scalePic * _WIDTH(i&), scalePic * _HEIGHT(i&)), i&, 0
  52.         ELSE
  53.             _PUTIMAGE , d&, 0
  54.         END IF
  55.         _DISPLAY
  56.         _LIMIT 60
  57.     LOOP UNTIL _KEYDOWN(27)
  58.     PRINT "Sorry no pic available now. Press any..."
  59.     SLEEP: END
  60.  
  61. FUNCTION NASApod&
  62.     DIM URL AS STRING
  63.     DIM URLFile AS STRING
  64.     DIM nasapic AS STRING
  65.     DIM nasarequest AS STRING
  66.     SHARED picTitle AS STRING
  67.     SHARED description AS STRING
  68.     DIM a%
  69.     URLFile = "nasapod.json"
  70.     URL = "https://api.nasa.gov/planetary/apod?api_key=w8uuw4gfVqpcnaTI3g02zJizTgtOaXJtaAmASebV"
  71.     a% = API_request(URL, URLFile)
  72.     DIM U AS INTEGER
  73.     U = FREEFILE
  74.     OPEN URLFile FOR BINARY AS #U
  75.     IF LOF(U) <> 0 THEN
  76.         nasarequest = SPACE$(LOF(U))
  77.         GET #U, , nasarequest
  78.     ELSE
  79.         CLOSE #U
  80.         KILL URLFile
  81.         NASApod = 0
  82.         EXIT FUNCTION
  83.     END IF
  84.     CLOSE #U
  85.     KILL URLFile
  86.     picTitle = GetKey("title", nasarequest)
  87.     description = GetKey("explanation", nasarequest)
  88.     nasapic = GetKey("hdurl", nasarequest)
  89.     URLFile = "nasapod.jpg"
  90.     URL = nasapic
  91.     a% = API_request(URL, URLFile)
  92.     OPEN URLFile FOR BINARY AS #U
  93.     IF LOF(U) <> 0 THEN
  94.         NASApod = _LOADIMAGE(URLFile, 32)
  95.     ELSE
  96.         CLOSE #U
  97.         KILL URLFile
  98.         NASApod = 0
  99.     END IF
  100.     CLOSE #U
  101.     KILL URLFile
  102.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  103. FUNCTION API_request (URL AS STRING, File AS STRING)
  104.     API_request = URLDownloadToFileA(0, URL + CHR$(0), File + CHR$(0), 0, 0)
  105. FUNCTION GetKey$ (keyname AS STRING, JSON AS STRING)
  106.     DIM jkey AS STRING
  107.     jkey = JSON
  108.     IF INSTR(jkey, CHR$(34) + keyname + CHR$(34)) THEN
  109.         jkey = MID$(jkey, INSTR(jkey, CHR$(34) + keyname + CHR$(34)) + LEN(keyname))
  110.         jkey = MID$(jkey, INSTR(jkey, ":") + 1)
  111.         jkey = STRING.Replace(jkey, "\" + CHR$(34), "'")
  112.         IF MID$(jkey, 1, 1) = CHR$(34) THEN
  113.             jkey = MID$(jkey, 2)
  114.         END IF
  115.         jkey = MID$(jkey, 1, INSTR(jkey, CHR$(34)) - 1)
  116.         IF RIGHT$(jkey, 1) = "," THEN
  117.             jkey = MID$(jkey, 1, LEN(jkey) - 1)
  118.         END IF
  119.     ELSE
  120.         GetKey = ""
  121.     END IF
  122.     GetKey = jkey
  123. SUB GetAllKey (keyname AS STRING, JSON AS STRING, ParseKey() AS STRING)
  124.     DIM unchangejson AS STRING
  125.     DIM jkey AS STRING
  126.     DIM x
  127.     unchangejson = JSON
  128.     DO
  129.         IF INSTR(unchangejson, CHR$(34) + keyname + CHR$(34)) THEN
  130.             x = x + 1
  131.             REDIM _PRESERVE ParseKey(x) AS STRING
  132.             unchangejson = MID$(unchangejson, INSTR(unchangejson, CHR$(34) + keyname + CHR$(34)) + LEN(keyname))
  133.             jkey = unchangejson
  134.             jkey = MID$(jkey, INSTR(jkey, ":") + 1)
  135.             jkey = STRING.Replace(jkey, "\" + CHR$(34), "'")
  136.             IF MID$(jkey, 1, 1) = CHR$(34) THEN
  137.                 jkey = MID$(jkey, 2)
  138.             END IF
  139.             jkey = MID$(jkey, 1, INSTR(jkey, CHR$(34)) - 1)
  140.             IF RIGHT$(jkey, 1) = "," THEN
  141.                 jkey = MID$(jkey, 1, LEN(jkey) - 1)
  142.             END IF
  143.             ParseKey(x) = jkey
  144.         END IF
  145.     LOOP UNTIL INSTR(unchangejson, CHR$(34) + keyname + CHR$(34)) = 0
  146. FUNCTION STRING.Replace$ (a AS STRING, b AS STRING, c AS STRING)
  147.     DIM j
  148.     DIM r$
  149.     j = INSTR(a, b)
  150.     IF j > 0 THEN
  151.         r$ = LEFT$(a, j - 1) + c + STRING.Replace(RIGHT$(a, LEN(a) - j + 1 - LEN(b)), b, c)
  152.     ELSE
  153.         r$ = a
  154.     END IF
  155.     STRING.Replace = r$
  156.  

Only one sample to work with, tomorrow this code could be rubbish ;-))
« Last Edit: August 20, 2020, 03:40:03 PM by bplus »

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #19 on: August 20, 2020, 06:14:24 PM »
That's pretty neat, @bplus ! I like the formatting of the text! What an excellent picture for today, too!
« Last Edit: August 20, 2020, 07:54:11 PM by SpriggsySpriggs »

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #20 on: August 20, 2020, 10:40:44 PM »
Updated NASA Picture of the Day API with source code I had posted elsewhere in the forum. Much better code. Uses the date to get the day's picture so you don't have to worry about the picture possibly being not there yet (but doesn't fix the issue of if they don't even put a picture link in the JSON response). Can be used as a screensaver by building the EXE and changing the extension to SCR, right-clicking it, and pressing install. Works pretty well. If left running, it updates automatically at 5 AM local. (I used it myself on my work computer as my screensaver. Was pretty neat. I could come in every day and see a new image on my desktop.) @bplus , I think your modification would be good to merge with this different version of my NASA Pic of the day. I will look at your code and mine and modify them to make one source and then update the source in the zip folder.

Offline loudar

  • Newbie
  • Posts: 61
  • improve it bit by bit.
Re: Spriggsy's API Collection
« Reply #21 on: August 28, 2020, 08:19:21 AM »
I have just now realized how beautiful bahnschrift can look! Really cool stuff.
Check out what I do besides coding: http://loudar.myportfolio.com/

Offline bplus

  • Forum Resident
  • Posts: 6907
  • b = b + ...
Re: Spriggsy's API Collection
« Reply #22 on: August 28, 2020, 12:54:18 PM »
"The Valley of Orion" really cool today!

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #23 on: August 29, 2020, 11:56:06 PM »
@bplus Yes, very beautiful! Today's is quite nice as well!
@loudar Yeah, I was just browsing the Fonts folder and happened upon it. Was wanting something that looked nice and smooth.
« Last Edit: August 29, 2020, 11:57:42 PM by SpriggsySpriggs »

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #24 on: September 05, 2020, 05:29:16 PM »
Will try to update this soon. Been busy with looking for a job and other things.

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #25 on: September 20, 2020, 09:13:23 PM »
Updated 09:11 PM 09/20/2020. Added Genderize API which checks probability of gender of name based on two letter country ID entered. The test code I have only returns back either male or female. You can comment out the KILL command in the API_request function to see the full JSON response which has also the probability either way.

bUt iT's 2020!1!111!!!!!
« Last Edit: September 20, 2020, 09:17:37 PM by SpriggsySpriggs »

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #26 on: September 20, 2020, 09:32:36 PM »
Updated 09:31 PM with QR Tag API which takes a URL and generates a QR code that will take you to the website you provide.

 

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #27 on: September 22, 2020, 07:52:12 PM »
09/22/2020 Added SHFileOperationA functions from WinAPI. The example program allows to delete a file to recycle bin rather than permanently.

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #28 on: September 22, 2020, 11:01:43 PM »
11:00 PM 09/22/2020 Updated my source for the SHFileOperationA WinAPI with the rest of the functionality in that set. It can now Copy, Move, Rename, and Recycle (all with confirmation dialogs, if the criteria is met).

Offline SpriggsySpriggs

  • QB64 Developer
  • Forum Resident
  • Posts: 1057
  • If you're API and you know it clap your hands
    • My GitHub
Re: Spriggsy's API Collection
« Reply #29 on: September 22, 2020, 11:08:30 PM »
11:06 09/22/2020 Added Kernel32 and Advapi32 WinAPI usage for getting computer name and username. Works in 32 bit AND 64 bit.

11:33 PM 09/22/2020 Added Shell32 function to SHFileOperation WinAPI code which allows you to empty the recycle bin with confirmation. Even though it isn't SHFileOperation, it is still part of the same DLL and since it has something to do with the recycle bin I felt it was appropriate to add it to that one in particular.

1:33 AM 09/23/2020 Added Shell32 function SHQueryRecycleBin to the SHFileOperation WinAPI code which queries the Recycle Bin and shows how many files are contained in it and how much space the bin is currently occupying on disk.
« Last Edit: September 23, 2020, 01:47:21 AM by SpriggsySpriggs »