Python: Tracking any phone number

 Did you know Python is capable of getting the location of any person just by knowing his/her phone number? In this exercise, we will see how this is possible. Once again, this tutorial is only for educational and entertainment purposes.

The libraries geocoder, and carrier libraries from the phonenumbers library should be imported. These libraries will help with the purpose of this exercise. Let's start!


import phonenumbers

from phonenumbers import geocoder

from phonenumbers import carrier

#Introducing the phone number to be tracked

number = '+51 XXXXXXXXX'

phone_number = phonenumbers.parse(number)

carrier = carrier.name_for_number(phone_number, 'es')

region = geocoder.description_for_number(phone_number,'es')




As seen from the pictures, Python can output the name of the region in any language. If you want, for example, in English, you write 'en' instead of 'es', or 'de' for German, or 'ru' for Russian. The output of the variable carrier is the phone company that provides the given phone number.

Python is also able to point on the map the location of the given phone number. How to do so? As you may know, every single location can be defined according to its latitude and longitude coordinates. If you go to Google maps and click on any city, the latitude and longitude coordinates for this specific place will be shown. 

However, if you just introduce these numbers into Python, it won't understand. What is the solution then? It is necessary then to get an API key code that will convert the coordinates to a place. We can get the API code by creating an account in OpenCage. Here is the link: https://opencagedata.com/

In Python, there is a library named opencage.geocoder. As explained, this library and the API key will be used to help Python convert the latitude and longitude coordinates to the corresponding place.


from opencage.geocoder import OpenCageGeocode

key = 'XXXXXXXXXXXXXXXXXXXXXXXX'

geocoder = OpenCageGeocode(key)

query = str(region)

results = geocoder.geocode(query)

print(results)

lat = results[0]['geometry']['lat']

lng = results[0]['geometry']['lng']

print(lat, lng)


Now you just got the latitude and longitude coordinates. If you copy them into Google Maps, you will be able to see the location! But, you can also 'create' your own map in a nice way in Python. For this purpose, the library folium is needed. This library makes possible the visualization of the outputted coordinates into an interactive map.


import folium

my_map = folium.Map(location=[lat,lng], zoom_start = 5)

folium.Marker([lat,lng], popup = region).add_to((my_map))


Now, it is time to save the map in an html file. This is very simple to do.


my_map.save('Location.html')


In the folder you have saved your Python script, you will find a new file, the html one. If you click on it, you will be able to see the place, as shown below.





Important: 

1. When you enter your desired phone number, please do not forget the country's code. It is very important!

2. Due to companies' security and policy reasons, the location got from the Python script is not accurate. 

Comments

Popular posts from this blog

Python: Pandas DataFrame data manipulation

Python: Drawing a heart