Author Topic: A quick program for folks to test  (Read 5688 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
A quick program for folks to test
« on: December 24, 2020, 10:47:16 pm »
Code: QB64: [Select]
  1.  
  2. ip$ = GetPublicIP$
  3. PRINT "Your IP Address is:"; ip$
  4. Lat_Long ip$, lat, lon
  5. PRINT "Your Latitude and Longitude is: "; lat, lon
  6. PRINT "For your location, the following is true for Xmas day:"
  7. SunStuff lat, lon, 12, 25, 2020
  8.  
  9.  
  10. FUNCTION GetPublicIP$
  11.     f = FREEFILE
  12.     OPEN "tempPIP.txt" FOR OUTPUT AS #f: CLOSE f
  13.     SHELL _HIDE "cmd /c nslookup myip.opendns.com resolver1.opendns.com>tempPIP.txt"
  14.  
  15.     OPEN "tempPIP.txt" FOR INPUT AS #f
  16.     IF LOF(f) THEN
  17.         DO
  18.             LINE INPUT #f, temp$
  19.             IF temp$ <> "" THEN last$ = temp$ 'there's a blank line after the data we need.
  20.             '                                 Ignore it.  What we want is the last line of info generated here.
  21.         LOOP UNTIL EOF(1)
  22.     END IF
  23.     CLOSE f
  24.     l = _INSTRREV(last$, "Address:")
  25.     IF l THEN GetPublicIP$ = MID$(last$, l + 10)
  26.     KILL "tempPIP.txt"
  27.  
  28. SUB Lat_Long (ip$, lat, lon)
  29.     out$ = "powershell.exe -c " + CHR$(34) + "Invoke-Webrequest 'ip-api.com/line/"
  30.     out$ = out$ + ip$
  31.     out$ = out$ + "?fields=lat,lon' -OutFile '.\temp.txt'" + CHR$(34)
  32.     SHELL _HIDE out$
  33.     OPEN "temp.txt" FOR INPUT AS #1
  34.     INPUT #1, lat
  35.     INPUT #1, lon
  36.     CLOSE 1
  37.  
  38. SUB SunStuff (lat, lon, month, day, year)
  39.     out$ = "powershell.exe -c " + CHR$(34) + "Invoke-Webrequest 'https://api.sunrise-sunset.org/json?lat="
  40.     out$ = out$ + _TRIM$(STR$(lat)) + "&lng="
  41.     out$ = out$ + _TRIM$(STR$(lon)) + "&date="
  42.     d$ = _TRIM$(STR$(year)) + _TRIM$(STR$(month)) + _TRIM$(STR$(day))
  43.     out$ = out$ + d$ + "'   -OutFile '.\temp.txt'"
  44.     SHELL out$
  45.     OPEN "temp.txt" FOR BINARY AS #1
  46.     t$ = SPACE$(LOF(1))
  47.     GET #1, 1, t$
  48.     'strip off unwanted stuff
  49.     l = INSTR(t$, ":{"): t$ = MID$(t$, l + 2)
  50.     DO
  51.         l = INSTR(t$, CHR$(34))
  52.         t$ = LEFT$(t$, l - 1) + MID$(t$, l + 1)
  53.     LOOP UNTIL l = 0
  54.     DO
  55.         l = INSTR(t$, "_")
  56.         t$ = LEFT$(t$, l - 1) + " " + MID$(t$, l + 1)
  57.     LOOP UNTIL l = 0
  58.     t$ = _TRIM$(t$)
  59.     t$ = LEFT$(t$, LEN(t$) - 1)
  60.  
  61.     DO
  62.         l = INSTR(t$, ",")
  63.         IF l = 0 THEN EXIT DO
  64.         whole$ = LEFT$(t$, l)
  65.         l$ = LEFT$(whole$, INSTR(whole$, ":") - 1)
  66.         r$ = MID$(whole$, INSTR(whole$, ":") + 1)
  67.         r$ = LEFT$(r$, LEN(r$) - 1)
  68.         PRINT l$; " is "; r$
  69.         t$ = MID$(t$, l + 1)
  70.     LOOP
  71.     CLOSE 1
  72.  

Output should look like the below (more, or less, changed by your location):

  [ You are not allowed to view this attachment ]  

Let me know if this fails to work for any of you Windows folk, and let me know what it does do (if anything at all), so I can work on this.  I'm hoping to add this into my little calendar program so that it'll always be able to give me sunrise and sunset times for my location in the world.  :D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: A quick program for folks to test
« Reply #1 on: December 24, 2020, 10:51:40 pm »
Works awesome!

I was thinking about two things for the calendar, too. Might as well state them here.

1) Names of the days of the week across the top. I'm sure you kinda get used to the days not being there, but it would be a little more complete that way.

2) Externalizing holiday data as an external file. This opens up a big world, in the sense that if you let certain RSS feeds add to that data file, this calendar can archive or predict all kinds of stuff.
« Last Edit: December 24, 2020, 11:00:37 pm by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A quick program for folks to test
« Reply #2 on: December 24, 2020, 10:52:37 pm »
NOTE: All times are in UTC and summer time adjustments are not included in the returned data.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: A quick program for folks to test
« Reply #3 on: December 24, 2020, 11:07:03 pm »
Oh and - if you completely generalize the routine that undresses t$, you will have a JSON parser.
You're not done when it works, you're done when it's right.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A quick program for folks to test
« Reply #4 on: December 24, 2020, 11:14:51 pm »
Oh and - if you completely generalize the routine that undresses t$, you will have a JSON parser.

That's a job for another day.  ;)

As for days, that's easy enough to insert.  I'll add a CONST as a toggle for folks who want them, or not.

For the external holidays, I'm still sorting out the format I want to add for future versions.  I'm thinking something like:

Exact, 12,25, Christmas
Last, 12, 5, Foo_day
First, 12, 2, Fee_day

12/25 is Christmas
Last Thursday (5) in December (12) is Foo_day
First Monday (2) in December is Fee_day

Flexible enough for mouse holidays, and not so complex as to be indecipherable.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: A quick program for folks to test
« Reply #5 on: December 24, 2020, 11:19:46 pm »
In my attempt, it said that it could not find the file in line 38. That's where you tell it to open temp.txt for input. And it's supposed to contain lat and long? Weird. I'll bet my enterprise net is blocking whatever you're trying to do here.

But, it did print out my IP address. Of course, that's an address behind the NAT, so it doesn't really help pinpoint my location. Wha' happened?
« Last Edit: December 24, 2020, 11:23:39 pm by Bert22306 »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A quick program for folks to test
« Reply #6 on: December 24, 2020, 11:23:07 pm »
In my attempt, it said that it could not find the file in line 38. That's where you tell it to input temp.txt.

But, it did print out my IP address. Of course, that's an address behind the NAT, so it doesn't really help pinpoint my location. Wha' happened?

Remove the _HIDE on line 37 and see what the output in the console looks like. 

Did it generate a lat/long value for you?

And you need permissions to run powershell scripts on your PC, as well, if that's an issue.
« Last Edit: December 24, 2020, 11:26:15 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: A quick program for folks to test
« Reply #7 on: December 24, 2020, 11:28:20 pm »
No. It says something about authentication required to reach the site, but it did show a public IP address, at least, as opposed to the one behind my NAT. I'm doing this test from my corporate enterprise net, which is almost certainly why these problems exist.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
🎄Re: A quick program for folks to test🎄
« Reply #8 on: December 24, 2020, 11:37:00 pm »
@Steve
🎄
  [ You are not allowed to view this attachment ]  

So in Australia we have longer Xmas days than you.
« Last Edit: December 24, 2020, 11:42:41 pm by Richard »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: A quick program for folks to test
« Reply #9 on: December 25, 2020, 12:11:17 am »
I shall give this a try and use my pipecom library to get rid of the file operations.
Shuwatch!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A quick program for folks to test
« Reply #10 on: December 25, 2020, 12:34:51 am »
I shall give this a try and use my pipecom library to get rid of the file operations.

Personally, I prefer the file operations.  With them, I can check to see if the file exists, and if so, don't need to call the functions again.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A quick program for folks to test
« Reply #11 on: December 25, 2020, 01:10:06 am »
Hi Steve,

Mine didn't run. Linux Mint 20. "Line: 38 (in main module). File not found". "temp.txt"

J
Logic is the beginning of wisdom.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A quick program for folks to test
« Reply #12 on: December 25, 2020, 01:18:38 am »
Hi Steve,

Mine didn't run. Linux Mint 20. "Line: 38 (in main module). File not found". "temp.txt"

J

Yours won't run, as it relies on window's Powershell to download info from the web.  I'll need to add a Linux method to do the same, and then it'll work for you.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: A quick program for folks to test
« Reply #13 on: December 25, 2020, 04:06:02 am »
Thanks Steve... No rush... Just curious to see if it would work....
Logic is the beginning of wisdom.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: A quick program for folks to test
« Reply #14 on: December 25, 2020, 04:16:49 am »
Thanks Steve... No rush... Just curious to see if it would work....

Linux would probably just need to use wget instead.

SHELL "wget http://example.com/file.tar -O /path/to/dir/file.tar"

With the syntax above, used with the links from the powershell code, you should be able to download the same results on Linux as on windows.  I'll see about putting up some code for you to try later, so you can be my test monkey with it.  I don't usually try and code much for Linux, as all my personal stuff is windows, but this is easy enough to account for that I don't mind trying it blindfolded.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!