Code Monkey home page Code Monkey logo

gofreegeoip's Introduction

gofreegeoip

Build Status

Provides access to Freegeoip.net using GO

Freegeoip provides an API which can give results via, XML, JSON and CSV. This app uses the JSON interface.

freegeoip.net/{format}/{IP_or_hostname}

You are limited to 10,000 calls per hour. If this is not enough you have to provide your own server! ;-) In which case see the Github project here.

###Install Assuming you have go installed and setup on your system.

go get github.com/danward79/gofreegeoip

######To run from source

cd $GOPATH/src/github.com/danward79/gofreegeoip
go run gofreegeoip.go -s freegeoip.net -a 8.8.8.8

######To compile as a genuine command line tool.

cd $GOPATH/src/github.com/danward79/gofreegeoip
go build gofreegeoip.go

###Command Line interface

cd $GOPATH/src/github.com/danward79/gofreegeoip
./gofreegeoip -s freegeoip.net -a 8.8.8.8

######Command Line Switches

  • -s server, Enter the IP address (IP:PORT) for the query server, leave blank for freegeoip.net

  • -a ip, Enter the IP address for the location you wish to quiery, leave blank for this External WAN IP

This will return two items a Location array and a status, which complies with HTTP status codes, defined in RFC 2616.

Location array
    {
      IP
  	CountryCode
  	CountryName
  	RegionCode  
  	RegionName  
  	City
  	ZipCode
  	TimeZone
  	Latitude
  	Longitude
  	MetroCode
    }
Status codes
        StatusOK                   = 200
        StatusForbidden            = 403 (Returned when you exceed your API allowance)
        StatusNotFound             = 404

The library will also return a status of 1, 2 or 3 if another error has occurred. This is accompanied by a log message in stdout

###Library call

Example of call from code.

package main

import (
	"fmt"
	"github.com/danward79/gofreegeoip"
)

func main() {

	loc, status := gofreegeoip.Query("freegeoip.net", "8.8.8.8")

	fmt.Printf("%+v", loc)
	fmt.Println(status)
}

TODO - Query Timeout handling. The ADSL internet provided where I live is terrible. So somekind of back-off is essential.

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.