Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ChatBot_A6/Locator.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
18 lines (14 sloc)
689 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This module was written by Humza Shahid. | |
import pygeoip | |
from requests import get | |
def userLocator(): | |
"""Gets user's city and country from their IP address, and then returns the city and country""" | |
#note: database uses different files for finding user's city and country | |
#note: (GeoIP.dat is for countries and GeoLiteCity.dat is for cities) | |
#note: so two different files and variables are needed to get required info. | |
gi = pygeoip.GeoIP('GeoIP.dat') | |
gic = pygeoip.GeoIP('GeoLiteCity.dat') | |
ip = get('https://api.ipify.org').text | |
userCity = gic.record_by_addr(ip)['city'] | |
userCountry = gi.country_name_by_addr(ip) | |
return userCity, userCountry |