Skip to content

Added phone number generator and normaliser script file #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ requests==2.24.0
urllib3==1.25.9
verify-email==2.4.1
yarl==1.4.2
phonenumbers~=8.12.6
4 changes: 2 additions & 2 deletions src/core/base/osint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"""
from typing import Any

from src.core.utils.validators import validate_kwargs
from src.core.base.base import BaseRunner
from src.core.utils.validators import validate_kwargs


class PossibleKeys:
"""
Defines default values for the function arguments (kwargs, named args)
"""

KEYS = ["email", "username", "fullname", "vk_api_key", "phone"]
KEYS = ["email", "username", "fullname", "vk_api_key", "phone", "region"]


class OsintRunner(BaseRunner):
Expand Down
5 changes: 5 additions & 0 deletions src/scripts/convert/phone_num_generator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys
from pathlib import Path

__root_dir = Path(__file__).parents[4]
sys.path.append(str(__root_dir))
13 changes: 13 additions & 0 deletions src/scripts/convert/phone_num_generator/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3

from pprint import pprint
from sys import argv

from .module import Runner

runner = Runner()
result = runner.run(
phone=argv[1] if len(argv) >= 2 else "+79131161111",
region=argv[2] if len(argv) >= 3 else "ru"
)
pprint(result)
75 changes: 75 additions & 0 deletions src/scripts/convert/phone_num_generator/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python3
from phonenumbers import NumberParseException, parse, format_number, PhoneNumberFormat

from src.core.base.osint import OsintRunner, PossibleKeys
from src.core.utils.response import ScriptResponse
from src.core.utils.validators import validate_kwargs


class Runner(OsintRunner):
def __init__(self, logger: str = __name__):
super(Runner, self).__init__(logger)

@staticmethod
def __gen_all(formatted_num: str) -> [str]:
"""
Generates all possible variants of number formats
:param formatted_num: formatted number
:return: the list of all formats
"""
number_groups = formatted_num.split(" ")
num_list = [formatted_num]
if len(number_groups) == 1:
return num_list
separators = ["", "-"]
# Automate some separation formats of numbers
for sep in separators:
joined = sep.join(number_groups)
num_list.append(joined)
phone_w_brackets = "{prefix}({code}){rest}".format(
prefix=number_groups[0],
code=number_groups[1][1:-1] if number_groups[1][0] == '(' and number_groups[1][-1] == ')' else
number_groups[1],
rest="".join(number_groups[2:])
)
num_list.append(phone_w_brackets)
return num_list

@validate_kwargs(PossibleKeys.KEYS)
def run(self, *args, **kwargs) -> ScriptResponse.success or ScriptResponse.error:
"""
Main runner function for the script
:param args: args from core runner
:param kwargs: kwargs from core runner
:return: ScriptResponse message
"""
try:
phone = kwargs.get("phone")
region = kwargs.get("region")
parsed_num = parse(phone, region)
except NumberParseException:
return ScriptResponse.error(result=None, message="Not viable number or not international format")
except Exception:
return ScriptResponse.error(result=None, message="Something went wrong!")
try:
result = [
phone for phone_format in [
PhoneNumberFormat.NATIONAL,
PhoneNumberFormat.INTERNATIONAL,
PhoneNumberFormat.E164
]
for phone in self.__gen_all(format_number(parsed_num, phone_format))
]
except Exception:
return ScriptResponse.error(result=None, message="Something went wrong!")

return ScriptResponse.success(
result=result,
message=f"All possible number formats for phone number {phone}",
)


if __name__ == "__main__":
script_module = Runner()
script_result = script_module.run()
print(script_result)
1 change: 1 addition & 0 deletions src/scripts/convert/phone_num_generator/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
phonenumbers==8.12.6
24 changes: 24 additions & 0 deletions src/scripts/convert/phone_num_generator/test_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest

from src.scripts.convert.phone_num_generator import module
from .module import Runner


class Test(unittest.TestCase):

def setUp(self):
self.runner = module.Runner()

def test_num_1(self):
runner = Runner()
response = runner.run(phone="+442083661177", region=None)
self.assertIn("020-8366-1177", response.get('result'))

def test_num_2(self):
runner = Runner()
response = runner.run(phone="8137777777", region="RU")
self.assertIn("+7-813-777-77-77", response.get('result'))


if __name__ == '_main_':
unittest.main()