Advertisement
Dori_mon

Untitled

Jul 17th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.31 KB | None | 0 0
  1. import requests
  2. import json
  3. from numpy import double
  4.  
  5. _BASE_URL = 'https://smshub.org/stubs/handler_api.php'
  6.  
  7. class SmsActivateApi:
  8.  
  9.  
  10. def __init__(self, **kwargs):
  11.  
  12. self._api_key = kwargs.get('api_key')
  13. self._requests_session = kwargs.get('requests_session', requests.Session())
  14. self._request_headers = kwargs.get('request_headers', {})
  15. self._request_timeout = kwargs.get('request_timeout', 15000)
  16.  
  17.  
  18. @property
  19. def api_key(self):
  20. return self._api_key
  21.  
  22.  
  23. @property
  24. def requests_session(self):
  25. return self._requests_session
  26.  
  27.  
  28. def is_availability(self, country, service, forward=False):
  29.  
  30. key = service[:2] + '_' + ('1' if forward else '0')
  31. response = _send_action('getNumbersStatus', country=country)
  32.  
  33. availability = json.loads(response.text)
  34.  
  35. return availability.get(key, 0) > 0
  36.  
  37.  
  38. def get_balance(self):
  39.  
  40. response = _send_action('getBalance')
  41. response_text = response.text
  42. response_array = response_text.split(':')
  43.  
  44. if response_array[0] != 'ACCESS_BALANCE':
  45. raise SmsException(f'Couldn\'t access balance: {response_text}')
  46.  
  47. return float(response_array[1])
  48.  
  49.  
  50. def create_phone_number(self, country, service):
  51.  
  52. response = _send_action('getNumbersStatus', country=country, service=service)
  53. response_text = response.text
  54.  
  55. if response_text == 'NO_NUMBERS':
  56. raise NoAvailableNumbersException()
  57.  
  58. if response_text == 'NO_BALANCE':
  59. raise NotEnoughBalanceException()
  60.  
  61. if response_text == 'BAD_SERVICE':
  62. raise InvalidServiceException()
  63.  
  64. response_array = response.text.split(':')
  65.  
  66. if response_array[0] != 'ACCESS_NUMBER':
  67. raise SmsException(f'Couldn\'t order phone number: {response_text}')
  68.  
  69. return PhoneNumber(id=response_array[1], number=response_array[2], api=self)
  70.  
  71.  
  72. def _send_action(self, action, **kwargs):
  73.  
  74. url = _BASE_URL + '?action=' + action + '&api_key=' + self.api_key
  75.  
  76. for k,v in kwargs.items():
  77. url += '&' + k + '=' + v
  78.  
  79. response = self.requests_session.get(url, headers=self._request_headers, timeout=self._request_timeout, verify=False)
  80.  
  81. if response.status_code != 200:
  82. raise InvalidStatusException('You are getting an invalid status code, code: ' + response.status_code + '\nResponse:\n' + response.text)
  83.  
  84. response_text = response.text
  85.  
  86. if response_text == 'NO_KEY':
  87. raise NoKeyException()
  88.  
  89. if response_text == 'BAD_KEY':
  90. raise InvalidKeyException()
  91.  
  92. if response_text == 'ERROR_SQL':
  93. raise InternalDatabaseException()
  94.  
  95. if response_text.split(':')[0] == 'BANNED':
  96. raise BannedException()
  97.  
  98. if response_text == 'BAD_ACTION':
  99. raise InvalidAuctionException()
  100.  
  101. return response
  102.  
  103.  
  104. class PhoneNumber:
  105.  
  106.  
  107. def __init__(self, **kwargs):
  108.  
  109. self._id = kwargs.get('id')
  110. self._number = kwargs.get('number')
  111. self._api = kwargs.get('api')
  112.  
  113.  
  114. @property
  115. def id(self):
  116. return self._id
  117.  
  118.  
  119. @property
  120. def number(self):
  121. return self._number
  122.  
  123.  
  124. def set_sms_sent(self):
  125.  
  126. response = self._set_status(1)
  127. response_text = response.text
  128.  
  129. if response_text != 'ACCESS_READY':
  130. raise SmsException(f'Couldn\'t set status to sms sent: {response_text}')
  131.  
  132. return response
  133.  
  134.  
  135. def set_already_used(self):
  136.  
  137. response = self._set_status(8)
  138. response_text = response.text
  139.  
  140. if response_text != 'ACCESS_CANCEL':
  141. raise SmsException(f'Couldn\'t set status to already used: {response_text}')
  142.  
  143. return response
  144.  
  145.  
  146. def get_code(self):
  147.  
  148. response = self._send_action('getStatus')
  149. response_text = response.text
  150.  
  151. if response_text == 'STATUS_WAIT_CODE':
  152. raise WaitingForCodeException()
  153.  
  154. response_array = response_text.split(':')
  155.  
  156. if response_array[0] != 'STATUS_OK':
  157. raise SmsException(f'Couldn\'t get code: {response_text}')
  158.  
  159. return response_array[1]
  160.  
  161.  
  162. def set_completed(self):
  163.  
  164. response = self._set_status(6)
  165. response_text = response.text
  166.  
  167. if response_text != 'ACCESS_ACTIVATION':
  168. raise SmsException(f'Couldn\'t set status to completed: {response_text}')
  169.  
  170.  
  171. def _set_status(self, status):
  172.  
  173. response = self._send_action('setStatus', status=status)
  174. response_text = response.text
  175.  
  176. if response_text == 'BAD_STATUS':
  177. raise InvalidStatusException()
  178.  
  179. return response
  180.  
  181.  
  182. def _send_action(self, action, **kwargs):
  183. kwargs['id'] = self.id
  184. response = self._api._send_action(action, **kwargs)
  185. response_text = response.text
  186.  
  187. if response_text == 'NO_ACTIVATION' or response_text == 'WRONG_ACTIVATION_ID':
  188. raise InvalidIdException()
  189.  
  190. return response
  191.  
  192.  
  193. class SmsException(Exception):
  194.  
  195. def __init__(self, message):
  196. super(SmsException, self).__init__(message)
  197.  
  198.  
  199. class NoKeyException(SmsException):
  200.  
  201. def __init__(self, message='SMS API key is empty!'):
  202. super(NoKeyException, self).__init__(message)
  203.  
  204.  
  205. class InvalidKeyException(SmsException):
  206.  
  207. def __init__(self, message='SMS API key invalid!'):
  208. super(InvalidKeyException, self).__init__(message)
  209.  
  210.  
  211. class InternalDatabaseException(SmsException):
  212.  
  213. def __init__(self, message='SMS API has internal database error!'):
  214. super(InternalDatabaseException, self).__init__(message)
  215.  
  216.  
  217. class BannedException(SmsException):
  218.  
  219. def __init__(self, message='You are banned from using the sms activate service!'):
  220. super(BannedException, self).__init__(message)
  221.  
  222.  
  223. class InvalidAuctionException(SmsException):
  224.  
  225. def __init__(self, message='Invalid action!'):
  226. super(InvalidAuctionException, self).__init__(message)
  227.  
  228.  
  229. class NoAvailableNumbersException(SmsException):
  230.  
  231. def __init__(self, message='There are no available phone numbers!'):
  232. super(NoAvailableNumbersException, self).__init__(message)
  233.  
  234.  
  235. class NotEnoughBalanceException(SmsException):
  236.  
  237. def __init__(self, message='You don\'t have enough money!'):
  238. super(NotEnoughBalanceException, self).__init__(message)
  239.  
  240.  
  241. class InvalidServiceException(SmsException):
  242.  
  243. def __init__(self, message='Invalid service!'):
  244. super(InvalidServiceException, self).__init__(message)
  245.  
  246.  
  247. class InvalidStatusException(SmsException):
  248.  
  249. def __init__(self, message):
  250. super(InvalidServiceException, self).__init__(message)
  251.  
  252.  
  253. class InvalidIdException(SmsException):
  254.  
  255. def __init__(self, message='Provided id is invalid!'):
  256. super(InvalidIdException, self).__init__(message)
  257.  
  258.  
  259. class InvalidStatusException(SmsException):
  260.  
  261. def __init__(self, message='Provided status is invalid!'):
  262. super(InvalidStatusException, self).__init__(message)
  263.  
  264.  
  265. class WaitingForCodeException(SmsException):
  266.  
  267. def __init__(self, message='Waiting for sms to receive!'):
  268. super(WaitingForCodeException, self).__init__(message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement