Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Exception import *
- from utils.memory_util import (
- set_bot_celery_pid,
- set_bot_cookies
- )
- from InstagramCaptchaChallenge import InstagramCaptchaChallenge
- from utils.network_util import (
- is_proxy_ok,
- get_new_proxy
- )
- from random import randint
- from process.instagram_phone_verify_process import InstagramPhoneVerifyProcess
- from utils.memory_util import append_bot_list_by_key
- from utils.config import (
- SRV_KEY,
- MASTER_URL,
- ID
- )
- import time, traceback, sys, requests, json
- def handle_exception(func):
- def wrapped(*args, **kwargs):
- insta_obj = args[0]
- try:
- return func(*args, **kwargs)
- except LoginRequiredException as e:
- set_bot_cookies(insta_obj.bot_id, None)
- if e.report:
- report_exceptions(e, insta_obj.bot_id)
- if e.shutdown:
- shutdown_process(insta_obj)
- except (ProxyConnectionException, ProxyRefusedException, ProxyUnavailableException) as e:
- proxy_data = None
- while not is_proxy_ok(proxy_data):
- sleep_time = randint(60*1, 60*3)
- insta_obj.print_log('Proxy Unreachable, sleeping for : %s ' % str(sleep_time))
- time.sleep(sleep_time)
- insta_obj.print_log('Getting New Proxy')
- proxy_data = get_new_proxy()
- print('proxy_data', proxy_data)
- insta_obj.print_log('New proxy succesfully gotten')
- insta_obj.proxy_ip = proxy_data['proxy_ip']
- insta_obj.proxy_port = proxy_data['proxy_port']
- insta_obj.proxy_username = proxy_data['proxy_username'] if 'proxy_username' in proxy_data else None
- insta_obj.proxy_password = proxy_data['proxy_password'] if 'proxy_password' in proxy_data else None
- insta_obj.api.setProxy(
- insta_obj.proxy_ip,
- insta_obj.proxy_port,
- insta_obj.proxy_username,
- insta_obj.proxy_password
- )
- new_args = list(args)
- new_args[0] = insta_obj
- new_args = tuple(new_args)
- return retry_failed_function(insta_obj, func, args, kwargs)
- except CheckPointException as e:
- challenge_data = json.loads(str(e))
- if challenge_data['challenge']['lock']:
- # Phone verify
- report_exceptions(e, insta_obj.bot_id)
- insta_obj.has_fail = True
- shutdown_process(insta_obj)
- else:
- # Captcha challenge
- try:
- time.sleep(randint(12,20))
- insta_obj.print_log(challenge_data)
- insta_obj.print_log('Start Captcha challenge')
- icc = InstagramCaptchaChallenge(insta_obj.bot_id)
- icc.execute()
- except Exception as err:
- # Failed captcha lock. Shutdown
- traceback.print_exc()
- insta_obj.print_log(err)
- report_unknown_exception(err, insta_obj.bot_id, code_id=19)
- insta_obj.has_fail = True
- shutdown_process(insta_obj)
- except SpamDetected as e:
- insta_obj.spam_count += 1
- insta_obj.print_log('Instagram API SPAM detected Exception : %s ' % e)
- insta_obj.print_log('Spam count for the bot : %s' % insta_obj.spam_count)
- if insta_obj.spam_count > insta_obj.MAX_SPAM_COUNT:
- insta_obj.print_log('Shutting down the process, this is the %s spam time.' % insta_obj.MAX_SPAM_COUNT )
- if e.report:
- report_exceptions(e, insta_obj.bot_id)
- insta_obj.has_fail = True
- shutdown_process(insta_obj)
- #Take a break
- wait_time = randint(60*5 * insta_obj.spam_count, 60*10 * insta_obj.spam_count)
- insta_obj.print_log('Take a break for : %s seconds' % wait_time)
- time.sleep(wait_time)
- return retry_failed_function(insta_obj, func, args, kwargs)
- except InstagramAPIException as e:
- if e.report:
- report_exceptions(e, insta_obj.bot_id)
- insta_obj.print_log('Instagram API Exception : %s' % e)
- if e.shutdown:
- insta_obj.has_fail = True
- shutdown_process(insta_obj)
- return retry_failed_function(insta_obj, func, args, kwargs)
- except ClientException as e:
- if e.report:
- report_exceptions(e, insta_obj.bot_id)
- insta_obj.print_log('Client Exception : %s' % e)
- if e.shutdown:
- insta_obj.has_fail = True
- shutdown_process(insta_obj)
- return retry_failed_function(insta_obj, func, args, kwargs)
- return wrapped
- @handle_exception
- def retry_failed_function(insta_obj, func, args, kwargs):
- return func(*args, **kwargs)
- def shutdown_process(instabot_obj):
- instabot_obj.has_fail = True
- set_bot_celery_pid(instabot_obj.bot_id, None)
- sys.exit(0)
- def report_exceptions(exception, bot_id):
- print('Report Exception to master server')
- append_bot_list_by_key(
- bot_id=bot_id,
- key='exceptions',
- data={
- "code_id": exception.code_id,
- "activated": exception.activated,
- "has_shutdown_bot": exception.shutdown,
- "exception_message": str(exception),
- "failed_endpoint": exception.failed_endpoint,
- "request_data": exception.request_data
- }
- )
- def report_unknown_exception(exception, bot_id, code_id=11):
- print('Report Unknown Exception to master server')
- append_bot_list_by_key(
- bot_id=bot_id,
- key='exceptions',
- data={
- "code_id": code_id,
- "activated": True,
- "has_shutdown_bot": True,
- "exception_message": str(exception),
- "failed_endpoint": None,
- "request_data": None
- }
- )
- def throw_client_exception(func):
- def wrapped(*args, **kwargs):
- request = args[1]
- try:
- return func(*args, **kwargs)
- except InstagramServerInvalidJSONException:
- pass
- except InstagramAPIException as e:
- print(str(e))
- raise e
- except Exception as e:
- exception_string = str(e)
- if 'transfer closed with' in exception_string:
- raise InstagramServerLoadException(exception_string, request.getEndPoint(), request.getData())
- elif 'Connection refused' in exception_string:
- raise ProxyRefusedException(exception_string, request.getEndPoint(), request.getData())
- elif 'SSL read: error' in exception_string:
- raise SSLReadException(exception_string, request.getEndPoint(), request.getData())
- elif 'Unknown SSL protocol error' in exception_string:
- raise UnknownSSLException(exception_string, request.getEndPoint(), request.getData())
- elif 'Received HTTP code 403' in exception_string or 'Received HTTP code 407' in exception_string:
- raise ProxyConnectionException(exception_string, request.getEndPoint(), request.getData())
- 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:
- raise ProxyTimeoutException(exception_string, request.getEndPoint(), request.getData())
- elif 'Proxy CONNECT aborted' in exception_string:
- raise ProxyConnectAbortedException(exception_string, request.getEndPoint(), request.getData())
- elif 'Received HTTP code 503' in exception_string or 'reset by peer' in exception_string:
- raise ProxyUnavailableException(exception_string, request.getEndPoint(), request.getData())
- elif 'SSL certificate problem' in exception_string or 'unable to get local issuer certificate' in exception_string or 'SSL routines' in exception_string:
- raise SSLCertificateException(exception_string, request.getEndPoint(), request.getData())
- 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:
- raise GNUTLSException(exception_string, request.getEndPoint(), request.getData())
- return wrapped
- def throw_instagram_api_exception(data, data_str, request):
- if 'message' in data:
- message = data['message']
- if 'reset your password' in message:
- raise ResetPasswordException(data_str, request.getEndPoint(), request.getData())
- elif 'checkpoint_required' in message:
- raise CheckPointException(data_str, request.getEndPoint(), request.getData())
- elif 'violating our terms' in message:
- raise BannedException(data_str, request.getEndPoint(), request.getData())
- elif 'is incorrect. Please try again' in message:
- raise BadPasswordException(data_str, request.getEndPoint(), request.getData())
- elif 'few minutes before you try again' in message:
- raise DelayAbuseException(data_str, request.getEndPoint(), request.getData())
- elif 'INVALID_REQUEST' in message:
- raise InvalidRequestException(data_str, request.getEndPoint(), request.getData())
- elif 'challenge_required' in message:
- raise CheckPointException(data_str, request.getEndPoint(), request.getData())
- elif 'the max limit of accounts' in message:
- raise MaxFollowingLimit(data_str, request.getEndPoint(), request.getData())
- elif 'feedback_required' in message:
- raise SpamDetected(data_str, request.getEndPoint(), request.getData())
- elif 'you cannot like this media' in message:
- raise LikeMediaException(data_str, request.getEndPoint(), request.getData())
- elif 'Not authorized to view user' in message:
- raise UserViewProfileException(data_str, request.getEndPoint(), request.getData())
- elif 'Server Error' in message:
- raise ServerErrorException(data_str, request.getEndPoint(), request.getData())
- elif 'login_required' in message:
- raise LoginRequiredException(data_str, request.getEndPoint(), request.getData())
- elif 'Invalid target user' in message:
- raise InvalidTargetUserException(data_str, request.getEndPoint(), request.getData())
- elif 'media configure error' in message:
- raise MediaConfigureException(data_str, request.getEndPoint(), request.getData())
- else:
- raise UnidentifiedException(data_str, request.getEndPoint(), request.getData())
- else:
- raise UnidentifiedException(data_str, request.getEndPoint(), request.getData())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement