def update_name(self, new_name): ReportHandler.add_log("Updating", "Updating user name with {0}".format(self.mail)) url = config.url + api_requests.update_user body_update = {'token': self.token, 'name': new_name} update_name = requests.post(url, body_update, headers=config.headers) ReportHandler.report_it("Updating name", update_name.json())
def get_filters(self): try: with self.__connection: self.__cursor.execute("SELECT * FROM filters") to_parse = self.__cursor.fetchall() return to_parse except: ReportHandler.add_error("Getting filters", "Failed")
def set_language(self, new_language): ReportHandler.add_log( "Setting new language", "Setting new language for user {0}".format(self.mail)) url = config.url + api_requests.set_new_language body_update = {'token': self.token, 'language': new_language} update_language = requests.put(url, body_update) ReportHandler.report_it("Updating language", update_language.json())
def watch_ad(self, ad_id): # trying_to_watch_ad ReportHandler.add_log("Watching add", "Trying to watch ad with id {0}".format(ad_id)) url = config.url + api_requests.watch_ad param = {'token': self.token} view_ad = requests.get(url + str(ad_id), headers=config.headers, params=param) ReportHandler.report_it("Watching add", view_ad.json())
def get_min_and_max_ad_id(self): try: with self.__connection: self.__cursor.execute(api_requests.query_get_ads_min_and_max) result = self.__cursor.fetchone() config.min_ad_id = result[0] config.max_ad_id = result[1] print(config.max_ad_id) except: ReportHandler.add_error("Getting min and max ad ids", "Failed")
def authorize(self): ReportHandler.add_log("Authorization", "Authorizing with user {0}".format(self.mail)) url = config.url + api_requests.authorize body_login = {'email': self.mail, 'password': self.password} user_login = requests.post(url, body_login, headers=config.headers_desk) data = user_login.json() return self.token_builder(data, "Authorization")
def token_builder(self, data=None, function=None): ReportHandler.response_token(function, data) if data.get('data'): try: self.token = data['token'] except KeyError: data = data.get('data') data = data[0] self.token = data.get('token') else: return False
def watch_random_ad(self): ad_id = random.randint(config.min_ad_id, config.max_ad_id) ReportHandler.add_log( "Watching random add", "Trying to watch random add with id {0}".format(ad_id)) url = config.url + api_requests.watch_ad param = {'token': self.token} view_ad = requests.get(url + str(ad_id), headers=config.headers_desk, params=param) ReportHandler.report_it("Watching random add", view_ad.json())
def register(self): ReportHandler.add_log("Registration", "Registering with user {0}".format(self.mail)) url = config.url + api_requests.register body_register = { 'name': self.name + datetime.datetime.now().strftime("%H:%M:%S.%f"), 'email': self.mail, 'password': self.password } user_register = requests.post(url, body_register, headers=config.headers_desk) return self.token_builder(user_register.json(), "Registration")
def register(self): ReportHandler.add_log("Registration", "Registering with user {0}".format(self.mail)) url = config.url + api_requests.register body_register = { 'name': self.name, 'email': self.mail, 'password': self.password } user_register = requests.post(url, body_register, headers=config.headers_desk) data = user_register.json() return self.token_builder(data, "Registration")
def user_upload_image(self, path=None): ReportHandler.add_log("Upload image", "Trying to upload random image") url = config.url + api_requests.upload_image if path is None: photo_path = Rule.ad_get_new_random_image()[0] else: photo_path = path body_upload = {'token': self.token} upload_image = requests.post(url, body_upload, headers=config.headers_desk, files={'image': open(photo_path, 'rb')}) data = upload_image.json() img_id = data.get('data') ReportHandler.report_it("Upload image", data) return upload_image.json()
def create_ad(self): ReportHandler.add_log("Creating add", "Trying to create add with your settings") url = config.url + api_requests.create_ad body_add = { 'category_id': config.ad_category_id, 'location_id': config.ad_location_id, 'name': config.ad_name, 'description': config.ad_description, 'cost': config.ad_cost, 'token': self.token } user_register = requests.post( url, body_add, headers=config.headers_desk, files={'photos[]': open(config.ad_photos[0], 'rb')})
def create_ad(self, ad_category_id=None, ad_location_id=None, ad_name=None, ad_description=None, ad_cost=None, ad_photos=None, ad_filters=None): if ad_category_id is None: ad_category_id = config.ad_category_id if ad_location_id is None: ad_location_id = config.ad_location_id if ad_name is None: ad_name = config.ad_name if ad_description is None: ad_description = config.ad_description if ad_cost is None: ad_cost = config.ad_cost if ad_filters is None: ad_filters = config.ad_filters if type(ad_photos) == int: ad_photos = ad_photos elif ad_photos is None: ad_photos = self.parse_photo() ReportHandler.add_log("Creating add", "Trying to create add with your settings") url = config.url + api_requests.advert_create body_add = { "category_id": ad_category_id, "location_id": ad_location_id, "name": ad_name, "description": ad_description, "ad_filters[]": ad_filters, "cost": ad_cost, "token": self.token, "image_ids[]": ad_photos } creating_add = requests.post(url, body_add, headers=config.headers_desk) ReportHandler.report_it("Creating add", creating_add.json()) try: return creating_add.json().get("data")[0].get("id") except IndexError: return False
def start_approve_ads(self): ReportHandler.add_log("Approving adds", "Starting approver") try: self.browser.maximize_window() self.browser.get(self.desktop_url) self.browser.get(self.link) login_field = self.browser.find_element_by_css_selector( '[id="loginform-email"]') password_field = self.browser.find_element_by_css_selector( '[id="loginform-password"]') login_button = self.browser.find_element_by_css_selector( 'button[class="btn btn-success btn-block"]') login_field.send_keys("*****@*****.**") password_field.send_keys("admin") login_button.click() time.sleep(2) return True finally: return False
def approve_ads(self): for ad_id in config.queue: try: link = config.admin_url + "items/index/edit/{ad_id}".format( ad_id=ad_id) self.browser.get(link) time.sleep(0.5) confirm_button = self.browser.find_element_by_xpath( "//button[text()='Confirmed ']") save_button = self.browser.find_element_by_css_selector( 'input[type="submit"]') confirm_button.click() save_button.click() ReportHandler.add_log("Approving ads", "Ad with id {} approved".format(ad_id)) config.queue.remove(ad_id) except: print("Can't do smth for ad {}".format(ad_id)) config.queue.remove(ad_id) ReportHandler.add_error("Approving add", "Failed for id {}".format(ad_id))
def watch_ad(self, ad_id): # trying_to_watch_ad ReportHandler.add_log("Watching add", "Trying to watch ad with id {0}".format(ad_id)) url = config.url + api_requests.watch_ad param = {'token': self.token} view_ad = requests.get(url + str(ad_id), headers=config.headers_desk, params=param) if '"success": true' in view_ad: ReportHandler.add_log("Watch add", "successfull") else: ReportHandler.add_log("Watch add", "failed") ReportHandler.add_error("Watch add", str(view_ad.json()))
def init_app(url): clear_previous_logs() url_builder(url) config.headers = config.headers_mobile if config.users_count > 1: config.email = config.email[:config.email.find( '@')] + "{}" + config.email[config.email.find('@'):] ReportHandler.add_log("App initialization", "Trying to configure app") if set_add_example(): ReportHandler.add_log("App initialization", "Configured") else: ReportHandler.add_log( "App initialization", "Failed to load json configs, will work with default settings") ReportHandler.add_error( "App initialization", "Failed to load json configs, will work with default settings") ReportHandler.add_log("Making url", "Configured url with {0}".format(config.url)) # TODO: обработать вход минимум и максимум айди объяв min_ad = 0 max_ad = 60
def watch_random_ad(self): ad_id = random.randint(config.min_ad_id, config.max_ad_id) ReportHandler.add_log( "Watching random add", "Trying to watch random add with id {0}".format(ad_id)) url = config.url + api_requests.watch_ad param = {'token': self.token} view_ad = requests.get(url + str(ad_id), headers=config.headers_desk, params=param) if view_ad.json().get('success'): ReportHandler.add_log("Watch add", "successful") else: ReportHandler.add_log("Watch add", "failed") ReportHandler.add_error("Watch add", str(view_ad))
def generate_ads(self): i = 0 email = config.email ReportHandler.parse_log("Generating ads", "Starting generation...") while i < self.users_count: if self.users_count != 1: email_number = 100 * int(self.thread_number) + i + 1 email = config.email.format(str(email_number)) user = User(email, config.password) user.register() user.authorize() i += 1 curr_ads = 0 while curr_ads < self.ads_count: self.generate_ad_example() temp_id = user.create_ad() try: if temp_id: config.queue.append(temp_id) except TypeError: ReportHandler.add_error("Appending ad id", "Failed") finally: curr_ads += 1
def approve_adds_with_status_2(self): ReportHandler.add_log("Approving ads with DB", "Starting") try: with self.__connection: self.__cursor.execute(api_requests.query_set_status_confirmed) ReportHandler.add_log("Approving ads with DB", "Finished successfully") except: ReportHandler.add_error("Approving ads with DB", "Failed")
def create_ad_old(self, ad_category_id=None, ad_location_id=None, ad_name=None, ad_description=None, ad_cost=None, ad_photos=None): if ad_category_id is None: ad_category_id = config.ad_category_id if ad_location_id is None: ad_location_id = config.ad_location_id if ad_name is None: ad_name = config.ad_name if ad_description is None: ad_description = config.ad_description if ad_cost is None: ad_cost = config.ad_cost if ad_photos is None: ad_photos = config.ad_photos ReportHandler.add_log("Creating add", "Trying to create add with your settings") url = config.url + api_requests.create_ad body_add = { 'category_id': ad_category_id, 'location_id': ad_location_id, 'name': ad_name, 'description': ad_description, 'cost': ad_cost, 'token': self.token } creating_add = requests.post( url, body_add, headers=config.headers_desk, files={'photos[]': open(ad_photos[0], 'rb')}) ReportHandler.report_it("Creating add", creating_add.json())
def __init__(self): ReportHandler.add_log("Connect to DB", "Starting") self.login = "******" self.password = "******" self.url = config.db_url self.host = "192.168.2.3" self.__cursor = "" try: self.__connection = psycopg2.connect(dbname=self.url, user=self.login, password=self.password, host=self.host) ReportHandler.add_log("Connect to DB", "Connection established") self.__cursor = self.__connection.cursor() except psycopg2.OperationalError: ReportHandler.add_error("Wrong DB setted up", "Failed") except: ReportHandler.add_error("Connect to DB", "Failed")
def user_upload_image(self): ReportHandler.add_log("Upload image", "Trying to upload random image") url = config.url + api_requests.upload_image with open("venv/Uploads/images.json", "r") as read_file: photos = json.load(read_file) photo_path = "venv/Uploads/" + photos[0] body_upload = {'token': self.token} upload_image = requests.post(url, body_upload, headers=config.headers_desk, files={'image': open(photo_path, 'rb')}) data = upload_image.json() img_id = data.get('data') if upload_image.json().get('success'): ReportHandler.add_log( "Upload image", "successful, image id {0}".format(str(img_id[0].get('id')))) else: ReportHandler.add_log("Uploading image", "failed") ReportHandler.add_error("Watch add", str(upload_image.json())) return upload_image.json()
def clear_previous_logs(): ReportHandler.print_errors() ReportHandler.print_logs()
def token_builder(self, data, function=False): if not data.get('success'): if function: ReportHandler.add_log( function, "Failed to get right response\n__________________________") ReportHandler.add_error( function, "Could not get response:\n{0}".format(data)) else: ReportHandler.add_log( "Response", "Failed to get right response\n________________________") ReportHandler.add_error( "Response", "Could not get response:\n{0}".format(data)) else: data = data.get('data') if function: ReportHandler.add_log(function, "\n{0}".format(data)) else: ReportHandler.add_log("Response", "\n{0}".format(data)) try: self.token = data.get('token') except AttributeError: data = data[0] self.token = data.get('token')
def finish_app(): ReportHandler.add_log("Finalizing", "Finishing app") ReportHandler.print_errors() ReportHandler.print_logs()
def finish_approval(self): self.browser.quit() ReportHandler.add_log("Approving add", "Finished")