move-coop / parsons

A python library of connectors for the progressive community.
https://www.parsonsproject.org/
Other
261 stars 132 forks source link

Phone Number Validation/Parsing #28

Open jburchard opened 5 years ago

jburchard commented 5 years ago

We should add in phone number validation/parsing tools.

mklaber commented 4 years ago

@jburchard I've done some phone cleansing and validation recently using phonenumbers. While my work mostly not relevant to Parsons nor does it really fit under the licensing agreements, here's a snippet of code that I wrote that could be helpful to you. It does a couple things:

  1. Tries to parse the phone number as-is (obviously change from GB to US). If it parses and it's valid, return it
  2. If it's not valid or can't parse it, it'll switch to the PhoneNumberMatcher and try to extract multiple phone numbers from it, returns all that are valid

I've found it helpful in dealing with data where users enter multiple phone numbers in the same field (e.g., 5075554323,8005551234).

def find_phone_numbers(s):
    try:
        p = phonenumbers.parse(s, "GB")
        if phonenumbers.is_valid_number(p):
            # it's a singularly valid phone number. Just return it.
            # Otherwise, go for the PhoneNumberMatcher
            return [p]
    except NumberParseException as e:
        print(e)
    matches = []
    for match in phonenumbers.PhoneNumberMatcher(s, "GB"):
        # print(match)
        if phonenumbers.is_valid_number(match.number):
            matches.append(match.number)
    return matches
aemrich commented 4 years ago

Hey, I'm interested in working on this issue (already talked with @eliotst a bit about it). Let me know if there's anything I should know -- as my starting point, I'm going to look into implementing something similar to what @mklaber describes above. Thanks!

iamwpj commented 4 years ago

Is someone working on this? I saw this blog post from Twilio about verifying numbers and noticed that it parses pretty reliably too. https://www.twilio.com/blog/2016/02/how-to-verify-phone-numbers-in-python-with-the-twilio-lookup-api.html

Simply calling it and passing a number formatted and some random way will allow you to spit out a standard format.

c = Client(ACCOUNT_SID,AUTH_TOKEN)

number = sys.argv[1]
if number:
    checked = c.lookups.phone_numbers(number).fetch()
    print(f"{number}\t --> {checked.national_format}")
else:
    print("Please specify a phone number.")

Produces:

3192663333       --> (319) 266-3333
+1-319.266.3333  --> (319) 266-3333
319-266-3333     --> (319) 266-3333
(319) 266-3333   --> (319) 266-3333
319 266 3333     --> (319) 266-3333
319-266-3333     --> (319) 266-3333
jburchard commented 4 years ago

Nope. That’s really cool, though

On Sun, Sep 27, 2020 at 10:24 AM iamwpj notifications@github.com wrote:

Is someone working on this? I saw this blog post from Twilio about verifying numbers and noticed that it parses pretty reliably too. https://www.twilio.com/blog/2016/02/how-to-verify-phone-numbers-in-python-with-the-twilio-lookup-api.html

Simply calling it and passing a number formatted and some random way will allow you to spit out a standard format.

c = Client(ACCOUNT_SID,AUTH_TOKEN)

number = sys.argv[1]

if number:

checked = c.lookups.phone_numbers(number).fetch()

print(f"{number}\t --> {checked.national_format}")

else:

print("Please specify a phone number.")

Produces:

3192663333 --> (319) 266-3333

+1-319.266.3333 --> (319) 266-3333

319-266-3333 --> (319) 266-3333

(319) 266-3333 --> (319) 266-3333

319 266 3333 --> (319) 266-3333

319-266-3333 --> (319) 266-3333

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/move-coop/parsons/issues/28#issuecomment-699649042, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKGDO7IKOY7R57S6TQLCMTSH5KMFANCNFSM4IYA6X2A .

shaunagm commented 4 years ago

Hi @iamwpj, you are welcome to take this on! Are you in our Slack, by the way? If so, can you ping me there so I can connect your github handle to your name? And if not, would you like to join us?

schlich commented 4 years ago

@shaunagm , i just stumbled across this project and am absolutely ecstatic that this package exists. I would love to be added to the Slack if you guys would have me!!! email is tyler.schlichenmeyer@gmail.com

austinweisgrau commented 3 years ago

Like @mklaber, at Movement Labs we use the phonenumbers package regularly for basic phone number validation and normalization. Here's a snippet of our basic use case:

def parse_cell(cell):
    try:
        return phonenumbers.parse(str(cell), 'US').national_number
    except phonenumbers.NumberParseException:
        return None
shaunagm commented 1 year ago

Seems related to #554

mkrausse-ggtx commented 11 months ago

I've submitted a pull request that created a format phone numbers. I decided not to use the phonenumbers package because I didn't want to add a new dependency to the mix. I also realized that all the utilities test is in one file. I figured we should break that out into individual files for each method test. https://github.com/move-coop/parsons/pull/928

Some additions to this method could be accepting args for which format you want the number in but I went with E.164 which is the international standard.