-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-phone.py
More file actions
27 lines (21 loc) · 984 Bytes
/
test-phone.py
File metadata and controls
27 lines (21 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import phonenumbers
from phonenumbers import geocoder
def get_country_from_phone(phone_str, default_region="JP"):
try:
# Remove spaces and dashes
cleaned = phone_str.strip().replace(" ", "").replace("-", "")
# If the number starts with +, use None as region (E.164 format)
if cleaned.startswith("+"):
number = phonenumbers.parse(cleaned, None)
else:
number = phonenumbers.parse(cleaned, default_region)
if not phonenumbers.is_valid_number(number):
return "Invalid number"
country = geocoder.description_for_number(number, "en")
return country
except phonenumbers.NumberParseException as e:
return f"Error: {str(e)}"
# Examples
print(get_country_from_phone("+819012345678")) # => Japan
print(get_country_from_phone("1310720562")) # => Japan (with "JP" as default region)
print(get_country_from_phone("12025550123", "US")) # => United States