Skip to content

Add GeolocationLookup #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions GeolocationLookup/GeolocationLookup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
import json
import argparse
import requests

# Define functions

def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-k', '--apikey', metavar='IP2Location.io API Key.')
parser.add_argument('-p', '--ip', metavar='Specify an IP address.')

return parser

# Define variables
keyless = False
self_ip_query = False
parameters = {}

if len(sys.argv) > 1:
# check if API key is provided or not
parser = create_parser()
args = parser.parse_args(sys.argv[1:])
if args.apikey is None:
keyless = True
else:
parameters['key'] = args.apikey
if args.ip is None:
self_ip_query = True
else:
parameters['ip'] = args.ip
else:
# Assuming using Keyless API
keyless = True

if keyless:
print(f"Calling keyless API now...")
if len(parameters) > 0:
r = requests.get('https://api.ip2location.io/', params=parameters)
else:
r = requests.get('https://api.ip2location.io/')
else:
print(f"Calling API now...")
if len(parameters) > 0:
r = requests.get('https://api.ip2location.io/', params=parameters)
else:
r = requests.get('https://api.ip2location.io/')

if r.json() == None:
print(f"Unexpected error, please try again later.")
elif 'error' in r.json():
print(f"IP2Location.io API error: {r.json()['error']['error_message']}")
else:
print(r.json())
43 changes: 43 additions & 0 deletions GeolocationLookup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Geolocation Lookup

This script make use of [IP2Location.io API](https://www.ip2location.io/) to query enrich geolocation information for an IP address. The script supports both normal API call and keyless API call. The keyless API had a limitation of 500 queries per day. Once this limit is reached, any additional API calls will fail until the time period resets at 00:00 UTC daily.

## Parameters

|Parameter| Description |
|--|--|
|-k, --apikey | IP2Location.io API Key. You can sign up a free one from https://www.ip2location.io/sign-up. If not provided, the script will use keyless API. |
|-p, --ip | IP address to be query for. If not provided, the source IP address will be use to query. |


## Usage

Before running the script, make sure install the required libraries first:

```
pip install -r requirements.txt
```

To perform normal query with the API Key, you can use `python GeolocationLookup.py -k <your_IP2Locatio.io_API_Key> -p <ip_address>`. Replaced the relevant values before run the command.

For keyless API call, you can run`python GeolocationLookup.py` or `python GeolocationLookup.py -p <ip_address>`.

For a successful call, you shall getting a JSON result printed out. For instance, you will see the following output for the IP address 8.8.8.8 for keyless API:

```json
{
"ip": "8.8.8.8",
"country_code": "US",
"country_name": "United States of America",
"region_name": "California",
"city_name": "Mountain View",
"latitude": 37.38605,
"longitude": -122.08385,
"zip_code": "94035",
"time_zone": "-07:00",
"asn": "15169",
"as": "Google LLC",
"is_proxy": false,
"message": "Limit to 500 queries per day. Sign up for a Free plan at https://www.ip2location.io to get 30K queries per month."
}
```
1 change: 1 addition & 0 deletions GeolocationLookup/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ So far, the following projects have been integrated to this repo:
|[Frammed text generator](FramedText) | [jcdwalle](https://github.com/jcdwalle)|
|[Get Time By TimeZone](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Get_Time_TimezoneWise)|[Parth Shah](https://github.com/codingis4noobs) |
|[git_automation](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/git_automation)| [loge1998](https://github.com/loge1998)|
|[Geolocation Lookup](GeolocationLookup)| [IP2Location](https://github.com/ip2location) |
|[Github repo creator](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Git_repo_creator)|[Harish Tiwari ](https://github.com/optimist2309)|
|[Github Review Bot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Github-Review-Bot)|[Gaurav Giri](https://github.com/gaurovgiri)|
|[GithubBot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Github_Bot)|[Abhilasha](https://github.com/Abhilasha06)|
Expand Down