Author Topic: Find geographic location of user using IP address  (Read 2533 times)

0 Members and 1 Guest are viewing this topic.

FELIX

  • Bull Frog
  • Posts: 241
Find geographic location of user using IP address
« on: January 11, 2017, 10:11:58 PM »
How to get a user's IP to through an IPINFODB API to return an XML with location information?

Code: [Select]
(setq IPADDRESS "???.???.???.???");get IP
(setq APIKEY    "<your_api_key>") ;from http://ipinfodb.com
(setq STRX      (strcat "http://api.ipinfodb.com/v3/ip-city/?key=" APIKEY "&ip=" IPADDRESS)) ;XML
OK.

dgpuertas

  • Newt
  • Posts: 80
Re: Find geographic location of user using IP address
« Reply #1 on: January 12, 2017, 03:24:54 AM »

FELIX

  • Bull Frog
  • Posts: 241
Re: Find geographic location of user using IP address
« Reply #2 on: January 12, 2017, 07:47:49 AM »
I need IP Address not remote file
OK.

ChrisCarlson

  • Guest
Re: Find geographic location of user using IP address
« Reply #3 on: January 12, 2017, 09:33:44 AM »
One option is to setup a remote site which only returns the user's IP address.

Lee-Mac wrote a routine to grab internet time, the connection and return method could easily be adapted.

Code - Auto/Visual Lisp: [Select]
  1. ;;---------------------=={ Internet Time }==------------------;;
  2. ;;                                                            ;;
  3. ;;  Returns the date and/or UTC time as a string in the       ;;
  4. ;;  format specified. Data is sourced from a NIST server.     ;;
  5. ;;------------------------------------------------------------;;
  6. ;;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;
  7. ;;------------------------------------------------------------;;
  8. ;;  Arguments:                                                ;;
  9. ;;  format - string specifying format of returned information ;;
  10. ;;           using the following identifiers to represent     ;;
  11. ;;           date & time quantities:                          ;;
  12. ;;           YYYY = 4-digit year                              ;;
  13. ;;           YY   = Year, MO = Month,   DD = Day              ;;
  14. ;;           HH   = Hour, MM = Minutes, SS = Seconds          ;;
  15. ;;------------------------------------------------------------;;
  16. ;;  Returns:  String containing formatted date/time data      ;;
  17. ;;------------------------------------------------------------;;
  18. (defun LM:InternetTime ( format / result rgx server xml )
  19.     (setq server "http://time.nist.gov:13")
  20.     (setq result
  21.         (vl-catch-all-apply
  22.             (function
  23.                 (lambda ( / str )
  24.                     (setq xml (vlax-create-object "MSXML2.XMLHTTP.3.0"))
  25.                     (setq rgx (vlax-create-object "vbscript.regexp"))
  26.                     (vlax-invoke-method xml 'open "POST" server :vlax-false)
  27.                     (vlax-invoke-method xml 'send)
  28.                     (if (setq str (vlax-get-property xml 'responsetext))
  29.                         (progn
  30.                             (vlax-put-property rgx 'global     actrue)
  31.                             (vlax-put-property rgx 'ignorecase actrue)
  32.                             (vlax-put-property rgx 'multiline  actrue)
  33.                             (setq str (strcat " " (itoa (jtoy (+ (atoi (substr str 2 5)) 2400000.5))) (substr str 7)))
  34.                             (mapcar
  35.                                 (function
  36.                                     (lambda ( a b )
  37.                                         (vlax-put-property rgx 'pattern a)
  38.                                         (setq format (vlax-invoke rgx 'replace format b))
  39.                                     )
  40.                                 )
  41.                                '("YYYY" "YY" "MO" "DD" "HH" "MM" "SS")
  42.                                '( "$1"  "$2" "$3" "$4" "$5" "$6" "$7")
  43.                             )
  44.                             (vlax-put-property rgx 'pattern
  45.                                 (strcat
  46.                                     "(?:[^\\d]+)([\\d]+)(?:[^\\d]+)([\\d]+)"
  47.                                     "(?:[^\\d]+)([\\d]+)(?:[^\\d]+)([\\d]+)"
  48.                                     "(?:[^\\d]+)([\\d]+)(?:[^\\d]+)([\\d]+)"
  49.                                     "(?:[^\\d]+)([\\d]+)(?:.+)\\n"
  50.                                 )
  51.                             )
  52.                             (vlax-invoke-method rgx 'replace str format)
  53.                         )
  54.                     )
  55.                 )
  56.             )
  57.         )
  58.     )
  59.     (if xml  (vlax-release-object xml))
  60.     (if rgx  (vlax-release-object rgx))
  61.     (if (vl-catch-all-error-p result)
  62.         (prompt (vl-catch-all-error-message result))
  63.         result
  64.     )
  65. )
  66. ;; Julian Date to Calendar Year - Lee Mac
  67. ;; Algorithm from: Meeus, Jean.  Astronomical Algorithms.
  68. (defun jtoy ( j / a b c d )
  69.     (setq j (fix j)
  70.           a (fix (/ (- j 1867216.25) 36524.25))
  71.           b (+ (- (+ j 1 a) (fix (/ a 4))) 1524)
  72.           c (fix (/ (- b 122.1) 365.25))
  73.           d (fix (/ (- b (fix (* 365.25 c))) 30.6001))
  74.     )
  75.     (fix (- c (if (< 2 (fix (if (< d 14) (1- d) (- d 13)))) 4716 4715)))
  76. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Find geographic location of user using IP address
« Reply #4 on: January 12, 2017, 10:27:40 AM »
I need IP Address not remote file
No ... you need a remote file. That's how you obtain the data. Simply using a tool to get the IP will only give you the local IP address as per the computer on a LAN (the one starting with 192.168....). That would never give you anything regarding geographic location.


What you need is some external site which lists the client's external IP. Then download that into a file, and read the contents to obtain the IP address. The cleanest I know of is: http://icanhazip.com/ It just lists the IP itself, nothing else.


To make it easier, I've added some source code:

Code - Auto/Visual Lisp: [Select]
  1.  
  2. (defun Get-Http  (URL / util fn f txt)
  3.   (if (and (vla-IsURL util URL)
  4.            (not (vl-catch-all-error-p
  5.                   (vl-catch-all-apply 'vla-GetRemoteFile (list util URL 'fn :vlax-true))))
  6.            (setq f (open fn "r")))
  7.     (progn (while (setq fn (read-line f)) (setq txt (cons (strcat fn "\n") txt)))
  8.            (close f)
  9.            (apply 'strcat (reverse txt)))))
  10.  
  11. (defun GetExternalIP  (/ data)
  12.   (if (setq data (Get-Http "http://icanhazip.com"))
  13.     (vl-string-trim " \t\n" data)))

Just use it as (GetExternalIP)
« Last Edit: January 12, 2017, 10:32:27 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: Find geographic location of user using IP address
« Reply #5 on: January 12, 2017, 11:07:53 AM »
API to return an XML with location information?

https://www.theswamp.org/index.php?topic=33065.0

will return you current location
Code: [Select]
(vk_ReadXML "http://freegeoip.net/xml/")
you may also specify specific IP
Code: [Select]
(vk_ReadXML "http://freegeoip.net/xml/8.8.8.8")
« Last Edit: January 12, 2017, 11:11:31 AM by VovKa »

FELIX

  • Bull Frog
  • Posts: 241
Re: Find geographic location of user using IP address
« Reply #6 on: January 12, 2017, 02:50:22 PM »
Irneb, grateful for Judah in getting the external IP.

VovKa, just one word: PERFECT!
OK.