Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get countries from IP addresses

    Hi, I've got about 1000 IP addresses that I'd like to get country information for. R has a package for grabbing info from the freegeoip API. Does Stata have anything similar? I've played around a bit with the packages 'googleplaces' and 'geocodehere', but these require finding an actual place (like an address) to get the relevant info, whereas something like freegeoip outputs a bunch of really clean info (country, region, postcode, time zone etc).

    Is anything comparable available in Stata? It's a bit of a pain to need to go back and forth to R for this project so I'm hoping there is a solution.

    thanks!

  • #2
    You can wrap this code in a for loop:

    Code:
    insheet using http://ip-api.com/csv/208.80.152.201, clear nonames
    list
    You might also want to add a sleep 500 command in every loop, or you'll end up banned:

    Our system will automatically ban any IP addresses doing over 150 requests per minute. To unban your IP click here.

    You are free to use ip-api.com for non-commercial use. We do not allow commercial use without prior approval.

    Comment


    • #3
      Oh cool, that sounds like it'll work great. It also looks like other geolocator APIs have this functionality too (like freegeoip.net).

      Naïve question, but how would you recommend writing the forloop? Is there a simple way to dump all observations of a single variable into a list, so that I can loop over each IP that I need to get info for? I haven't had to do anything like this before.

      And I'm guessing that it wouldn't make sense to build the dataset in real time but rather to save each csv as a one-line dta and then append them all together in another loop?

      Comment


      • #4
        Whoops, two searches led me to...

        Code:
        levelsof ip, local(list_of_ips)
        foreach ip of local `list_of_ips' {
              insheet using http://ip-api.com/csv/`ip', clear nonames
              save `ip'
        }
        Last edited by Samuel Mehr; 04 Apr 2017, 01:26.

        Comment


        • #5
          Hmm, this should work in principle, but I think the formatting of IP addresses is causing Stata to throw an r(198) error [invalid name]. Can that be avoided somehow?

          Comment


          • #6
            I think I've tricked Stata into treating IPs as strings....

            Code:
            loc N = _N
            forval i = 1/`N' {            // make local vars for each IP
                loc ip`i' = ip[`i']
                }
            forval j = 1/`N' {            // get CSV for each IP, save as dta
                insheet using "http://freegeoip.net/csv/`ip`j''", clear nonames
                save `ip`j'', replace
                }
            forval k = 1/`N' {            // build dataset
                append using `ip`k''
                rm `ip`k''
                }
            Thanks for the tip, Sergio. If you have a sec, could you tell me if there is a more elegant way to do this?
            Last edited by Samuel Mehr; 04 Apr 2017, 01:44.

            Comment


            • #7
              Sure. I wrapped it up in a program and put it on Github here:

              https://github.com/sergiocorreia/geocode_ip

              How to use it:

              Code:
              geocode_ip ip, clear
              Let me know if it's useful and I might add it to SSC.

              Comment


              • #8
                Thanks! What I posted above is working for now and I'm traveling for the next few days, but I will take a look at the cleaner version soon. My guess is that people would use this, though, since there currently isn't a clean, one-command solution to this straightforward task, as there is in R.

                Comment


                • #9
                  Hello Sergio,

                  I´ve tried using the geocode_ip command but notice that the freegoip.net website is now redirected to a new ipstack.com address. I notice from the latest ado file that the command still queries freegoip. Is the command no longer working?

                  Comment


                  • #10
                    Originally posted by Carlos Lagorio View Post
                    Hello Sergio,

                    I´ve tried using the geocode_ip command but notice that the freegoip.net website is now redirected to a new ipstack.com address. I notice from the latest ado file that the command still queries freegoip. Is the command no longer working?
                    I haven't used it lately, and you are right, they now require an API key, which would require some changes in the code.

                    Comment


                    • #11
                      I recently uploaded to SSC a new program, checkipaddresses, . It queries a different API (iphub.info, which is mostly focused on detecting VPNs and proxies. But it might be useful as-is or as a basis for modification.

                      Comment

                      Working...
                      X