Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- from numpy import double
- _BASE_URL = 'https://smshub.org/stubs/handler_api.php'
- class SmsActivateApi:
- def __init__(self, **kwargs):
- self._api_key = kwargs.get('api_key')
- self._requests_session = kwargs.get('requests_session', requests.Session())
- self._request_headers = kwargs.get('request_headers', {})
- self._request_timeout = kwargs.get('request_timeout', 15000)
- @property
- def api_key(self):
- return self._api_key
- @property
- def requests_session(self):
- return self._requests_session
- def is_availability(self, country, service, forward=False):
- key = service[:2] + '_' + ('1' if forward else '0')
- response = _send_action('getNumbersStatus', country=country)
- availability = json.loads(response.text)
- return availability.get(key, 0) > 0
- def get_balance(self):
- response = _send_action('getBalance')
- response_text = response.text
- response_array = response_text.split(':')
- if response_array[0] != 'ACCESS_BALANCE':
- raise SmsException(f'Couldn\'t access balance: {response_text}')
- return float(response_array[1])
- def create_phone_number(self, country, service):
- response = _send_action('getNumbersStatus', country=country, service=service)
- response_text = response.text
- if response_text == 'NO_NUMBERS':
- raise NoAvailableNumbersException()
- if response_text == 'NO_BALANCE':
- raise NotEnoughBalanceException()
- if response_text == 'BAD_SERVICE':
- raise InvalidServiceException()
- response_array = response.text.split(':')
- if response_array[0] != 'ACCESS_NUMBER':
- raise SmsException(f'Couldn\'t order phone number: {response_text}')
- return PhoneNumber(id=response_array[1], number=response_array[2], api=self)
- def _send_action(self, action, **kwargs):
- url = _BASE_URL + '?action=' + action + '&api_key=' + self.api_key
- for k,v in kwargs.items():
- url += '&' + k + '=' + v
- response = self.requests_session.get(url, headers=self._request_headers, timeout=self._request_timeout, verify=False)
- if response.status_code != 200:
- raise InvalidStatusException('You are getting an invalid status code, code: ' + response.status_code + '\nResponse:\n' + response.text)
- response_text = response.text
- if response_text == 'NO_KEY':
- raise NoKeyException()
- if response_text == 'BAD_KEY':
- raise InvalidKeyException()
- if response_text == 'ERROR_SQL':
- raise InternalDatabaseException()
- if response_text.split(':')[0] == 'BANNED':
- raise BannedException()
- if response_text == 'BAD_ACTION':
- raise InvalidAuctionException()
- return response
- class PhoneNumber:
- def __init__(self, **kwargs):
- self._id = kwargs.get('id')
- self._number = kwargs.get('number')
- self._api = kwargs.get('api')
- @property
- def id(self):
- return self._id
- @property
- def number(self):
- return self._number
- def set_sms_sent(self):
- response = self._set_status(1)
- response_text = response.text
- if response_text != 'ACCESS_READY':
- raise SmsException(f'Couldn\'t set status to sms sent: {response_text}')
- return response
- def set_already_used(self):
- response = self._set_status(8)
- response_text = response.text
- if response_text != 'ACCESS_CANCEL':
- raise SmsException(f'Couldn\'t set status to already used: {response_text}')
- return response
- def get_code(self):
- response = self._send_action('getStatus')
- response_text = response.text
- if response_text == 'STATUS_WAIT_CODE':
- raise WaitingForCodeException()
- response_array = response_text.split(':')
- if response_array[0] != 'STATUS_OK':
- raise SmsException(f'Couldn\'t get code: {response_text}')
- return response_array[1]
- def set_completed(self):
- response = self._set_status(6)
- response_text = response.text
- if response_text != 'ACCESS_ACTIVATION':
- raise SmsException(f'Couldn\'t set status to completed: {response_text}')
- def _set_status(self, status):
- response = self._send_action('setStatus', status=status)
- response_text = response.text
- if response_text == 'BAD_STATUS':
- raise InvalidStatusException()
- return response
- def _send_action(self, action, **kwargs):
- kwargs['id'] = self.id
- response = self._api._send_action(action, **kwargs)
- response_text = response.text
- if response_text == 'NO_ACTIVATION' or response_text == 'WRONG_ACTIVATION_ID':
- raise InvalidIdException()
- return response
- class SmsException(Exception):
- def __init__(self, message):
- super(SmsException, self).__init__(message)
- class NoKeyException(SmsException):
- def __init__(self, message='SMS API key is empty!'):
- super(NoKeyException, self).__init__(message)
- class InvalidKeyException(SmsException):
- def __init__(self, message='SMS API key invalid!'):
- super(InvalidKeyException, self).__init__(message)
- class InternalDatabaseException(SmsException):
- def __init__(self, message='SMS API has internal database error!'):
- super(InternalDatabaseException, self).__init__(message)
- class BannedException(SmsException):
- def __init__(self, message='You are banned from using the sms activate service!'):
- super(BannedException, self).__init__(message)
- class InvalidAuctionException(SmsException):
- def __init__(self, message='Invalid action!'):
- super(InvalidAuctionException, self).__init__(message)
- class NoAvailableNumbersException(SmsException):
- def __init__(self, message='There are no available phone numbers!'):
- super(NoAvailableNumbersException, self).__init__(message)
- class NotEnoughBalanceException(SmsException):
- def __init__(self, message='You don\'t have enough money!'):
- super(NotEnoughBalanceException, self).__init__(message)
- class InvalidServiceException(SmsException):
- def __init__(self, message='Invalid service!'):
- super(InvalidServiceException, self).__init__(message)
- class InvalidStatusException(SmsException):
- def __init__(self, message):
- super(InvalidServiceException, self).__init__(message)
- class InvalidIdException(SmsException):
- def __init__(self, message='Provided id is invalid!'):
- super(InvalidIdException, self).__init__(message)
- class InvalidStatusException(SmsException):
- def __init__(self, message='Provided status is invalid!'):
- super(InvalidStatusException, self).__init__(message)
- class WaitingForCodeException(SmsException):
- def __init__(self, message='Waiting for sms to receive!'):
- super(WaitingForCodeException, self).__init__(message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement