Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
maldevel committed Dec 12, 2015
1 parent 43bf0b8 commit f574bdf
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 0 deletions.
84 changes: 84 additions & 0 deletions geolocation/IpGeoLocation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
IPGeoLocation - Retrieve IP Geolocation information (http://ip-api.com)
Copyright (C) 2015 maldevel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

__author__ = 'maldevel'


class IpGeoLocation:
"""Represents an IP Geolocation information object"""

def __init__(self, jsonData = None):
self.ASN = '-'
self.City = '-'
self.Country = '-'
self.CountryCode = '-'
self.ISP = '-'
self.Latitude = 0.0
self.Longtitude = 0.0
self.Organization = '-'
self.IP = '0.0.0.0'
self.Region = '-'
self.RegionName = '-'
self.Status = '-'
self.Timezone = '-'
self.Zip = '-'

if jsonData != None:
if type(jsonData) is dict:
if 'as' in jsonData:
self.ASN = jsonData['as']

if 'city' in jsonData:
self.City = jsonData['city']

if 'country' in jsonData:
self.Country = jsonData['country']

if 'countryCode' in jsonData:
self.CountryCode = jsonData['countryCode']

if 'isp' in jsonData:
self.ISP = jsonData['isp']

if 'lat' in jsonData:
self.Latitude = jsonData['lat']

if 'lon' in jsonData:
self.Longtitude = jsonData['lon']

if 'org' in jsonData:
self.Organization = jsonData['org']

if 'query' in jsonData:
self.IP = jsonData['query']

if 'region' in jsonData:
self.Region = jsonData['region']

if 'regionName' in jsonData:
self.RegionName = jsonData['regionName']

if 'status' in jsonData:
self.Status = jsonData['status']

if 'timezone' in jsonData:
self.Timezone = jsonData['timezone']

if 'zip' in jsonData:
self.Zip = jsonData['zip']

50 changes: 50 additions & 0 deletions geolocation/IpGeoLocationLib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
IPGeoLocation - Retrieve IP Geolocation information (http://ip-api.com)
Copyright (C) 2015 maldevel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

__author__ = 'maldevel'

import json, ipaddress
from urllib import request
from geolocation.IpGeoLocation import IpGeoLocation

class IpGeoLocationLib:
"""Retrieve IP Geolocation information using http://ip-api.com website"""

def __init__(self):
pass

def GetInfo(self, ip):
"""Retrieve information"""
if self._isValidIPAddress(ip):
try:
req = request.urlopen('http://ip-api.com/json/{:s}'.format(ip))
if req.code == 200:
encoding = req.headers.get_content_charset()
return IpGeoLocation(json.loads(req.read().decode(encoding)))
except:
pass
return False


def _isValidIPAddress(self, ip):
"""Check if ip is a valid IPv4/IPv6 address"""
try:
ipaddress.ip_address(ip)
return True
except:
return False
19 changes: 19 additions & 0 deletions geolocation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
IPGeoLocation - Retrieve IP Geolocation information (http://ip-api.com)
Copyright (C) 2015 maldevel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

__author__ = 'maldevel'
79 changes: 79 additions & 0 deletions ip2geolocation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""
IPGeoLocation - Retrieve IP Geolocation information (http://ip-api.com)
Copyright (C) 2015 maldevel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""

__author__ = 'maldevel'

import argparse, sys
from argparse import RawTextHelpFormatter
from geolocation.IpGeoLocationLib import IpGeoLocationLib

VERSION = '1.0'

if __name__ == '__main__':

parser = argparse.ArgumentParser(description="""
IPGeoLocation {} - Retrieve IP Geolocation information
Powered by http://ip-api.com
""".format(VERSION), formatter_class=RawTextHelpFormatter)
parser.add_argument('-t', '--target', metavar='IP', type=str, dest='ip', default=None, help='IP Address')

if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)

args = parser.parse_args()

ipGeoLocRequest = IpGeoLocationLib()
IpGeoLocObj = ipGeoLocRequest.GetInfo(args.ip)

if IpGeoLocObj:
print("""
IPGeoLocation {} - Retrieve IP Geolocation information
Powered by http://ip-api.com
Results
IP: {}
ASN: {}
City: {}
Country: {}
Country Code: {}
ISP: {}
Latitude: {}
Longtitude: {}
Organization: {}
Region Code: {}
Region Name: {}
Timezone: {}
Zip Code: {}
""".format(VERSION,
IpGeoLocObj.IP,
IpGeoLocObj.ASN,
IpGeoLocObj.City,
IpGeoLocObj.Country,
IpGeoLocObj.CountryCode,
IpGeoLocObj.ISP,
IpGeoLocObj.Latitude,
IpGeoLocObj.Longtitude,
IpGeoLocObj.Organization,
IpGeoLocObj.Region,
IpGeoLocObj.RegionName,
IpGeoLocObj.Timezone,
IpGeoLocObj.Zip)
)

0 comments on commit f574bdf

Please sign in to comment.