Advertisement
Guest User

Untitled

a guest
Feb 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.02 KB | None | 0 0
  1. from Exception import *
  2. from utils.memory_util import (
  3. set_bot_celery_pid,
  4. set_bot_cookies
  5. )
  6. from InstagramCaptchaChallenge import InstagramCaptchaChallenge
  7. from utils.network_util import (
  8. is_proxy_ok,
  9. get_new_proxy
  10. )
  11. from random import randint
  12. from process.instagram_phone_verify_process import InstagramPhoneVerifyProcess
  13. from utils.memory_util import append_bot_list_by_key
  14. from utils.config import (
  15. SRV_KEY,
  16. MASTER_URL,
  17. ID
  18. )
  19. import time, traceback, sys, requests, json
  20.  
  21. def handle_exception(func):
  22. def wrapped(*args, **kwargs):
  23. insta_obj = args[0]
  24. try:
  25. return func(*args, **kwargs)
  26. except LoginRequiredException as e:
  27. set_bot_cookies(insta_obj.bot_id, None)
  28. if e.report:
  29. report_exceptions(e, insta_obj.bot_id)
  30. if e.shutdown:
  31. shutdown_process(insta_obj)
  32. except (ProxyConnectionException, ProxyRefusedException, ProxyUnavailableException) as e:
  33. proxy_data = None
  34. while not is_proxy_ok(proxy_data):
  35. sleep_time = randint(60*1, 60*3)
  36. insta_obj.print_log('Proxy Unreachable, sleeping for : %s ' % str(sleep_time))
  37. time.sleep(sleep_time)
  38. insta_obj.print_log('Getting New Proxy')
  39. proxy_data = get_new_proxy()
  40. print('proxy_data', proxy_data)
  41. insta_obj.print_log('New proxy succesfully gotten')
  42. insta_obj.proxy_ip = proxy_data['proxy_ip']
  43. insta_obj.proxy_port = proxy_data['proxy_port']
  44. insta_obj.proxy_username = proxy_data['proxy_username'] if 'proxy_username' in proxy_data else None
  45. insta_obj.proxy_password = proxy_data['proxy_password'] if 'proxy_password' in proxy_data else None
  46. insta_obj.api.setProxy(
  47. insta_obj.proxy_ip,
  48. insta_obj.proxy_port,
  49. insta_obj.proxy_username,
  50. insta_obj.proxy_password
  51. )
  52. new_args = list(args)
  53. new_args[0] = insta_obj
  54. new_args = tuple(new_args)
  55. return retry_failed_function(insta_obj, func, args, kwargs)
  56. except CheckPointException as e:
  57. challenge_data = json.loads(str(e))
  58. if challenge_data['challenge']['lock']:
  59. # Phone verify
  60. report_exceptions(e, insta_obj.bot_id)
  61. insta_obj.has_fail = True
  62. shutdown_process(insta_obj)
  63. else:
  64. # Captcha challenge
  65. try:
  66. time.sleep(randint(12,20))
  67. insta_obj.print_log(challenge_data)
  68. insta_obj.print_log('Start Captcha challenge')
  69. icc = InstagramCaptchaChallenge(insta_obj.bot_id)
  70. icc.execute()
  71. except Exception as err:
  72. # Failed captcha lock. Shutdown
  73. traceback.print_exc()
  74. insta_obj.print_log(err)
  75. report_unknown_exception(err, insta_obj.bot_id, code_id=19)
  76. insta_obj.has_fail = True
  77. shutdown_process(insta_obj)
  78. except SpamDetected as e:
  79. insta_obj.spam_count += 1
  80. insta_obj.print_log('Instagram API SPAM detected Exception : %s ' % e)
  81. insta_obj.print_log('Spam count for the bot : %s' % insta_obj.spam_count)
  82. if insta_obj.spam_count > insta_obj.MAX_SPAM_COUNT:
  83. insta_obj.print_log('Shutting down the process, this is the %s spam time.' % insta_obj.MAX_SPAM_COUNT )
  84. if e.report:
  85. report_exceptions(e, insta_obj.bot_id)
  86. insta_obj.has_fail = True
  87. shutdown_process(insta_obj)
  88. #Take a break
  89. wait_time = randint(60*5 * insta_obj.spam_count, 60*10 * insta_obj.spam_count)
  90. insta_obj.print_log('Take a break for : %s seconds' % wait_time)
  91. time.sleep(wait_time)
  92. return retry_failed_function(insta_obj, func, args, kwargs)
  93. except InstagramAPIException as e:
  94. if e.report:
  95. report_exceptions(e, insta_obj.bot_id)
  96. insta_obj.print_log('Instagram API Exception : %s' % e)
  97. if e.shutdown:
  98. insta_obj.has_fail = True
  99. shutdown_process(insta_obj)
  100. return retry_failed_function(insta_obj, func, args, kwargs)
  101. except ClientException as e:
  102. if e.report:
  103. report_exceptions(e, insta_obj.bot_id)
  104. insta_obj.print_log('Client Exception : %s' % e)
  105. if e.shutdown:
  106. insta_obj.has_fail = True
  107. shutdown_process(insta_obj)
  108. return retry_failed_function(insta_obj, func, args, kwargs)
  109. return wrapped
  110.  
  111. @handle_exception
  112. def retry_failed_function(insta_obj, func, args, kwargs):
  113. return func(*args, **kwargs)
  114.  
  115. def shutdown_process(instabot_obj):
  116. instabot_obj.has_fail = True
  117. set_bot_celery_pid(instabot_obj.bot_id, None)
  118. sys.exit(0)
  119.  
  120. def report_exceptions(exception, bot_id):
  121. print('Report Exception to master server')
  122. append_bot_list_by_key(
  123. bot_id=bot_id,
  124. key='exceptions',
  125. data={
  126. "code_id": exception.code_id,
  127. "activated": exception.activated,
  128. "has_shutdown_bot": exception.shutdown,
  129. "exception_message": str(exception),
  130. "failed_endpoint": exception.failed_endpoint,
  131. "request_data": exception.request_data
  132. }
  133. )
  134.  
  135. def report_unknown_exception(exception, bot_id, code_id=11):
  136. print('Report Unknown Exception to master server')
  137. append_bot_list_by_key(
  138. bot_id=bot_id,
  139. key='exceptions',
  140. data={
  141. "code_id": code_id,
  142. "activated": True,
  143. "has_shutdown_bot": True,
  144. "exception_message": str(exception),
  145. "failed_endpoint": None,
  146. "request_data": None
  147. }
  148. )
  149.  
  150. def throw_client_exception(func):
  151. def wrapped(*args, **kwargs):
  152. request = args[1]
  153. try:
  154. return func(*args, **kwargs)
  155. except InstagramServerInvalidJSONException:
  156. pass
  157. except InstagramAPIException as e:
  158. print(str(e))
  159. raise e
  160. except Exception as e:
  161. exception_string = str(e)
  162. if 'transfer closed with' in exception_string:
  163. raise InstagramServerLoadException(exception_string, request.getEndPoint(), request.getData())
  164. elif 'Connection refused' in exception_string:
  165. raise ProxyRefusedException(exception_string, request.getEndPoint(), request.getData())
  166. elif 'SSL read: error' in exception_string:
  167. raise SSLReadException(exception_string, request.getEndPoint(), request.getData())
  168. elif 'Unknown SSL protocol error' in exception_string:
  169. raise UnknownSSLException(exception_string, request.getEndPoint(), request.getData())
  170. elif 'Received HTTP code 403' in exception_string or 'Received HTTP code 407' in exception_string:
  171. raise ProxyConnectionException(exception_string, request.getEndPoint(), request.getData())
  172. elif 'Received HTTP code 504 from proxy' in exception_string or 'Proxy CONNECT aborted' in exception_string or 'Connection timed out' in exception_string or 'Operation timed out' in exception_string or 'SSL connection timeout' in exception_string:
  173. raise ProxyTimeoutException(exception_string, request.getEndPoint(), request.getData())
  174. elif 'Proxy CONNECT aborted' in exception_string:
  175. raise ProxyConnectAbortedException(exception_string, request.getEndPoint(), request.getData())
  176. elif 'Received HTTP code 503' in exception_string or 'reset by peer' in exception_string:
  177. raise ProxyUnavailableException(exception_string, request.getEndPoint(), request.getData())
  178. elif 'SSL certificate problem' in exception_string or 'unable to get local issuer certificate' in exception_string or 'SSL routines' in exception_string:
  179. raise SSLCertificateException(exception_string, request.getEndPoint(), request.getData())
  180. elif 'GnuTLS recv error' in exception_string or 'unexpected TLS handshake' in exception_string or 'Recv failure' in exception_string or 'gnutls_handshake' in exception_string:
  181. raise GNUTLSException(exception_string, request.getEndPoint(), request.getData())
  182. return wrapped
  183.  
  184. def throw_instagram_api_exception(data, data_str, request):
  185. if 'message' in data:
  186. message = data['message']
  187. if 'reset your password' in message:
  188. raise ResetPasswordException(data_str, request.getEndPoint(), request.getData())
  189. elif 'checkpoint_required' in message:
  190. raise CheckPointException(data_str, request.getEndPoint(), request.getData())
  191. elif 'violating our terms' in message:
  192. raise BannedException(data_str, request.getEndPoint(), request.getData())
  193. elif 'is incorrect. Please try again' in message:
  194. raise BadPasswordException(data_str, request.getEndPoint(), request.getData())
  195. elif 'few minutes before you try again' in message:
  196. raise DelayAbuseException(data_str, request.getEndPoint(), request.getData())
  197. elif 'INVALID_REQUEST' in message:
  198. raise InvalidRequestException(data_str, request.getEndPoint(), request.getData())
  199. elif 'challenge_required' in message:
  200. raise CheckPointException(data_str, request.getEndPoint(), request.getData())
  201. elif 'the max limit of accounts' in message:
  202. raise MaxFollowingLimit(data_str, request.getEndPoint(), request.getData())
  203. elif 'feedback_required' in message:
  204. raise SpamDetected(data_str, request.getEndPoint(), request.getData())
  205. elif 'you cannot like this media' in message:
  206. raise LikeMediaException(data_str, request.getEndPoint(), request.getData())
  207. elif 'Not authorized to view user' in message:
  208. raise UserViewProfileException(data_str, request.getEndPoint(), request.getData())
  209. elif 'Server Error' in message:
  210. raise ServerErrorException(data_str, request.getEndPoint(), request.getData())
  211. elif 'login_required' in message:
  212. raise LoginRequiredException(data_str, request.getEndPoint(), request.getData())
  213. elif 'Invalid target user' in message:
  214. raise InvalidTargetUserException(data_str, request.getEndPoint(), request.getData())
  215. elif 'media configure error' in message:
  216. raise MediaConfigureException(data_str, request.getEndPoint(), request.getData())
  217. else:
  218. raise UnidentifiedException(data_str, request.getEndPoint(), request.getData())
  219. else:
  220. raise UnidentifiedException(data_str, request.getEndPoint(), request.getData())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement