Esempio n. 1
0
    def extract_phone(self, field_value):
        """
        Parses the input string to create a PhoneNumber entity with all the possible
        values.

        Args:
            field_value -> The value of the field where the information will be
                collected from
        """
        try:
            parsed = phonenumbers.parse(field_value)
        except phonenumbers.NumberParseException as e:
            # The phone number couldn't be parsed
            return None

        # Checks if it could be a phone number, but not if it could be real
        if phonenumbers.is_possible_number(parsed):

            nsn = str(phonenumbers.national_significant_number(parsed))
            ndc_len = phonenumbers.length_of_national_destination_code(parsed)

            if ndc_len > 0:
                area_code = nsn[:ndc_len]
                lastnumbers = nsn[ndc_len:]
            else:
                area_code = None
                lastnumbers = nsn

            return ents.PhoneNumber(phonenumber=field_value,
                                    countrycode=parsed.country_code,
                                    areacode=area_code if area_code else None,
                                    lastnumbers=lastnumbers)

        return None
Esempio n. 2
0
def _invalid_phone_number(s):
    try:
        # parse input as a US number, unless a leading + is provided,
        # in which case the input will be validated according to the country code
        parsed = phonenumbers.parse(s, 'US')
    except phonenumbers.phonenumberutil.NumberParseException:
        # could not be parsed due to unexpected characters
        return True

    if not phonenumbers.is_possible_number(parsed):
        # could not be a phone number due to length, invalid characters, etc
        return True
    elif parsed.country_code == 1 and phonenumbers.length_of_national_destination_code(parsed) == 0:
        # US number does not contain area code
        return True

    return False