Git Product home page Git Product logo

ip-api-go's Introduction

ip-api-go

This is a go module for using ip-api to make batch queries.

With regards to the /batch endpoint, the ip-api batch endpoint docs state:

This endpoint is limited to 15 requests per minute from an IP address.

The geolocator in this go module consequently makes a request every 5 seconds (12 times a minute) to safely respect this limit. This makes it capable of geolocating up to 1200 IPs per minute.

If you're looking for a module to make no more than 45 geolocation queries per minute then you're probably better off using one of ip-api's non-batch endpoints yourself directly for instant results.

Usage

NewGeolocator should be used to create a Geolocator instance. NewGeolocator takes one integer argument specifying the size of the queue in the Geolocator. If the Geolocator runs out of space in its queue it will return an error, GeolocatorQueueFull, when you try to call Locate with a new IP.

It also takes a second boolean argument which specifies if the geolocator is being used in a development environment, in which case it will return dummy geolocations specified in geolocation.go.

Once you have a Geolocator instance, you can call Locate on it whenever you want. If the Geolocator hasn't queried ip-api for its geolocation yet, and it hasn't been queued, it will be queued. Until the Geolocator has queried ip-api for its geolocation, a LocationNotYetFound error will be returned. Once the geolocator has queried ip-api and cached the value for the IP you're requesting, it will return a Geolocation.

The cache of a Geolocator is not automatically cleared. You must call Prune on it periodically, or when its cache gets too big.

Example usage

package main

import (
	"fmt"
	"time"

	geolocator "github.com/TheTeaCat/ip-api-go"
)

func main() {
	const targetIP = "8.8.8.8"
	g := geolocator.NewGeolocator(1000, false)

	for {
		location, err := g.Locate(targetIP)

		// If any other error occurs, something has gone wrong.
		if err != nil && err.Error() != geolocator.LocationNotYetFound {
			fmt.Printf(
				"Something went wrong, err: %[1]v\n", err.Error(),
			)
			return
		}

		// If LocationNotYetFound occurs, the geolocator hasn't gotten round to processing our request.
		if err != nil && err.Error() == geolocator.LocationNotYetFound {
			fmt.Printf(
				"Still locating %[1]v...\n", targetIP,
			)
			time.Sleep(1 * time.Second) // We wait a second before trying again.
			continue
		}

		fmt.Printf(
			"%[1]v is in (The) %[2]v!\n",
			targetIP, location.Country, err,
		)
		return
	}
}

Expected output:

Still locating 8.8.8.8...
Still locating 8.8.8.8...
Still locating 8.8.8.8...
Still locating 8.8.8.8...
Still locating 8.8.8.8...
Still locating 8.8.8.8...
8.8.8.8 is in (The) United States!

ip-api-go's People

Contributors

onfe avatar theteacat avatar

Watchers

 avatar  avatar

Forkers

onfe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.