Author Topic: Get Local Time and Public IP via Online API  (Read 3338 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Get Local Time and Public IP via Online API
« on: July 20, 2020, 03:26:43 pm »
In case someone is needing this; here. These two functions use a public online API to get public IP address or local time, according to IP. Therefore, if you were using a VPN you would have perhaps a different return value. Anyways, here it is. Have fun with it. I'll be posting more API usage soon.
Code: QB64: [Select]
  1. PRINT GetLocalTime
  2.  
  3. PRINT GetPublicIP
  4.  
  5. FUNCTION GetLocalTime$
  6.     DIM URL AS STRING
  7.     DIM URLFile AS STRING
  8.     DIM localtime AS STRING
  9.     DIM a%
  10.     URLFile = "localtime"
  11.     URL = "http://worldtimeapi.org/api/ip/"
  12.     URL = URL + GetPublicIP + ".txt"
  13.     a% = FileDownload(URL, URLFile)
  14.     DIM U AS INTEGER
  15.     U = FREEFILE
  16.     OPEN URLFile FOR BINARY AS #U
  17.     IF LOF(U) <> 0 THEN
  18.         DO
  19.             LINE INPUT #U, localtime
  20.         LOOP UNTIL INSTR(localtime, "datetime:")
  21.     ELSE
  22.         CLOSE #U
  23.         KILL URLFile
  24.         GetLocalTime = ""
  25.         EXIT FUNCTION
  26.     END IF
  27.     CLOSE #U
  28.     KILL URLFile
  29.     localtime = MID$(localtime, 11)
  30.     GetLocalTime = localtime
  31.  
  32. FUNCTION GetPublicIP$
  33.     DIM URL AS STRING
  34.     DIM URLFile AS STRING
  35.     DIM publicip AS STRING
  36.     DIM a%
  37.     URLFile = "publicip"
  38.     URL = "https://api.ipify.org/"
  39.     a% = FileDownload(URL, URLFile)
  40.     DIM U AS INTEGER
  41.     U = FREEFILE
  42.     OPEN URLFile FOR BINARY AS #U
  43.     IF LOF(U) <> 0 THEN
  44.         LINE INPUT #U, publicip
  45.     ELSE
  46.         CLOSE #U
  47.         KILL URLFile
  48.         GetPublicIP = ""
  49.         EXIT FUNCTION
  50.     END IF
  51.     CLOSE #U
  52.     KILL URLFile
  53.     GetPublicIP = publicip
  54.  
  55.     FUNCTION URLDownloadToFileA (BYVAL pCaller AS LONG, szURL AS STRING, szFileName AS STRING, BYVAL dwReserved AS LONG, BYVAL lpfnCB AS LONG)
  56.  
  57. FUNCTION FileDownload (URL AS STRING, File AS STRING)
  58.     FileDownload = URLDownloadToFileA(0, URL, File, 0, 0)
« Last Edit: July 20, 2020, 03:42:39 pm by SpriggsySpriggs »
Shuwatch!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Get Local Time and Public IP via Online API
« Reply #1 on: July 21, 2020, 01:31:34 am »
Nice work! :)
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials