-
Phone Number Parsing: Utilizes the
phonenumberslibrary to parse and validate phone numbers, extracting country and national numbers. -
Location Lookup: Determines the approximate geographical location (country/region) associated with the phone number.
-
Service Provider Identification: Identifies the telecommunication service provider for the given number.
-
Geocoding: Converts the textual location into precise latitude and longitude coordinates using the OpenCage Geocoding API.
-
Interactive Map Generation: Creates an interactive HTML map (
myLocation.html) with a marker placed at the determined coordinates, showing the location details on hover.
Before running the script, ensure you have Python 3.x installed on your system. You will also need the following Python libraries:
-
phonenumbers -
folium -
opencage
You can install the required libraries using pip. You can run this command directly in your R Markdown document if you have reticulate configured to use a Python environment.
# Install Python packages if not already installed
# You might need to set up a Python environment for reticulate first
# For example: reticulate::py_install(packages = c("phonenumbers", "folium", "opencage"))
import subprocess
import sys
def install_package(package):
try:
__import__(package)
print(f"{package} is already installed.")
except ImportError:
print(f"Installing {package}...")
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
print(f"{package} installed successfully.")
install_package("phonenumbers")
install_package("folium")
install_package("opencage")
This project uses the OpenCage Geocoding API to convert location names into geographical coordinates. You will need to obtain a free API key:
-
Go to the OpenCage Geocoding website.
-
Sign up for a free account to get your API key.
Once you have your API key, replace 'YOUR API' in the provided script with your actual key:
Key='YOUR API' # Replace 'YOUR API' with your actual OpenCage Geocoding API KeyFollow these steps to run the script:
-
Create
number.py: In the same directory as your main script (e.g.,tracker.py), create a new Python file namednumber.py. This file will store the phone number you want to track.# number.py num = "+12025550100" # Replace with the target phone number, including the country code (e.g., +1 for USA, +44 for UK).
-
Save the main script: Save the provided Python code (your
tracker.pyor similar) in the same directory. Ensure you have replaced'YOUR API'with your actual OpenCage Geocoding API key. -
Run the script: Open your terminal or command prompt, navigate to the directory where you saved the files, and run the script:
python your_script_name.py
(Replace
your_script_name.pywith the actual name of your main Python script file.)Alternatively, if you have
reticulateset up in R, you could potentially run the Python script directly from an R chunk, though for a full script, running from the terminal is often more straightforward.# Example of how you might run the script if it were in a single file # Note: This assumes your Python environment is correctly configured with reticulate # and that the 'number.py' and 'tracker.py' files are accessible. # import subprocess # subprocess.run(["python", "your_script_name.py"])
Upon successful execution, the script will:
-
Print the detected location and service provider to your console.
-
Generate an HTML file named
myLocation.htmlin the same directory. Open this file in any web browser to view the interactive map with the phone number's approximate location marked.
-
Educational Use Only: This tool is intended for educational purposes and personal experimentation.
-
Accuracy: The accuracy of the location and service provider information is dependent on the data provided by the
phonenumberslibrary and the OpenCage Geocoding API. It may not always be precise or real-time. -
Privacy & Legal Compliance: Always ensure you have the legal right and explicit consent to track any phone number. Do not use this tool for illegal, unethical, or malicious activities. Respect privacy laws and regulations in your jurisdiction.