#!/usr/bin/env python import os import django from pathlib import Path # Setup Django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.develop') django.setup() from city_detection_ip import CITY_DB_PATH import geoip2.database def test_maxmind_capabilities(): """Test what the MaxMind GeoLite2 database can actually do""" if not CITY_DB_PATH.exists(): print(f"โŒ Database file not found: {CITY_DB_PATH}") return print(f"โœ… Found MaxMind database: {CITY_DB_PATH}") try: with geoip2.database.Reader(CITY_DB_PATH) as reader: # Show what methods are available on the reader print("\n๐Ÿ“‹ Available methods on MaxMind reader:") methods = [method for method in dir(reader) if not method.startswith('_')] for method in methods: print(f" - {method}") # Test IP lookup (this works) print("\n๐Ÿงช Testing IP lookup (should work):") try: response = reader.city('8.8.8.8') # Google DNS IP print(f" IP 8.8.8.8 โ†’ City: {response.city.name}, Country: {response.country.iso_code}") except Exception as e: print(f" โŒ IP lookup failed: {e}") # Try to find coordinate-based methods (these won't exist) print("\n๐Ÿ” Checking for coordinate-based methods:") coord_methods = ['city_by_coords', 'location', 'reverse', 'geocode', 'coordinates', 'latlon'] found_coord_methods = [m for m in methods if any(coord in m.lower() for coord in coord_methods)] if found_coord_methods: print(f" โš ๏ธ Found potential coordinate methods: {found_coord_methods}") for method in found_coord_methods: try: method_obj = getattr(reader, method) print(f" - {method}: {method_obj}") except Exception as e: print(f" - {method}: Error - {e}") else: print(" โœ… No coordinate-based methods found (as expected)") # Demonstrate the limitation print("\n๐Ÿšซ Demonstrating the limitation:") print(" MaxMind databases only support IP โ†’ Location lookups") print(" They do NOT support Coordinates โ†’ Location lookups") print(" For reverse geocoding, you need a spatial database like our geonames_city table") except Exception as e: print(f"โŒ Error opening database: {e}") def test_coordinate_lookup_attempt(): """Try to 'force' a coordinate lookup to show it doesn't work""" print("\n๐ŸŽฏ Attempting coordinate lookup (this will fail):") if not CITY_DB_PATH.exists(): return try: with geoip2.database.Reader(CITY_DB_PATH) as reader: # Try various ways to pass coordinates (all will fail) test_coords = [ "5.161.94.84", # Tehran coordinates "35.698,51.4115", # String format [35.698, 51.4115], # List format ] for coords in test_coords: try: # This will raise AttributeError - no such method exists response = reader.city(coords) print(f" โŒ Unexpectedly succeeded with {coords}: {response}") except AttributeError as e: print(f" โœ… Expected error with {coords}: {e}") except Exception as e: print(f" โ“ Unexpected error with {coords}: {e}") except Exception as e: print(f"โŒ Database error: {e}") if __name__ == "__main__": print("๐Ÿงช Testing MaxMind GeoLite2 Database Capabilities") print("=" * 50) test_maxmind_capabilities() test_coordinate_lookup_attempt() print("\n" + "=" * 50) print("๐ŸŽฏ CONCLUSION:") print("MaxMind GeoLite2 databases are for IP geolocation only.") print("Use the geonames_city table for coordinate-based reverse geocoding.")