forked from daviddrysdale/python-phonenumbers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappspot.py
More file actions
executable file
·129 lines (109 loc) · 5.88 KB
/
Copy pathappspot.py
File metadata and controls
executable file
·129 lines (109 loc) · 5.88 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
"""
Script for comparison with https://phonenumber.appspot.com
"""
import getopt
import phonenumbers
import phonenumbers.carrier
import phonenumbers.geocoder
import phonenumbers.timezone
from phonenumbers import PhoneNumberFormat
import sys
import urllib.parse
def interactive_query():
number = prompt("Specify a Phone Number: ")
country = prompt_or_default("Specify a Default Country: ", None)
locale = prompt_or_default("Specify a locale for phone number geocoding: ", "en")
return (number, country, locale)
def prompt(msg):
result = input(msg)
return result.strip()
def prompt_or_default(msg, default):
result = prompt(msg)
if not result:
return default
else:
return result
def blank_if_none(val):
if val is None:
return ""
else:
return val
def appspot(number, country, locale):
print("\n\nPhone Number entered: %s" % number)
print("Default country entered: %s" % country)
print("Language entered: %s" % locale)
country = "ZZ" if country is None else country
numobj = phonenumbers.parse(number, country, keep_raw_input=True)
print("\nParsing result (parse(keep_raw_input=True))")
print("country_code : %s" % blank_if_none(numobj.country_code))
print("national_number : %s" % blank_if_none(numobj.national_number))
print("extension : %s" % blank_if_none(numobj.extension))
print("country_code_source : %s" % blank_if_none(phonenumbers.CountryCodeSource.to_string(numobj.country_code_source)))
print("italian_leading_zero : %s" % blank_if_none(numobj.italian_leading_zero))
print("number_of_leading_zeros : %s" % blank_if_none(numobj.number_of_leading_zeros))
print("raw_input : %s" % blank_if_none(numobj.raw_input))
print("preferred_domestic_carrier_code: %s" % blank_if_none(numobj.preferred_domestic_carrier_code))
valid = phonenumbers.is_valid_number(numobj)
print("\nValidation Results")
print("Result from isPossibleNumber() : %s" % phonenumbers.is_possible_number(numobj))
print("Result from isValidNumber() : %s" % valid)
if valid and country != "ZZ":
print("Result from isValidNumberForRegion(): %s" % phonenumbers.is_valid_number_for_region(numobj, country))
print("Phone Number region : %s" % phonenumbers.region_code_for_number(numobj))
print("Result from getNumberType() : %s" % phonenumbers.PhoneNumberType.to_string(phonenumbers.number_type(numobj)))
print("\nFormatting Results")
print("E164 format : %s" % (phonenumbers.format_number(numobj, PhoneNumberFormat.E164) if valid else "invalid"))
print("Original format : %s" % phonenumbers.format_in_original_format(numobj, country))
print("National format : %s" % phonenumbers.format_number(numobj, PhoneNumberFormat.NATIONAL))
print("International format : %s" % (phonenumbers.format_number(numobj, PhoneNumberFormat.INTERNATIONAL) if valid else "invalid"))
print("Out-of-country format from US : %s" % (phonenumbers.format_out_of_country_calling_number(numobj, "US") if valid else "invalid"))
print("Out-of-country format from CH : %s" % (phonenumbers.format_out_of_country_calling_number(numobj, "CH") if valid else "invalid"))
print("Format number for mobile dialing (calling from US) : %s" % (phonenumbers.format_number_for_mobile_dialing(numobj, "US", True) if valid else "invalid"))
print("Format for national dialing with preferred carrier code: %s" % (phonenumbers.format_national_number_with_carrier_code(numobj, "") if valid else "invalid"))
print(" and empty fallback carrier code")
print("\nAsYouTypeFormatter Results")
formatter = phonenumbers.AsYouTypeFormatter(country)
for i in range(len(number)):
input_char = number[i]
print("Char entered '%s' Output: %s" % (input_char, formatter.input_digit(input_char)))
if valid:
print("\nPhoneNumberOfflineGeocoder Results")
print("Location: %s" % phonenumbers.geocoder.description_for_number(numobj, locale))
print("\nPhoneNumberToTimeZonesMapper Results")
print("Time zone(s): [%s]" % ", ".join(phonenumbers.timezone.time_zones_for_number(numobj)))
print("\nPhoneNumberToCarrierMapper Results")
print("Carrier: %s" % phonenumbers.carrier.name_for_number(numobj, locale))
print("\nPython library version: %s" % phonenumbers.__version__)
print("\nCompare with: https://libphonenumber.appspot.com/phonenumberparser?number=%s&country=%s" % (urllib.parse.quote_plus(number), country))
def usage():
print("./appspot.py [opts]")
print(" --number <val> / -n <val> : number to parse")
print(" --country <val> / -c <val> : default country for parsing (default None)")
print(" --locale <val> / -l <val> : language (default None)")
print(" --help / -h : show this message")
if __name__ == '__main__':
number = None
country = None
locale = None
try:
opts, args = getopt.getopt(sys.argv[1:], "hn:c:l:", ["help", "number=", "country=", "locale="])
except getopt.GetoptError as err:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-n", "--number"):
number = a
elif o in ("-c", "--country"):
country = a
elif o in ("-l", "--locale"):
locale = a
else:
assert False, "unhandled option"
if number is None:
(number, country, locale) = interactive_query()
appspot(number, country, locale)