egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help with IPinfo Script

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
sysusr
Voice


Joined: 13 Dec 2016
Posts: 5

PostPosted: Sun Sep 16, 2018 11:13 am    Post subject: Need help with IPinfo Script Reply with quote

So far I have the following code to read data from ipinfo.io

Example of the site: https://ipinfo.io/8.8.8.8/json

Code:
proc geo {n u h c t} {
        catch {set http [::http::geturl http://ipinfo.io/[lindex $t 0]/json -timeout 6000]} error
        if {[string match "*error*" $error]} { putserv "PRIVMSG $c :connect error!" ; return 0 }
        if {[string match "*timeout*" $error]} { putserv "PRVMSG $c :timeout!"; return 0 }
        set html [::http::data $http]
        if {[string match "*Not Found*" $html]} { putserv "PRIVMSG $c :GeoIP: No data found." ; return }
        regexp -line -- {<"ip":>(.*)</ip>} $html -> ipz
        regexp -line -- {<"hostname":>(.*)</hostname>} $html -> hostname
        regexp -line -- {<"city":>(.*)</city>} $html -> city
        regexp -line -- {<"region">(.*)</region>} $html -> region
        regexp -line -- {<"country":>(.*)</country>} $html -> country
        regexp -line -- {<"loc":>(.*)</loc>} $html -> loc
        regexp -line -- {<"org":>(.*)</org>} $html -> org
        putserv "PRIVMSG $c :IP: $ipz - Hostname: $hostname - City: $city - Region: $region - Country: $country - Location: $loc - Organization: $org"
        http::cleanup $http
}


But I am getting the following error....

Quote:
[11:09:20] Tcl error [geo]: can't read "ipz": no such variable


Does anyone have any suggestion or help why I am getting this error? Thanks
Back to top
View user's profile Send private message
caesar
Ass Kicker


Joined: 14 Oct 2001
Posts: 3489
Location: Area 51

PostPosted: Sun Sep 16, 2018 1:34 pm    Post subject: Reply with quote

If the output is in json format why do you complicate things and not parse it as a dictionary with ::json::json2dict instead?

Here's a proof of concept.
Code:

package require http
package require json

proc geo {nick uhost hand chan text} {
   set lookup [lindex [split $text] 0]
   catch {set http [::http::geturl http://ipinfo.io/$lookup/json -timeout 6000]} error
   set data [::http::data $http]
   set json [::json::json2dict $data]
   ::http::cleanup $http
   foreach ele [dict keys $json] { set $ele [dict get $json $ele] }
   putserv "PRIVMSG $chan :IP: $ip - Hostname: $hostname - City: $city - Region: $region - Country: $country - Location: $loc - Organization: $org"
}

You can either grab json.tcl file and load it before your geo script or install tcllib package.
_________________
You may say anything about me, but at least don't misspell my name. xD
Back to top
View user's profile Send private message
sysusr
Voice


Joined: 13 Dec 2016
Posts: 5

PostPosted: Sun Sep 16, 2018 1:54 pm    Post subject: Reply with quote

Not sure what you mean. But I tried to load json and it broke. I already have tcllib package installed on my box. Just looking for the simplest way to parse the data
Back to top
View user's profile Send private message
caesar
Ass Kicker


Joined: 14 Oct 2001
Posts: 3489
Location: Area 51

PostPosted: Mon Sep 17, 2018 12:39 am    Post subject: Reply with quote

If you have tcllib then you don't need that separate json.tcl file, it should work without it.
_________________
You may say anything about me, but at least don't misspell my name. xD
Back to top
View user's profile Send private message
sysusr
Voice


Joined: 13 Dec 2016
Posts: 5

PostPosted: Mon Sep 17, 2018 10:06 am    Post subject: Reply with quote

caesar wrote:
If you have tcllib then you don't need that separate json.tcl file, it should work without it.


Unfortunately it still does not work. Any other suggestion?
Back to top
View user's profile Send private message
willyw
Owner


Joined: 15 Jan 2009
Posts: 970

PostPosted: Mon Sep 17, 2018 10:29 am    Post subject: Reply with quote

You might like to be SURE that you have json, etc.

In the partyline, do:
.tcl package names

and it will return a list of everything. Check to be sure that it is finding what you want/need.

( You will need to have the use of .tcl for owner's use, enabled in eggdrop.conf for this to work. )
_________________
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
Back to top
View user's profile Send private message
sysusr
Voice


Joined: 13 Dec 2016
Posts: 5

PostPosted: Mon Sep 17, 2018 10:43 am    Post subject: Reply with quote

willyw wrote:
You might like to be SURE that you have json, etc.

In the partyline, do:
.tcl package names

and it will return a list of everything. Check to be sure that it is finding what you want/need.

( You will need to have the use of .tcl for owner's use, enabled in eggdrop.conf for this to work. )


OK i got the script working but when the field is blank it has another error.

Quote:
[10:51:01] Tcl error [who]: can't read "hostname": no such variable
Back to top
View user's profile Send private message
caesar
Ass Kicker


Joined: 14 Oct 2001
Posts: 3489
Location: Area 51

PostPosted: Tue Sep 18, 2018 2:33 am    Post subject: Reply with quote

Here's a sneaky method. Replace:
Code:

foreach ele [dict keys $json] { set $ele [dict get $json $ele] }

with:
Code:

set keys [dict keys $json]
set expecting {ip hostname city region country loc postal phone org}
foreach ele $expecting {
   if {[lsearch $keys $ele] != -1} {
      set $ele [dict get $json $ele]
   } else {
      set $ele "n/a"
   }
}

_________________
You may say anything about me, but at least don't misspell my name. xD
Back to top
View user's profile Send private message
sysusr
Voice


Joined: 13 Dec 2016
Posts: 5

PostPosted: Fri Oct 12, 2018 2:09 pm    Post subject: Reply with quote

Forgot to reply back. Script works flawless now. Thank you
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber