Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks and Updates #9

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ ipython_config.py
# pyenv
.python-version

# misc
.DS_Store
*.pem
*.crt
*.db
*.db-journal
*.log
*.log.*

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
Expand Down
58 changes: 18 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,22 @@ To make this project only you need to follow this step:-

## Installation

Install package with pip
### Install package with pip (Windows)

```bash
pip install phonenumbers
pip install folium
pip install geocoder
pip install opencage
pip install load_dotenv
```
### Install package with pip (Mac)
```
python3 -m pip install phonenumbers
python3 -m pip install folium
python3 -m pip install geocoder
python3 -m pip install opencage
python3 -m pip install load_dotenv
```

Now need to collect Geocoder API Key from https://opencagedata.com/
Expand All @@ -39,49 +48,18 @@ Step3: From API Keys collect API key

![github3](https://user-images.githubusercontent.com/123636419/215339773-0171d38c-b9ad-490a-95d8-47366321048a.PNG)

### Set API key


Create a `.env` file. In this file enter the following replacing `keyhere` with your API key:
```
OPENCAGE_API_KEY=keyhere
```

## Deployment
Simply run the file `main.py`!

To deploy this project run

```bash
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
import opencage
from opencage.geocoder import OpenCageGeocode
import folium


key = "your key" #Geocoder API Key need to paste here "your key"
number = input("please giver your number: ")
new_number = phonenumbers.parse(number)
location = geocoder.description_for_number(new_number, "en")
print(location)

service_name = carrier.name_for_number(new_number,"en")
print(service_name)

geocoder = OpenCageGeocode(key)
query = str(location)
result = geocoder.geocode(query)
#print(result)

lat = result[0]['geometry']['lat']
lng = result[0]['geometry']['lng']

print(lat,lng)

my_map = folium.Map(location=[lat,lng], zoom_start=9)
folium.Marker([lat, lng], popup= location).add_to(my_map)

my_map.save("location.html")

print("location tracking completed")
print("Thank you")
```
### Please note:
This cannot be used to accuratly locate someone phone number as per: [link](https://blog.opencagedata.com/post/we-can-not-convert-a-phone-number-into-a-location-sorry)


You can follow me
Expand Down
90 changes: 0 additions & 90 deletions location.html

This file was deleted.

83 changes: 61 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,73 @@
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
import opencage
from phonenumbers import geocoder, carrier
from opencage.geocoder import OpenCageGeocode
import folium
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

key = "your key" #Geocoder API Key needs to paste here "your key"
number = input("please giver your number: ")
new_number = phonenumbers.parse(number)
location = geocoder.description_for_number(new_number, "en")
print(location)
# Function to validate phone number format
def validate_phone_number(number):
try:
parsed_number = phonenumbers.parse(number)
if phonenumbers.is_valid_number(parsed_number):
return parsed_number
else:
return None
except phonenumbers.phonenumberutil.NumberParseException:
return None

service_name = carrier.name_for_number(new_number,"en")
print(service_name)
# Main function for location tracking
def track_location():
# Get OpenCage API Key from environment variable
opencage_api_key = os.getenv("OPENCAGE_API_KEY") ## Otherwise is -> opencage_api_key = "ENTERKEY"
if not opencage_api_key:
raise ValueError("OpenCage API Key not found. Please set it in the environment or in a .env file.")

geocoder = OpenCageGeocode(key)
query = str(location)
result = geocoder.geocode(query)
#print(result)
number = input("Please enter the phone number (with country code): ")

lat = result[0]['geometry']['lat']
lng = result[0]['geometry']['lng']
# Validate phone number format
parsed_number = validate_phone_number(number)
if not parsed_number:
print("Invalid phone number format. Please enter a valid phone number.")
return

print(lat,lng)
# Get location and carrier information
location = geocoder.description_for_number(parsed_number, "en")
service_name = carrier.name_for_number(parsed_number, "en")

my_map = folium.Map(location=[lat,lng], zoom_start=9)
folium.Marker([lat, lng], popup= location).add_to(my_map)
print(f"Location: {location}")
print(f"Service Provider: {service_name}")

my_map.save("location.html")
# Geocode using OpenCage API
geocoder_obj = OpenCageGeocode(opencage_api_key)
query = str(location)
try:
result = geocoder_obj.geocode(query)
if result and len(result):
lat = result[0]['geometry']['lat']
lng = result[0]['geometry']['lng']

print("location tracking completed")
print("Thank you")
# Create map and mark the location
my_map = folium.Map(location=[lat, lng], zoom_start=9)
folium.Marker([lat, lng], popup=location).add_to(my_map)

# Save map to HTML file
html_file = "location.html"
my_map.save(html_file)

# Open the HTML file in the default web browser
import webbrowser
webbrowser.open('file://' + os.path.realpath(html_file))

print("Location tracking completed. Map saved and opened in browser.")
else:
print("Location not found.")
except Exception as e:
print(f"Error geocoding: {e}")

print("Thank you")

track_location()