Created
December 8, 2022 12:33
-
-
Save Lucho00Cuba/6f9aa2eebf81e0655335e8c3a3e89a3a to your computer and use it in GitHub Desktop.
Python Geolocator
This file contains hidden or 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
| import requests | |
| import json | |
| import sys | |
| import os | |
| from flask import request | |
| from flask import jsonify | |
| from flask import Flask | |
| from flask import render_template | |
| from flask_cors import CORS | |
| def get_ip(): | |
| response = requests.get('https://api64.ipify.org?format=json').json() | |
| return response["ip"] | |
| def get_location(ip_address=None): | |
| if ip_address is None: | |
| ip_address = get_ip() | |
| response = requests.get(f'https://ipapi.co/{ip_address}/json/').json() | |
| location_data = { | |
| "ip": ip_address, | |
| "city": response.get("city"), | |
| "region": response.get("region"), | |
| "country": response.get("country_name"), | |
| "latitude": response.get("latitude"), | |
| "longitude": response.get("longitude"), | |
| "provider": response.get("org"), | |
| "postal_code": response.get("postal") | |
| } | |
| return location_data | |
| def slack(message=None): | |
| if message is None: | |
| return 1 | |
| url = "https://hooks.slack.com/services/${WEBHOOK}" | |
| title = (f"Client IP :zap:") | |
| slack_data = { | |
| "username": "Maverick", | |
| "icon_emoji": ":airplane:", | |
| #"channel" : "#somerandomcahnnel", | |
| "attachments": [ | |
| { | |
| "color": "#2348ad", | |
| "fields": [ | |
| { | |
| "title": title, | |
| "value": message, | |
| "short": "false", | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| byte_length = str(sys.getsizeof(slack_data)) | |
| headers = {'Content-Type': "application/json", 'Content-Length': byte_length} | |
| response = requests.post(url, data=json.dumps(slack_data), headers=headers) | |
| if response.status_code != 200: | |
| raise Exception(response.status_code, response.text) | |
| return 0 | |
| # Creating the instance of the class Flask | |
| app = Flask(__name__) | |
| CORS(app, resources={r"/*": {"origins": "*"}}) | |
| @app.route("/get_my_ip", methods=["GET"]) | |
| def get_my_ip(): | |
| return jsonify({'ip': get_location()}), 200 | |
| @app.route("/", methods=["GET"]) | |
| def home(): | |
| req = get_location() | |
| message = f"IP: {req['ip']}\nCity: {req['city']}\nRegion: {req['region']}\nCountry: {req['country']}\nLatitude: {req['latitude']}\nLongitude: {req['longitude']}\nProvider: {req['provider']}\nPostal_Code: {req['postal_code']}" | |
| print(message) | |
| if slack(message) == 0: | |
| print("Message send ok") | |
| else: | |
| print("Message send fail") | |
| return "Hello from Geo-Py" | |
| # Run the application | |
| if __name__ == '__main__': | |
| app.run(debug=True, host="0.0.0.0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment