def task_report(self, task_id: int, data_result: dict) -> bool: """ Report about task results :param data_result: dict :param task_id: int :return: dict """ if data_result["status"]: uri = self.url_task_done.replace("id", str(self.individual_bot_id)).replace("ib", str(task_id)) else: uri = self.url_task_fail.replace("id", str(self.individual_bot_id)).replace("ib", str(task_id)) url = self.api_url + uri try: response = requests.post(url) except requests.exceptions.ConnectionError as error: logger.warning(f"{error}") return False if response.status_code == 200: data = json.loads(response.text) if data["status"]: return True return False
def make_request(self, main_url: str, uri: str, params: dict, authorization_data: dict) -> dict: """ :param authorization_data: dict :param main_url: str :param uri: str :param params: dict :return: dict """ try: headers = { 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/88.0.4324.150 Safari/537.36', } response = requests.post(main_url + uri, data=params, headers=headers) except requests.exceptions.ConnectionError as error: logger.warning(f"{error}") return {"status": False, "error": True, "error_type": error} if response.status_code == 200: data = json.loads(response.text) if data["status"]: return {"status": data["status"]} logger.warning(f"Error response code - {response.status_code}") return { "status": False, "error": True, "error_type": response.status_code }
def interceptor(request): try: headers["x-ig-app-id"] = request.headers["X-IG-App-ID"] new = request.headers["Cookie"].split(';') for new_one in new: cooka = new_one.split('=') cookies[cooka[0].strip()] = cooka[1].strip() except AttributeError as errors_inter: logger.warning( f"The authorization process was not correct.!!! Error - {errors_inter}" ) sys.stdout.write(f"Error {errors_inter}")
def login(account_data: dict, initialization_headers: object, initialization_cookies: object, requests_map: dict): headers = dict() cookies = {} def interceptor(request): try: headers["x-ig-app-id"] = request.headers["X-IG-App-ID"] new = request.headers["Cookie"].split(';') for new_one in new: cooka = new_one.split('=') cookies[cooka[0].strip()] = cooka[1].strip() except AttributeError as errors_inter: logger.warning( f"The authorization process was not correct.!!! Error - {errors_inter}" ) sys.stdout.write(f"Error {errors_inter}") try: chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--window-size=10x10") driver = webdriver.Chrome(executable_path=config.PATH_TO_CHROMEDRIVER, options=chrome_options) driver.get(requests_map["main_url"]) time.sleep(5) username_field = driver.find_element_by_name( requests_map["login"]["username_field"]) password_field = driver.find_element_by_name( requests_map["login"]["password_field"]) submit_elem = driver.find_element_by_tag_name('button') username_field.send_keys(account_data["username"]) password_field.send_keys(account_data["password"]) submit_elem.click() driver.request_interceptor = interceptor request = driver.wait_for_request(requests_map["login"]["uri"]) headers["x-ig-www-claim"] = request.response.headers[ "x-ig-set-www-claim"] driver.close() set_cookies(initialization_cookies, cookies) if set_headers(initialization_headers, headers, cookies): return {"status": True} return {"status": False} except Exception as errors: logger.warning( f"The authorization process was not correct.!!! Error - {errors}") return {"status": False}
def _send_task_report(self, key: str, data: dict = None): mes = self.map_messages.get(key, False) if mes: message = mes["message"] if data: for key in data: if type(data[key]) is dict: strings = json.dumps(str(data[key])) message = message.replace(key, strings) continue message = message.replace(key, str(data[key])) continue if self.is_file_write: logger.warning(message) if self.is_console: sys.stdout.write(message)
def get_new_task(self) -> dict: """ Get params for new task :return: dict """ uri = self.url_next_task.replace("id", str(self.individual_bot_id)) url = self.api_url + uri try: response = requests.get(url) except requests.exceptions.ConnectionError as error: logger.warning(f"{error}") return {"status": False, "error": True, "error_type": error} if response.status_code == 200: data = json.loads(response.text) if data["status"]: return {"status": data["status"], "error": False, "task_name": data['task_name'], "task_id": data['task_id']} return {"status": False, "error": False}
def set_headers(initialization_headers: object, headers: dict, cookies: dict): try: initialization_headers.set_attribute_headers("x-csrftoken", cookies["csrftoken"]) initialization_headers.set_attribute_headers("x-ig-www-claim", headers["x-ig-www-claim"]) initialization_headers.set_attribute_headers("x-ig-app-id", headers["x-ig-app-id"]) cookies_string = '' for cookie in cookies.items(): cookies_string += cookie[0] + "=" + cookie[1] + "; " initialization_headers.set_attribute_headers("Cookie", cookies_string.rstrip()) return True except KeyError as error: logger.warning( f"Some parameters from response (/login/ajax/) of instagram was not correct.!!! Error - {error}" ) return False
def run(self, task_id: int, initialization_parameters: object, initialization_headers: object, initialization_cookies: object) -> dict: """ Run task :param initialization_parameters: object :param initialization_cookies: object :param initialization_headers: object :param task_id: int :return: dict """ sys.stdout.write("Task PageHashTask is running!\n") data_result = self.social_api.page_hash_task(initialization_parameters, initialization_headers, initialization_cookies) if data_result["status"]: if data_result["data"]["status"] == 'ok': try: initialization_parameters.fetch_media_item_cursor = \ data_result["data"]["data"]["user"]["edge_web_feed_timeline"]["page_info"]["end_cursor"] initialization_parameters.has_next_page = \ data_result["data"]["data"]["user"]["edge_web_feed_timeline"]["page_info"]["has_next_page"] # set posts id list initialization_parameters.posts_id_list.clear() for post_id in data_result["data"]["data"]["user"]["edge_web_feed_timeline"]["edges"]: try: initialization_parameters.posts_id_list.append(str(post_id["node"]["id"])) except KeyError: continue sys.stdout.write(f"Task PageHashTask completed work successfully!\n") except KeyError as error: sys.stdout.write( f"The PageHashTask for the bot number {self.individual_id} was not correct.!!!" f" Check the log file loging_fbi.log!\n") error_text = f"Some parameters from response of instagram (graphql/query/) was not correct.!!! Error - {error}" logger.warning(error_text) data_result["status"] = False else: sys.stdout.write( f"The FlippingTapeTask for the bot number {self.individual_id} was not correct.!!!" f" Check the log file loging_fbi.log!\n") error_text = f"Some parameters from response of instagram (graphql/query/) was not correct.!!! Data - {data_result['data']}" logger.warning(error_text) data_result["status"] = False else: sys.stdout.write( f"The PageHashTask for the bot number {self.individual_id} was not correct.!!!" f" Check the log file loging_fbi.log!\n") error_text = f"Some parameters from response of instagram (graphql/query/) was not correct.!!! Data - {data_result['data']}" logger.warning(error_text) # send report to api sys_report = SystemApiRequests(self.individual_id) sys_report.task_report(task_id, data_result) return data_result
def make_request_authorization(self, main_url: str, uri: str, params: dict) -> dict: """ :param main_url: str :param uri: str :param params: dict :return: dict """ try: headers = { 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/88.0.4324.150 Safari/537.36' } response = requests.post(main_url + uri, params=params, headers=headers) except requests.exceptions.ConnectionError as error: logger.warning(f"Authorization instagram error - {error}") return {"status": False, "error": True, "error_type": error} except KeyError as error: logger.warning(f"Authorization instagram error - {error}") return {"status": False, "error": True, "error_type": error} if response.status_code == 200: authorization_data = dict() data = json.loads(response.text) print(data) headers = response.headers if headers['csrftoken']: authorization_data['id_did'] = headers['id_did'] authorization_data['csrftoken'] = headers['csrftoken'] authorization_data['mid'] = headers['mid'] authorization_data['ig_nrcb'] = headers['ig_nrcb'] return { "status": True, "error": False, 'authorization_data': authorization_data } logger.warning(f"Authorization instagram error - {response.text}") logger.warning(f"Error response code - {response.status_code}") return { "status": False, "error": True, "error_type": response.status_code }
def start(self): logger.warning(f"Bot {self.individual_id} start working!!!") # log in to the social network if self.login_task: data_authorization = self._perform_task(self.task_objects['login'], 0) if not data_authorization['status']: sys.stdout.write( f"The authorization process for the bot" f" number {self.individual_id} was not correct.!!!") logger.warning( f"The authorization process for the bot number {self.individual_id} was not correct.!!!" ) return self.authorization_data = data_authorization["authorization_data"] while self.execution_status: new_task = self._get_new_task() if new_task["status"]: # run new task sys.stdout.write(f"Task {new_task['task_name']} is running!\n") self._perform_task(self.task_objects[new_task["task_name"]], new_task['task_id']) continue elif new_task["error"]: sys.stdout.write("Server error!!!\n") time.sleep(10) continue else: sys.stdout.write("No tasks, I work autonomously!\n") self._perform_task(self.task_objects["flipping_tape"], 3) time.sleep(10) continue
def _make_request_get(self, main_url: str, uri: str, params: dict, headers: dict, cookies: dict) -> dict: """ :param headers: :param main_url: str :param uri: str :param params: dict :return: dict """ data = dict() try: response = self.request.get(main_url + uri, params=params, headers=headers) try: data = response.json() except JSONDecodeError as error: logger.warning(f"Error decode json - {error}, {response}") return { "status": False, "error": True, "error_type": error, "error_message": data } except requests.exceptions.ConnectionError as error: print(error) logger.warning(f"{error}") return {"status": False, "error": True, "error_type": error} try: response.raise_for_status() except requests.HTTPError as error: logger.warning(f"Error request {response.status_code}, {data}!") return { "status": False, "error": True, "error_type": response.status_code } if response.status_code == 200: data = json.loads(response.text) if data["status"] == 'ok': return {"status": True, "data": data} return { "status": False, "error": True, "error_type": response.status_code }
def make_request(self, main_url: str, uri: str, params: dict, authorization_data: dict) -> dict: """ :param authorization_data: dict :param main_url: str :param uri: str :param params: dict :return: dict """ try: headers = { 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/88.0.4324.150 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'x-ig-app-id': '1217981644879628', 'x-csrftoken': authorization_data['csrftoken'] } response = requests.post(main_url + uri, data=params, headers=headers) print(response.headers) except requests.exceptions.ConnectionError as error: logger.warning(f"{error}") return {"status": False, "error": True, "error_type": error} except KeyError as error: logger.warning(f"{error}") return {"status": False, "error": True, "error_type": error} if response.status_code == 200: data = json.loads(response.text) print("login", data) return { "status": True, "response_data": json.loads(response.text), 'authorization_data': authorization_data } logger.warning(f"Error response code - {response.status_code}") return { "status": False, "error": True, "error_type": response.status_code }
def _send_data_api(self, data): logger.warning(f"Bot {data} start working!!!")