How to use the phonenumbers.format_number function in phonenumbers

To help you get started, we’ve selected a few phonenumbers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jpf / sms-via-email / app.py View on Github external
def email_to_phone(from_email):
    '''Converts an email address like 14155551212@sms.example.com
       into a phone number like +14155551212'''
    (username, domain) = from_email.split('@')

    potential_number = '+' + username
    try:
        ph_num = ph.parse(potential_number, 'US')
        return ph.format_number(ph_num, ph.PhoneNumberFormat.E164)
    except:
        raise InvalidPhoneNumberInEmail(from_email)
github sundowndev / phoneinfoga / scanners / localscan.py View on Github external
test("Running local scan...")

    FormattedPhoneNumber = "+" + formatNumber(InputNumber)

    try:
        PhoneNumberObject = phonenumbers.parse(FormattedPhoneNumber, None)
    except Exception as e:
        throw(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")
github CroceRossaItaliana / jorvik / anagrafica / models.py View on Github external
def e164(self):
        """
        Ritorna il numero formattato come numero internazionale E.164.
        :return: Stringa che rappresenta il numero in formato internazionale E.164.
        """
        return phonenumbers.format_number(self._phonenumber(), phonenumbers.PhoneNumberFormat.E164)
github CroceRossaItaliana / jorvik / import.py View on Github external
def parse_numero(numero, paese="IT"):
    try:
        n = phonenumbers.parse(numero, paese)
    except phonenumbers.phonenumberutil.NumberParseException:
        return numero
    f = phonenumbers.format_number(n, phonenumbers.PhoneNumberFormat.E164)
    return f
github jhpyle / docassemble / docassemble_base / docassemble / base / functions.py View on Github external
def phone_number_in_e164(number, country=None):
    """Given a phone number and a country code, returns the number in
    E.164 format.  Returns None if the number could not be so formatted."""
    if country is None:
        country = get_country()
    try:
        pn = phonenumbers.parse(number, country)
        output = phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.E164)
    except:
        return None
    return output
github oddbird / portfoliyo / portfoliyo / model / users / migrations / 0005_update_phone_numbers.py View on Github external
"""
    if not value:
        return None

    if value[0] == '+':
        # Phone number may already be in E.164 format.
        parse_type = None
    else:
        # Assume US format otherwise
        parse_type = 'US'
    try:
        phone_representation = phonenumbers.parse(value, parse_type)
    except phonenumbers.NumberParseException:
        return None
    else:
        return phonenumbers.format_number(
            phone_representation, phonenumbers.PhoneNumberFormat.E164)
github democraciaconcodigos / escrutinio-social / fiscales / forms.py View on Github external
def validar_telefono(valor):
    valor = valor.strip()
    if valor.startswith(('15', '4')):
        valor = f'{CARACTERISTICA_DEFAULT} {valor}'
    elif valor.startswith('11') and 9 <= len(valor) <= 11:
        valor = f'9 {valor}'
    valor = phonenumbers.parse(valor, 'AR')
    formato = phonenumbers.PhoneNumberFormat.INTERNATIONAL
    valor = phonenumbers.format_number(valor, formato)
    return valor