@@ -112,6 +112,19 @@ def __init__(self):
112112 "Belgium" : "België" ,
113113 "Austria" : "Österreich" ,
114114 }
115+ # Country code mapping for different regions
116+ self .country_codes = {
117+ "US" : "1" ,
118+ "CA" : "1" ,
119+ "FR" : "33" ,
120+ "DE" : "49" ,
121+ "GB" : "44" ,
122+ "IT" : "39" ,
123+ "PL" : "48" ,
124+ "ES" : "34" ,
125+ "BE" : "32" ,
126+ "AT" : "43" ,
127+ }
115128
116129 self .fake = None
117130 self .locale = None
@@ -556,23 +569,18 @@ def normalize_regional_phone_numbers(self, phone: str, region: str) -> str:
556569 str
557570 The normalized phone number in the format <country-code><number>.
558571 """
572+ # Handle leading zero in local numbers before country code is removed
573+ if region not in ["US" , "CA" ] and phone .startswith ("0" ):
574+ # Remove the leading zero
575+ phone = phone [1 :]
559576
560- # Country code mapping for different regions
561- country_codes = {
562- "US" : "1" ,
563- "CA" : "1" ,
564- "FR" : "33" ,
565- "DE" : "49" ,
566- "GB" : "44" ,
567- "IT" : "39" ,
568- "PL" : "48" ,
569- "ES" : "34" ,
570- "BE" : "32" ,
571- "AT" : "43" ,
572- }
577+ # Fix Austrian phone number duplication issue before processing
578+ if region == "AT" and "4343" in phone :
579+ # Remove the duplicated country code
580+ phone = phone .replace ("4343" , "43" )
573581
574582 # If phone is already normalized, return as it is
575- expected_country_code = country_codes .get (region )
583+ expected_country_code = self . country_codes .get (region )
576584 if (
577585 expected_country_code
578586 and phone .isdigit ()
@@ -581,17 +589,13 @@ def normalize_regional_phone_numbers(self, phone: str, region: str) -> str:
581589 ):
582590 return phone
583591
584- # Fix Austrian duplicated country code before processing
585- if region == "AT" and phone .startswith ("+4343" ):
586- phone = "+43" + phone [5 :] # Remove duplicated 43
587-
588592 # Sub out anything that matches this regex statement with an empty string to get rid of extensions in generated phone numbers
589593 phone = re .sub (r"\s*(?:x|ext)\s*\d*$" , "" , phone , flags = re .IGNORECASE )
590594 # Sub out anything that is not a digit with the empty string to ensure the phone number is formatted with no spaces or special characters
591595 digits = re .sub (r"\D" , "" , phone )
592596
593597 # Determine country code
594- country_code = country_codes .get (
598+ country_code = self . country_codes .get (
595599 region , "1"
596600 ) # Default to "1" (US/CA) if the region is unknown
597601 # handle leading zeros
@@ -602,7 +606,7 @@ def normalize_regional_phone_numbers(self, phone: str, region: str) -> str:
602606 # Remove country code from the local number
603607 local_number = digits [len (country_code ) :]
604608
605- # Handle leading zero in local numbers
609+ # Handle leading zero in local numbers after country code is removed
606610 if region not in ["US" , "CA" ] and local_number .startswith ("0" ):
607611 # Remove the leading zero
608612 local_number = local_number [1 :]
@@ -613,7 +617,9 @@ def normalize_regional_phone_numbers(self, phone: str, region: str) -> str:
613617 return ""
614618
615619 # Return formatted phone number with correct country code
616- return f"{ country_code } { local_number } "
620+ result = f"{ country_code } { local_number } "
621+ logging .info (f"Phone normalization result: { phone } -> { result } " )
622+ return result
617623
618624
619625class BrowserActions :
0 commit comments