Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
PhoneNumberObject = phonenumbers.parse(FormattedPhoneNumber, None)
except Exception as e:
print(e)
else:
if not phonenumbers.is_valid_number(PhoneNumberObject):
return False
number = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164).replace("+", "")
numberCountryCode = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL).split(" ")[0]
numberCountry = phonenumbers.region_code_for_country_code(int(numberCountryCode))
localNumber = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164).replace(numberCountryCode, "")
internationalNumber = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
country = geocoder.country_name_for_number(PhoneNumberObject, "en")
location = geocoder.description_for_number(PhoneNumberObject, "en")
carrierName = carrier.name_for_number(PhoneNumberObject, "en")
if print_results:
print("International format: {}".format(internationalNumber))
print("Local format: {}".format(localNumber))
print("Country found: {} ({})".format(country, numberCountryCode))
print("City/Area: {}".format(location))
print("Carrier: {}".format(carrierName))
for timezoneResult in timezone.time_zones_for_number(PhoneNumberObject):
print("Timezone: {}".format(timezoneResult))
if phonenumbers.is_possible_number(PhoneNumberObject):
print("The number is valid and possible.")
else:
print("The number is valid but might not be possible.")
).replace("+", "")
numberCountryCode = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL
).split(" ")[0]
numberCountry = phonenumbers.region_code_for_country_code(
int(numberCountryCode)
)
localNumber = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164
).replace(numberCountryCode, "")
internationalNumber = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL
)
country = geocoder.country_name_for_number(PhoneNumberObject, "en")
location = geocoder.description_for_number(PhoneNumberObject, "en")
carrierName = carrier.name_for_number(PhoneNumberObject, "en")
if print_results:
plus("International format: {}".format(internationalNumber))
plus("Local format: {}".format(localNumber))
plus("Country found: {} ({})".format(country, numberCountryCode))
plus("City/Area: {}".format(location))
plus("Carrier: {}".format(carrierName))
for timezoneResult in timezone.time_zones_for_number(PhoneNumberObject):
plus("Timezone: {}".format(timezoneResult))
if phonenumbers.is_possible_number(PhoneNumberObject):
info("The number is valid and possible.")
else:
warn("The number is valid but might not be possible.")
).replace("+", "")
numberCountryCode = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL
).split(" ")[0]
numberCountry = phonenumbers.region_code_for_country_code(
int(numberCountryCode)
)
localNumber = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164
).replace(numberCountryCode, "")
internationalNumber = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL
)
country = geocoder.country_name_for_number(PhoneNumberObject, "en")
location = geocoder.description_for_number(PhoneNumberObject, "en")
carrierName = carrier.name_for_number(PhoneNumberObject, "en")
if print_results:
plus("International format: {}".format(internationalNumber))
plus("Local format: {}".format(localNumber))
plus("Country found: {} ({})".format(country, numberCountryCode))
plus("City/Area: {}".format(location))
plus("Carrier: {}".format(carrierName))
for timezoneResult in timezone.time_zones_for_number(PhoneNumberObject):
plus("Timezone: {}".format(timezoneResult))
if phonenumbers.is_possible_number(PhoneNumberObject):
info("The number is valid and possible.")
else:
warn("The number is valid but might not be possible.")
numberCountryCode = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL
).split(" ")[0]
numberCountry = phonenumbers.region_code_for_country_code(
int(numberCountryCode)
)
localNumber = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164
).replace(numberCountryCode, "")
internationalNumber = phonenumbers.format_number(
PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL
)
country = geocoder.country_name_for_number(PhoneNumberObject, "en")
location = geocoder.description_for_number(PhoneNumberObject, "en")
carrierName = carrier.name_for_number(PhoneNumberObject, "en")
if print_results:
plus("International format: {}".format(internationalNumber))
plus("Local format: {}".format(localNumber))
plus("Country found: {} ({})".format(country, numberCountryCode))
plus("City/Area: {}".format(location))
plus("Carrier: {}".format(carrierName))
for timezoneResult in timezone.time_zones_for_number(PhoneNumberObject):
plus("Timezone: {}".format(timezoneResult))
if phonenumbers.is_possible_number(PhoneNumberObject):
info("The number is valid and possible.")
else:
warn("The number is valid but might not be possible.")
u"value": value,
u"value_normalized": value_normalized,
u"note": None,
u"body_offset_start": match.start(),
u"body_offset_stop": match.end(),
u"excerpt": excerpt,
u"excerpt_value_start": excerpt_value_start,
u"excerpt_value_stop": excerpt_value_stop,
u"possible_area": None,
u"possible_carrier": None
}
# If the phonenumbers module was able to construct an actual phone number object, attempt to
# add some notes about the possible geographic region and telco carrier.
if phone_number_obj is not None:
area_name = geocoder.description_for_number(phone_number_obj, "en")
if area_name:
entity_dict[u'possible_area'] = u"Possible area: %s. " % area_name
carrier_name = carrier.name_for_number(phone_number_obj, "en")
if carrier_name:
entity_dict[u'possible_carrier'] = u"Possible carrier: %s." % carrier_name
tagged_phone_entities.append(entity_dict)
return tagged_phone_entities
q = {
"fields": ["file"],
"query": {
"term": {"file": query}
}
}
r = es.search(body=q, index=es_index)
data = r['hits']['hits']
urls = []
pn = []
for doc in data:
urls.append(re.findall(r'(https?://[^\s]+)', doc['fields']['file'][0]))
try:
for match in phonenumbers.PhoneNumberMatcher(doc['fields']['file'][0], region=None):
pn.append({'number': phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164),
'location': geocoder.description_for_number(match.number, "en")})
except KeyError:
pass
urls = filter(lambda x: x != [], urls)
# urls_flat=reduce(lambda x,y: x.extend(y),urls)
urls_flat = [item for sublist in urls for item in sublist]
return json.dumps({'urls': dict(Counter(urls_flat)), 'pn': pn})
def _parse_number(num, local_region, rates):
y = phonenumbers.parse(num, local_region)
#if phonenumbers.is_possible_number(y) == False:
# print("Isn't possible number:", num)
#if phonenumbers.is_valid_number(y) == False:
# print("Isn't valid number:", num)
output_num = phonenumbers.format_number(
y,
phonenumbers.PhoneNumberFormat.E164,
)
region_code = geocoder.region_codes_for_country_code(y.country_code)[0]
international = False
mobile = False
premium = False
if PhoneNumberType.MOBILE == phonenumbers.number_type(y):
mobile = True
if PhoneNumberType.PREMIUM_RATE == phonenumbers.number_type(y):
premium = True
if region_code != local_region:
international = True
fraud_level, pricing = rates.get_fraud_level_and_rate(output_num[1:])
return {
'premium': premium,
'mobile': mobile,
def bootstrap(config):
"""
This is very naughty and I should be ashamed of myself,
but the geocoder module imports some auto-executing
garbage that takes a sinister amount of time to execute.
I'm doing this so someone can import the CahootsParser
without having to take a nap while waiting for it to import.
Assuming this issue is ever corrected on the phonenumbers
end, I'll remove this obscene bit of code.
"""
from phonenumbers import geocoder as coder
# pylint: disable=invalid-name,global-statement
global geocoder
geocoder = coder
def __init__(self, ctx, label, start, end):
super(PhoneResult, self).__init__(ctx, label, start, end)
number = self._parse(label)
for country in ctx.countries:
if number is None:
number = self._parse(label, country)
self.valid = number is not None
if number is not None:
self.countries = [geocoder.region_code_for_number(number)]
self.label = phonenumbers.format_number(number, self.FORMAT)
self.key = self.label
def country_hint(self, value):
try:
number = parse_number(value)
return geocoder.region_code_for_number(number).lower()
except NumberParseException:
pass