-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
48 lines (42 loc) · 2.1 KB
/
main.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from fastapi import FastAPI, Query
from typing import Union
import phonenumbers
from phonenumbers import geocoder
from pydantic import BaseModel
import markdown2
from fastapi.responses import HTMLResponse
from phonenumbers import carrier
from phonenumbers import timezone
class PhoneNumberDetail(BaseModel):
country: Union[str, None]
countryOrLocation: Union[str, None]
carrier: Union[str, None]
country_code: Union[int, None]
national_number: Union[int, None]
country_code_source: Union[int, None]
country_code_source: Union[int, None]
is_possible_number: Union[bool] = False
is_valid_number: Union[bool] = False
time_zones_for_number: Union[list, None]
app = FastAPI()
@app.get("/",include_in_schema=False,response_class=HTMLResponse)
def root():
with open("README.md","r",encoding="utf-8") as file:
readme_content=file.read()
return markdown2.markdown(readme_content)
@app.get("/get_phone_number_details",response_model=PhoneNumberDetail,description="Checks if phone number is valid and returns country of number.")
def get_phone_number_details(phone_number:str=Query(description="Phone number starting with + (Example: +442083661177)")):
if(phone_number.startswith(" ")):
phone_number="+"+phone_number[1:]
try:
phone_number_details=phonenumbers.parse(phone_number)
phone_number_details_dict=phone_number_details.__dict__
phone_number_details_dict['country']=geocoder.country_name_for_number(phone_number_details, "en")
phone_number_details_dict['countryOrLocation']=geocoder.description_for_number(phone_number_details,"en")
phone_number_details_dict['carrier']=carrier.name_for_number(phone_number_details, "en")
phone_number_details_dict['is_possible_number']=phonenumbers.is_possible_number(phone_number_details)
phone_number_details_dict['is_valid_number']=phonenumbers.is_valid_number(phone_number_details)
phone_number_details_dict['time_zones_for_number'] = timezone.time_zones_for_number(phone_number_details)
except:
return PhoneNumberDetail()
return phone_number_details_dict