def click_nextpage(self, list_page_driver): if self.NEXTPAGE is None: return None try: ele = list_page_driver.find_element_by_xpath(self.NEXTPAGE) except selenium_exception.NoSuchElementException as e: return None try: ele.click() except selenium_exception.TimeoutException as e: pass time.sleep(1.5) return util.get_current_url(list_page_driver)
def get_rating(self,detail_page_driver): if self.RATING is None: return None try: ele = detail_page_driver.find_element_by_xpath(self.RATING) except selenium_exception.NoSuchElementException as e: logger.warn("详情页: "+util.get_current_url(detail_page_driver)+" 找不到rating") return None try: rating = str(float(ele.get_attribute("class").split("star")[1])*20) except Exception as e: return None return rating
def get_download_url(self, detail_page_driver): download_url = None try: ele = detail_page_driver.find_element_by_xpath(self.DOWNLOAD_URL) except selenium_exception.NoSuchElementException as e: try: detail_page_driver.refresh() except selenium_exception.TimeoutException as e: pass try: ele = detail_page_driver.find_element_by_xpath(self.DOWNLOAD_URL) except selenium_exception.NoSuchElementException as e: return download_url return util.href2url(self.SOURCE, ele.get_attribute('href'), util.get_current_url(detail_page_driver))
def get_detail_urls(self, list_page_driver): detail_urls = [] eles = list_page_driver.find_elements_by_xpath(self.DETAIL_URLS) if len(eles) == 0: try: list_page_driver.refresh() except selenium_exception.TimeoutException as e: pass eles = list_page_driver.find_elements_by_xpath(self.DETAIL_URLS) for ele in eles: detail_url = util.href2url(self.SOURCE, ele.get_attribute('href'), util.get_current_url(list_page_driver)) if detail_url: detail_urls.append(detail_url) return detail_urls
def get_download_url(self,detail_page_driver): time.sleep(1.5) download_url = None try: ele = detail_page_driver.find_element_by_xpath(self.DOWNLOAD_URL) except selenium_exception.NoSuchElementException as e: try: detail_page_driver.refresh() except selenium_exception.TimeoutException as e: pass try: ele = detail_page_driver.find_element_by_xpath(self.DOWNLOAD_URL) except selenium_exception.NoSuchElementException as e: return download_url return util.href2url(self.SOURCE,ele.get_attribute('href'),util.get_current_url(detail_page_driver))
def get_rating(self, detail_page_driver): if self.RATING is None: return None try: ele = detail_page_driver.find_element_by_xpath(self.RATING) except selenium_exception.NoSuchElementException as e: logger.warn("详情页: " + util.get_current_url(detail_page_driver) + " 找不到rating") return None try: rating = str( float(ele.get_attribute("class").split("star")[1]) * 20) except Exception as e: return None return rating
def wait_for_valid_authorization(self, username, logger): # save current page current_url = get_current_url(self) # stuck on invalid auth auth_method = "activity counts" counter = 0 while True and counter < 10: login_state = check_authorization(self, username, auth_method, logger) if login_state is False: logger.warning("not logged in") counter += 1 sleep(60) else: break # return to original page web_address_navigator(self, current_url)
def login_user(browser, logger, conn, proxy_ip, login_url, main_url, account_id, email, username, password, cookies): if not check_browser(browser, logger, proxy_ip): login_state = 2 return login_state web_address_navigator(browser, login_url) current_url = get_current_url(browser) if 'www.pinterest.com' not in current_url: cookies = False cookie_loaded = None login_state = None if cookies: try: for cookie in json.loads(cookies): if "sameSite" in cookie and cookie["sameSite"] == "None": cookie["sameSite"] = "Strict" browser.add_cookie(cookie) cookie_loaded = True logger.info("- Cookie for user '{}' loaded...".format(email)) # force refresh after cookie load or check_authorization() will FAIL reload_webpage(browser) # cookie has been LOADED, so the user SHOULD be logged in login_state = check_authorization(browser, email, "activity counts", logger, False) except: login_state = 0 if login_state == 1 and cookie_loaded: logger.info("Logged in successfully!") login_state = 1 return login_state # if user is still not logged in, then there is an issue with the cookie # so go create a new cookie. if cookie_loaded: logger.info( "- Issue with cookie for user '{}'. Creating new cookie...".format( email)) # Error could be faced due to "<button class="sqdOP L3NKy y3zKF" # type="button"> Cookie could not be loaded" or similar. # Session displayed we are in, but then a failure for the first # `login_elem` like the element is no longer attached to the DOM. # Saw this issue when session hasn't been used for a while; wich means # "expiry" values in cookie are outdated. try: # Since having issues with the cookie a new one can be generated, # if cookie cannot be created or deleted stop execution. logger.info("- Deleting browser cookies...") browser.delete_all_cookies() browser.refresh() time.sleep(random.randint(3, 5)) except Exception as e: # NF: start if isinstance(e, WebDriverException): logger.exception( "Error occurred while deleting cookies from web browser!\n\t{}" .format(str(e).encode("utf-8"))) login_state = 3 return login_state # NF: end web_address_navigator(browser, login_url) input_username_XP = read_xpath(login_user.__name__, "input_username_XP") explicit_wait(browser, logger, "VOEL", [input_username_XP, "XPath"], 35) # user input_username = browser.find_element_by_xpath(input_username_XP) (ActionChains(browser).move_to_element(input_username).click().send_keys( email).perform()) input_password = browser.find_elements_by_xpath( read_xpath(login_user.__name__, "input_password")) if not isinstance(password, str): password = str(password) (ActionChains(browser).move_to_element( input_password[0]).click().send_keys(password).perform()) sleep(1) (ActionChains(browser).move_to_element( input_password[0]).click().send_keys(Keys.ENTER).perform()) sleep(3) # check for wrong username or password message, and show it to the user try: error_alert = browser.find_element_by_xpath( "//*[contains(text(), 'The password you entered is incorrect')]") logger.warn(error_alert.text) login_state = 4 return login_state except NoSuchElementException: pass try: error_alert = browser.find_element_by_xpath( "//*[contains(text(), 'Safe mode alert')]") logger.warn(error_alert.text) login_state = 5 return login_state except NoSuchElementException: pass try: error_alert = browser.find_element_by_xpath( "//*[contains(text(), 'Your account has been suspended.')]") logger.warn(error_alert.text) login_state = 99 return login_state except NoSuchElementException: pass try: error_alert = browser.find_element_by_xpath( "//*[contains(text(), 'Want fresh ideas in your feed')]") logger.warn(error_alert.text) reload_webpage(browser) except NoSuchElementException: pass explicit_wait(browser, logger, "PFL", [], 5) # Check if user is logged-in (If there's two 'nav' elements) nav = browser.find_elements_by_xpath(read_xpath(login_user.__name__, "nav")) if len(nav) == 1: logger.info("Logged in successfully!") login_state = 1 # create cookie for username and save it cookies_list = browser.get_cookies() for cookie in cookies_list: if "sameSite" in cookie and cookie["sameSite"] == "None": cookie["sameSite"] = "Strict" cookies_list = json.dumps(cookies_list) sql = 'UPDATE account SET cookies=%s WHERE id=%s' conn.op_commit(sql, (cookies_list, account_id)) return login_state else: login_state = 0 return login_state
def upload_pic(browser, logger, conn, process_flag, current_time, account_id, username, upload_web, upload_pic_min, upload_pic_max): all_upload_num = random.randint(upload_pic_min, upload_pic_max) logger.info('Start uploading %s images...' % all_upload_num) upload_web_split = upload_web.split('.') upload_web_replace = upload_web_split[-2] + '.' + upload_web_split[-1] save_error = 0 while True: table_name = None upload_flag = 1 replace_domain = 0 save_time = (datetime.datetime.utcnow() + datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S") sql = 'SELECT * FROM upload_url where url=%s' result_1 = conn.op_select_one(sql, upload_web) if result_1: data_type = result_1['data_type'] sql = 'SELECT * FROM database_relation WHERE data_type=%s' result_2 = conn.op_select_one(sql, data_type) if result_2: table_name = result_2['table_name'] if table_name: sql = "SELECT COUNT(-1) AS allnum FROM %s WHERE id_of_saved='%s' AND save_time>='%s'" % ( table_name, account_id, current_time) upload_num = conn.op_select_one(sql)['allnum'] if upload_num < all_upload_num: random_time = random.randint(10, 30) time.sleep(random_time) if upload_web == 'original': sql = 'SELECT * FROM pin_upload WHERE saved=0 and data_type=%s ORDER BY RAND() LIMIT 1' result = conn.op_select_one(sql, upload_web) elif data_type == 'binding.domain.zhishuai.online': sql = 'SELECT * FROM %s WHERE saved=0 and belong_web="%s" ORDER BY RAND() LIMIT 1' % ( table_name, upload_web) result = conn.op_select_one(sql) else: replace_domain = 1 sql = 'SELECT * FROM %s WHERE saved=0 and data_type="%s" ORDER BY RAND() LIMIT 1' % ( table_name, data_type) result = conn.op_select_one(sql) if result: upload_pic_path = result['savelink'] if replace_domain == 1: upload_pic_path = upload_pic_path.replace( 'xxx.com', upload_web_replace) upload_pic_board = result['saveboard'] upload_pic_id = result['id'] browser.get(upload_pic_path) select_board_XP = "//div//h3[text()='%s']/../../../.." % upload_pic_board select_board_XP_flag = explicit_wait( browser, logger, "VOEL", [select_board_XP, "XPath"], 10, False) if select_board_XP_flag: select_board = browser.find_elements_by_xpath( select_board_XP)[-1] browser.execute_script( "arguments[0].scrollIntoView();", select_board) for _ in range(2): win32api.keybd_event(38, 0, 0, 0) win32api.keybd_event(38, 0, win32con.KEYEVENTF_KEYUP, 0) time.sleep(3) try: (ActionChains(browser).move_to_element( select_board).click().perform()) except: pass else: create_board_XP = "//a[contains(@href,'/board/create/')]" create_board_XP_flag = explicit_wait( browser, logger, "VOEL", [create_board_XP, "XPath"], 5, False) if create_board_XP_flag: create_board = browser.find_element_by_xpath( create_board_XP) try: (ActionChains(browser).move_to_element( create_board).click().perform()) except: pass input_board_name_XP = '//input[@id="boardNameInput"]' input_board_name_flag = explicit_wait( browser, logger, "VOEL", [input_board_name_XP, "XPath"], 10, False) if input_board_name_flag: time.sleep(2) browser.find_element_by_xpath( input_board_name_XP).send_keys( upload_pic_board) create_button = browser.find_element_by_xpath( "//div[text()='Create']/..") (ActionChains(browser).move_to_element( create_button).click().perform()) else: logger.info('Image save failed, Element not found') upload_flag = 0 if upload_flag == 1: time.sleep(5) current_url = get_current_url(browser) # upload_pic_board_replace = upload_pic_board.lower().replace('amp;', '').replace(',', '').replace('&', '').split(' ') # url_board_name = '' # for i in upload_pic_board_replace: # if i: # if url_board_name: # url_board_name = url_board_name + '-' + i # else: # url_board_name = url_board_name + i if username.lower() in current_url.lower(): logger.info( 'Image {} upload done!'.format(upload_pic_id)) write_txt_time() sql = "UPDATE %s SET saved=1, id_of_saved='%s', save_time='%s' WHERE id='%s'" % ( table_name, account_id, save_time, upload_pic_id) conn.op_commit(sql) else: save_error += 1 if save_error > 3: sql = "UPDATE account SET upload_done=4 WHERE id=%s" conn.op_commit(sql, account_id) break else: sql = "UPDATE account SET upload_done=9 WHERE id=%s" conn.op_commit(sql, account_id) logger.info('There is no data on this domain!') write_txt_time() break else: logger.info('Enough pictures have been uploaded for today!') write_txt_time() break time.sleep(2)