Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Christmas Info (Windows Users)
#1
Code: (Select All)
$Console:Only
_Dest _Console

ip$ = GetPublicIP$
Print "Your IP Address is:"; ip$
Print
Lat_Long ip$, lat, lon
Print "Your Latitude and Longitude is: "; lat, lon
Print
Print "For your location, the following is true for Xmas day:"
SunStuff lat, lon, 12, 25, 2020


Function GetPublicIP$
f = FreeFile
Open "tempPIP.txt" For Output As #f: Close f
Shell _Hide "cmd /c nslookup myip.opendns.com resolver1.opendns.com>tempPIP.txt"

Open "tempPIP.txt" For Input As #f
If LOF(f) Then
Do
Line Input #f, temp$
If temp$ <> "" Then last$ = temp$ 'there's a blank line after the data we need.
' Ignore it. What we want is the last line of info generated here.
Loop Until EOF(1)
End If
Close f
l = _InStrRev(last$, "Address:")
If l Then GetPublicIP$ = Mid$(last$, l + 10)
Kill "tempPIP.txt"
End Function

Sub Lat_Long (ip$, lat, lon)
out$ = "powershell.exe -c " + Chr$(34) + "Invoke-Webrequest 'ip-api.com/line/"
out$ = out$ + ip$
out$ = out$ + "?fields=lat,lon' -OutFile '.\temp.txt'" + Chr$(34)
Shell _Hide out$
Open "temp.txt" For Input As #1
Input #1, lat
Input #1, lon
Close 1
End Sub

Sub SunStuff (lat, lon, month, day, year)
out$ = "powershell.exe -c " + Chr$(34) + "Invoke-Webrequest 'https://api.sunrise-sunset.org/json?lat="
out$ = out$ + _Trim$(Str$(lat)) + "&lng="
out$ = out$ + _Trim$(Str$(lon)) + "&date="
d$ = _Trim$(Str$(year)) + _Trim$(Str$(month)) + _Trim$(Str$(day))
out$ = out$ + d$ + "' -OutFile '.\temp.txt'"
Shell out$
Open "temp.txt" For Binary As #1
t$ = Space$(LOF(1))
Get #1, 1, t$
'strip off unwanted stuff
l = InStr(t$, ":{"): t$ = Mid$(t$, l + 2)
Do
l = InStr(t$, Chr$(34))
t$ = Left$(t$, l - 1) + Mid$(t$, l + 1)
Loop Until l = 0
Do
l = InStr(t$, "_")
t$ = Left$(t$, l - 1) + " " + Mid$(t$, l + 1)
Loop Until l = 0
t$ = _Trim$(t$)
t$ = Left$(t$, Len(t$) - 1)

Do
l = InStr(t$, ",")
If l = 0 Then Exit Do
whole$ = Left$(t$, l)
l$ = Left$(whole$, InStr(whole$, ":") - 1)
r$ = Mid$(whole$, InStr(whole$, ":") + 1)
r$ = Left$(r$, Len(r$) - 1)
Print l$; " is "; r$
t$ = Mid$(t$, l + 1)
Loop
Close 1
End Sub
Reply
#2
Should be an interesting day in Aus: Sunrise 9:08 pm, Sunset 11:25 am ??? [Image: biggrin.png]
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply
#3
(10-18-2024, 03:22 AM)PhilOfPerth Wrote: Should be an interesting day in Aus: Sunrise 9:08 pm, Sunset 11:25 am ??? [Image: biggrin.png]

That's actually right.   Big Grin

You just have to adjust for the time zone difference for wherever you are from UTC, and then maybe add/subtract an hour for Daylight Savings Time, if applicable.

Times are all in UTC time.  (I guess I should've said that to start with.  Tongue

I'm just guessing, but would +9 sound about right for you, where you're at?  6:08 AM  and  8:25 PM for DEC 25th?
Reply
#4
I noticed a problem in line 34: After changing 'ip-api.com' to 'www.ip-api.com', it worked fine. Without 'www', an error occurs during execution in line 38 because the file 'temp.txt' is not found.
Greetings from Germany
"Give a man a program and he will be happy for a day. Teach him to program and he will be happy for the rest of his life."
Reply
#5
(10-18-2024, 12:44 PM)CletusSnow Wrote: I noticed a problem in line 34: After changing 'ip-api.com' to 'www.ip-api.com', it worked fine. Without 'www', an error occurs during execution in line 38 because the file 'temp.txt' is not found.
Greetings from Germany

Which version of Windows an Powershell, do you know?  I haven't had any issues with it, but that doesn't mean much as various versions of these tools get upgraded over time and systems and such.  I'm just curious if I can narrow down the problem and see why it works on my machine (and apparently Phil's as well), and yet needs to be modified for yours.  

Maybe it's a firewall or antivirus thing in effect?    Honestly, it's sometimes hard to tell when dealing with interweb unk.  Wink
Reply
#6
I have Windows 10 19045.5070 and PowerShell 7.4.5. Standard Windows security and firewall. Same issue on my Tablet and my other PC. Maybe special effects only for Germany?

Edit: It works fine on a virtual machine with Windows 11 26100 and PowerShell 1.0. It still works after an upgrade to PS 7.4.5
"Give a man a program and he will be happy for a day. Teach him to program and he will be happy for the rest of his life."
Reply
#7
The program doesn't work for me even with @CletusSnow's fix.

It looks like Invoke-Webrequest was introduced in PS 3, so it doesn't work on my lowly Win7 box which is stuck with PS 2.0.  (I've tried upgrading, but the MS installer package laughs in derision and shuts down.)

If someone needs to check their PS version, they can type $PSVersionTable at the PS prompt.


Edit: got it to work using curl instead of PowersHell:
Code: (Select All)
$Console:Only
_Dest _Console

ip$ = GetPublicIP$
Print "Your IP Address is:"; ip$
Print
Lat_Long ip$, lat, lon
Print "Your Latitude and Longitude is: "; lat, lon
Print
Print "For your location, the following is true for Xmas day:"
SunStuff lat, lon, 12, 25, 2020


Function GetPublicIP$
    f = FreeFile
    Open "tempPIP.txt" For Output As #f: Close f
    Shell _Hide "cmd /c nslookup myip.opendns.com resolver1.opendns.com>tempPIP.txt"

    Open "tempPIP.txt" For Input As #f
    If LOF(f) Then
        Do
            Line Input #f, temp$
            If temp$ <> "" Then last$ = temp$ 'there's a blank line after the data we need.
            '                                 Ignore it.  What we want is the last line of info generated here.
        Loop Until EOF(1)
    End If
    Close f
    l = _InStrRev(last$, "Address:")
    If l Then GetPublicIP$ = Mid$(last$, l + 10)
    Kill "tempPIP.txt"
End Function

Sub Lat_Long (ip$, lat, lon)
    'out$ = "powershell.exe -c " + Chr$(34) + "Invoke-Webrequest 'ip-api.com/line/"
    out$ = "curl -s -o ./temp.txt " + Chr$(34) + "http://www.ip-api.com/line/"
    out$ = out$ + ip$
    out$ = out$ + "?fields=lat,lon" + Chr$(34)
    'out$ = out$ + "?fields=lat,lon' -OutFile '.\temp.txt'" + Chr$(34)
    Shell _Hide out$
    Open "temp.txt" For Input As #1
    Input #1, lat
    Input #1, lon
    Close 1
End Sub

Sub SunStuff (lat, lon, month, day, year)
    'out$ = "powershell.exe -c " + Chr$(34) + "Invoke-Webrequest 'https://api.sunrise-sunset.org/json?lat="
    out$ = "curl -s -o temp.txt " + Chr$(34) + "http://api.sunrise-sunset.org/json?lat="
    out$ = out$ + _Trim$(Str$(lat)) + "&lng="
    out$ = out$ + _Trim$(Str$(lon)) + "&date="
    d$ = _Trim$(Str$(year)) + _Trim$(Str$(month)) + _Trim$(Str$(day))
    '    out$ = out$ + d$ + "'   -OutFile '.\temp.txt'"
    Shell out$
    Open "temp.txt" For Binary As #1
    t$ = Space$(LOF(1))
    Get #1, 1, t$
    'strip off unwanted stuff
    l = InStr(t$, ":{"): t$ = Mid$(t$, l + 2)
    Do
        l = InStr(t$, Chr$(34))
        t$ = Left$(t$, l - 1) + Mid$(t$, l + 1)
    Loop Until l = 0
    Do
        l = InStr(t$, "_")
        t$ = Left$(t$, l - 1) + " " + Mid$(t$, l + 1)
    Loop Until l = 0
    t$ = _Trim$(t$)
    t$ = Left$(t$, Len(t$) - 1)

    Do
        l = InStr(t$, ",")
        If l = 0 Then Exit Do
        whole$ = Left$(t$, l)
        l$ = Left$(whole$, InStr(whole$, ":") - 1)
        r$ = Mid$(whole$, InStr(whole$, ":") + 1)
        r$ = Left$(r$, Len(r$) - 1)
        Print l$; " is "; r$
        t$ = Mid$(t$, l + 1)
    Loop
    Close 1
End Sub



   

Man, that late sunrise/sunset in the wintertime really messes with my sleep schedule.
Reply
#8
@CletusSnow
@JRace

Give this version a shot, guys:

Code: (Select All)
$Unstable:Http

$Console:Only
_Dest _Console

ip$ = GetPublicIP$
Print "Your IP Address is:"; ip$
Print
Lat_Long ip$, lat, lon
Print "Your Latitude and Longitude is: "; lat, lon
Print
Print "For your location, the following is true for Xmas day:"
SunStuff lat, lon, 12, 25, 2024


Function GetPublicIP$
    f = FreeFile
    Open "tempPIP.txt" For Output As #f: Close f
    Shell _Hide "cmd /c nslookup myip.opendns.com resolver1.opendns.com>tempPIP.txt"

    Open "tempPIP.txt" For Input As #f
    If LOF(f) Then
        Do
            Line Input #f, temp$
            If temp$ <> "" Then last$ = temp$ 'there's a blank line after the data we need.
            '                                Ignore it.  What we want is the last line of info generated here.
        Loop Until EOF(1)
    End If
    Close f
    l = _InStrRev(last$, "Address:")
    If l Then GetPublicIP$ = Mid$(last$, l + 10)
    Kill "tempPIP.txt"
End Function

Sub Lat_Long (ip$, lat, lon)
    out$ = "www.ip-api.com/line/" + ip$ + "?fields=lat"
    t$ = Download$(out$, junk&)
    lat = Val(t$)
    out$ = "www.ip-api.com/line/" + ip$ + "?fields=lon"
    t$ = Download$(out$, junk&)
    lon = Val(t$)
End Sub

Sub SunStuff (lat, lon, month, day, year)
    out$ = "api.sunrise-sunset.org/json?lat="
    out$ = out$ + _Trim$(Str$(lat)) + "&lng="
    out$ = out$ + _Trim$(Str$(lon)) + "&date="
    d$ = _Trim$(Str$(year)) + _Trim$(Str$(month)) + _Trim$(Str$(day))
    out$ = out$ + d$
    t$ = Download$(out$, junk&)
    'strip off unwanted stuff
    l = InStr(t$, ":{"): t$ = Mid$(t$, l + 2)
    Do
        l = InStr(t$, Chr$(34))
        t$ = Left$(t$, l - 1) + Mid$(t$, l + 1)
    Loop Until l = 0
    Do
        l = InStr(t$, "_")
        t$ = Left$(t$, l - 1) + " " + Mid$(t$, l + 1)
    Loop Until l = 0
    t$ = _Trim$(t$)
    t$ = Left$(t$, Len(t$) - 1)

    Do
        l = InStr(t$, ",")
        If l = 0 Then Exit Do
        whole$ = Left$(t$, l)
        l$ = Left$(whole$, InStr(whole$, ":") - 1)
        r$ = Mid$(whole$, InStr(whole$, ":") + 1)
        r$ = Left$(r$, Len(r$) - 1)
        Print l$; " is "; r$
        t$ = Mid$(t$, l + 1)
    Loop
    Close 1
End Sub



' Content of the HTTP response is returned. The statusCode is also assigned.
Function Download$ (url As String, statusCode As Long)
    h& = _OpenClient("HTTP:" + url)
    statusCode = _StatusCode(h&)
    While Not EOF(h&)
        _Limit 60
        Get #h&, , s$
        content$ = content$ + s$
    Wend
    Close #h&
    Download$ = content$
End Function

Here, I've taken out the powershell commands and swapped this over to using our native QB64PE tcp/ip connections to download from the internet.  I'm thinking this should work for everyone without any issues.

(This may even work for Linux/Mac folks, if you can supply your lat/lon coordinates for it.  (Or ip address, as we use it to look up your lat/lon for you.)
Reply
#9
Smooth as silk on Win7 Pro 64-bit.
Reply
#10
Yep, it works.
"Give a man a program and he will be happy for a day. Teach him to program and he will be happy for the rest of his life."
Reply




Users browsing this thread: 7 Guest(s)