From 8810d6525633d58212bd385f3f902f73f3a014e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Puerta=20Mart=C3=ADn?= Date: Fri, 3 May 2024 21:11:43 -0400 Subject: [PATCH] Refactor sms_services module to centralize SMS service instance retrieval --- ninjemail/email_providers/gmail.py | 17 ++----------- ninjemail/email_providers/yahoo.py | 17 ++----------- ninjemail/sms_services/__init__.py | 38 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/ninjemail/email_providers/gmail.py b/ninjemail/email_providers/gmail.py index dae3741..a96294c 100644 --- a/ninjemail/email_providers/gmail.py +++ b/ninjemail/email_providers/gmail.py @@ -6,7 +6,7 @@ from selenium.webdriver.support.ui import Select from selenium.webdriver.common.keys import Keys from selenium.webdriver.remote.webelement import WebElement -from sms_services import getsmscode, smspool, fivesim +from sms_services import get_sms_instance URL = 'https://accounts.google.com/signup' WAIT = 5 @@ -56,20 +56,7 @@ def create_account(driver, """ SMS_SERVICE = sms_key['name'] - - if SMS_SERVICE == 'getsmscode': - data = sms_key['data'] - data.update({'project': 1, - 'country': 'hk'}) - sms_provider = getsmscode.GetsmsCode(**data) - elif SMS_SERVICE == 'smspool': - data = sms_key['data'] - data.update({'service': 395}) - sms_provider = smspool.SMSPool(**data) - elif SMS_SERVICE == '5sim': - data = sms_key['data'] - data.update({'service': 'google'}) - sms_provider = fivesim.FiveSim(**data) + sms_provider = get_sms_instance(sms_key, 'google') logging.info('Creating Gmail account') diff --git a/ninjemail/email_providers/yahoo.py b/ninjemail/email_providers/yahoo.py index e421e2b..4611138 100644 --- a/ninjemail/email_providers/yahoo.py +++ b/ninjemail/email_providers/yahoo.py @@ -5,7 +5,7 @@ from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.ui import Select from selenium.webdriver.common.keys import Keys -from sms_services import getsmscode, smspool, fivesim +from sms_services import get_sms_instance URL = 'https://login.yahoo.com/account/create' WAIT = 25 @@ -40,20 +40,7 @@ def create_account(driver, """ SMS_SERVICE = sms_key['name'] - - if SMS_SERVICE == 'getsmscode': - data = sms_key['data'] - data.update({'project': 15, - 'country': 'us'}) - sms_provider = getsmscode.GetsmsCode(**data) - elif SMS_SERVICE == 'smspool': - data = sms_key['data'] - data.update({'service': 1034}) - sms_provider = smspool.SMSPool(**data) - elif SMS_SERVICE == '5sim': - data = sms_key['data'] - data.update({'service': 'yahoo'}) - sms_provider = fivesim.FiveSim(**data) + sms_provider = get_sms_instance(sms_key, 'yahoo') logging.info('Creating Yahoo account') diff --git a/ninjemail/sms_services/__init__.py b/ninjemail/sms_services/__init__.py index e69de29..615e08a 100644 --- a/ninjemail/sms_services/__init__.py +++ b/ninjemail/sms_services/__init__.py @@ -0,0 +1,38 @@ +from sms_services import getsmscode +from sms_services import smspool +from sms_services import fivesim + +def get_sms_instance(sms_info, email_provider): + """ + Retrieves an instance of an SMS provider based on the given SMS information. + + Args: + sms_info (dict): A dictionary containing the SMS service name and associated data. + + Returns: + object: An instance of the SMS provider based on the provided SMS service. + """ + service_name = sms_info['name'] + + if service_name == 'getsmscode': + data = sms_info['data'] + project = 1 + if email_provider == 'yahoo': + project = 15 + data.update({'project': project, 'country': 'us'}) + sms_provider = getsmscode.GetsmsCode(**data) + elif service_name == 'smspool': + data = sms_info['data'] + service = 395 + if email_provider == 'yahoo': + service = 1034 + data.update({'service': service}) + sms_provider = smspool.SMSPool(**data) + elif service_name == '5sim': + data = sms_info['data'] + data.update({'service': email_provider}) + sms_provider = fivesim.FiveSim(**data) + + print(data) + + return sms_provider