def __init__(self): super(OPPinterest, self).__init__() self.conn = OPMysql(MYSQLINFO) logging.config.fileConfig('logging.conf') self.logs = logging.getLogger() email = logging.handlers.SMTPHandler( ("smtp.163.com", 25), '*****@*****.**', ['*****@*****.**'], "Logging from pinterest", credentials=('*****@*****.**', '123456'), ) self.logs.addHandler(email) self.hostname = socket.gethostname() self.current_time = (datetime.datetime.utcnow() + datetime.timedelta(hours=8)).strftime("%Y-%m-%d") self.login_url = 'https://www.pinterest.com/login/?referrer=home_page' self.home_url = 'https://www.pinterest.com/homefeed/' self.driver = None self.proxy_type = 0 self.account_id = 0 self.email = None self.pwd = None self.port = 0 self.vpn = None self.proxy_ip = None self.upload_web = None self.cookie = None self.agent = None self.success_num = 0 self.config_id = 0 self.click_our_pin_control = 0 # Try to locate the home element. If there is off, you don't need to do all kinds of pop-ups # Steps and params to control self.upload_pic_control = 0 self.upload_pic_min = 0 self.upload_pic_max = 0 self.random_browsing_control = 0 self.browsing_pic_min = 0 self.browsing_pic_max = 0 self.save_home_url_control = 0 self.create_board_num = 0 self.save_pic_control = 0 self.follow_num = 0 self.pin_self_count = 0 self.created_boards = 0 self.search_words_count = 0 self.scroll_num = 0
def main(): MYSQLINFO = { "host": 'localhost', "user": '******', "passwd": '123456', "db": 'pinterest', "port": 3306, "charset": 'utf8mb4' } domain_list = get_domain() conn = OPMysql(MYSQLINFO) sql = 'SELECT count(1) as allcount FROM %s' % DB_NAME all_data_count = conn.op_select_one(sql)['allcount'] print('发布数据总量:', all_data_count) domain_count = len(domain_list) print('域名数量:', domain_count) domain_info_num = all_data_count // domain_count print('每个域名分配数据数量:', domain_info_num) for t in range(domain_count): if t > 0: start_num = t * domain_info_num + 2 else: start_num = t * domain_info_num + 1 end_num = (t + 1) * domain_info_num + 1 print(start_num, '--->', end_num) sql = 'UPDATE %s SET domain="%s", savebuttonlink=REPLACE(savebuttonlink, "newcolordress.com", "%s") WHERE id BETWEEN %d AND %d' % ( DB_NAME, domain_list[t], domain_list[t].replace( 'https://', ''), start_num, end_num) conn.op_commit(sql) conn.dispose()
from DBPools import OPMysql MYSQLINFO = { "host": 'localhost', "user": '******', "passwd": '123456', "db": 'pinterest', "port": 3306, "charset": 'utf8mb4' } conn = OPMysql(MYSQLINFO) sql = 'SELECT * FROM domain WHERE state=0' all_domain = conn.op_select_all(sql) if all_domain: for i in all_domain: domain = i['domain'] sql = 'SELECT * FROM account WHERE state=1 AND home_page!="" AND upload_web="-" AND id>2573 ORDER BY RAND() LIMIT 1' r = conn.op_select_one(sql) if r: email = r['email'] sql = 'UPDATE account SET upload_web=%s, setting_num=6 WHERE email=%s' conn.op_commit(sql, (domain, email)) print(email, ': Domain name added!') sql = 'UPDATE domain SET state=1 WHERE domain=%s' conn.op_commit(sql, domain) else: print('No assigned domain name!')
class OPPinterest(): def __init__(self): super(OPPinterest, self).__init__() self.conn = OPMysql(MYSQLINFO) logging.config.fileConfig('logging.conf') self.logs = logging.getLogger() email = logging.handlers.SMTPHandler( ("smtp.163.com", 25), '*****@*****.**', ['*****@*****.**'], "Logging from pinterest", credentials=('*****@*****.**', '123456'), ) self.logs.addHandler(email) self.hostname = socket.gethostname() self.current_time = (datetime.datetime.utcnow() + datetime.timedelta(hours=8)).strftime("%Y-%m-%d") self.login_url = 'https://www.pinterest.com/login/?referrer=home_page' self.home_url = 'https://www.pinterest.com/homefeed/' self.driver = None self.proxy_type = 0 self.account_id = 0 self.email = None self.pwd = None self.port = 0 self.vpn = None self.proxy_ip = None self.upload_web = None self.cookie = None self.agent = None self.success_num = 0 self.config_id = 0 self.click_our_pin_control = 0 # Try to locate the home element. If there is off, you don't need to do all kinds of pop-ups # Steps and params to control self.upload_pic_control = 0 self.upload_pic_min = 0 self.upload_pic_max = 0 self.random_browsing_control = 0 self.browsing_pic_min = 0 self.browsing_pic_max = 0 self.save_home_url_control = 0 self.create_board_num = 0 self.save_pic_control = 0 self.follow_num = 0 self.pin_self_count = 0 self.created_boards = 0 self.search_words_count = 0 self.scroll_num = 0 def action(self): while True: step_flag = 1 if self.success_num > 4: os.system('shutdown -r') print('Clear cache') time.sleep(9999) write_txt_time() print('Host Name:', self.hostname) self.get_account_count() self.get_account() if self.account_id > 0: self.get_config() self.success_num += 1 write_txt_time() step_flag = self.re_driver(step_flag) if step_flag == 1: login_state = login(self.driver, self.login_url, self.account_id, self.email, self.pwd, self.cookie) time.sleep(1) if login_state == 1 or login_state == 11: sql = "UPDATE account SET state=1, action_time=%s, proxy_err_times=0 WHERE id=%s" self.conn.op_commit( sql, (self.current_time, self.account_id)) if login_state == 11: cookie = get_coo(self.driver) sql = 'UPDATE account SET cookie=%s WHERE id=%s' self.conn.op_commit(sql, (cookie, self.account_id)) self.driver.get(self.home_url) time.sleep(5) handle_pop_up(self.driver) else: if login_state == 2: sql = 'UPDATE account set proxy_err_times=proxy_err_times+1 where id=%s' self.conn.op_commit(sql, self.account_id) sql = 'UPDATE account SET state=%s, login_times=login_times+1, action_computer="-" WHERE id=%s' self.conn.op_commit(sql, (login_state, self.account_id)) step_flag = 0 print('Account log-in failure, will exit the browser!') try: self.driver.quit() except: pass time.sleep(5) continue if step_flag == 1: if self.save_home_url_control == 1: print('Save home page!') save_home_url(self.driver, self.conn, self.account_id) if self.create_board_num > 0 and self.created_boards < self.create_board_num: print('Start create board') create_board(self.driver, self.conn, self.home_url, self.account_id, self.create_board_num) if self.follow_num > 0: follow(self.driver, self.conn, self.home_url, step_flag, self.account_id, self.follow_num) if self.random_browsing_control == 1: random_browsing(self.driver, self.conn, self.home_url, self.account_id, step_flag, self.save_pic_control, self.browsing_pic_min, self.browsing_pic_max) if self.click_our_pin_control == 1: click_our_pin(self.driver, self.conn, self.home_url, step_flag, self.current_time, self.scroll_num, self.pin_self_count, self.search_words_count, self.account_id) if self.upload_web != '-' and self.upload_pic_control == 1: upload_pic(self.driver, self.conn, step_flag, self.current_time, self.account_id, self.upload_web, self.upload_pic_min, self.upload_pic_max) print('End of account processing...') time.sleep(3) self.driver.quit() sql = 'UPDATE account SET login_times=0, action_computer="-" WHERE id=%s' self.conn.op_commit(sql, self.account_id) self.conn.dispose() write_txt_time() time.sleep(10) else: print('Not data! The system will reboot in 30 minutes...') write_txt_time() os.system('shutdown -r -t 1800') time.sleep(1800) break # Access to the account def get_account(self): sql = 'SELECT * from machine where v_name=%s' machine_info = self.conn.op_select_one(sql, self.hostname[0]) if machine_info: machine_type = machine_info['machine_type'] self.proxy_ip = machine_info['proxy_ip'] if self.hostname == 'vinter-wang-co': sql = 'SELECT * FROM account WHERE id=1' result = self.conn.op_select_one(sql) self.get_account_info(result) else: sql = "SELECT * FROM account WHERE proxy_type=%s AND action_computer=%s AND action_time<%s AND state=1 AND login_times<4 ORDER BY action_time ASC LIMIT 1" result = self.conn.op_select_one( sql, (machine_type, self.hostname, self.current_time)) if result: self.get_account_info(result) else: sql = "SELECT * FROM account WHERE proxy_type=%s AND action_computer='-' AND action_time<%s AND state=1 AND login_times<4 ORDER BY action_time ASC limit 1" result = self.conn.op_select_one( sql, (machine_type, self.current_time)) if result: self.get_account_info(result) sql = "UPDATE account SET action_computer=%s WHERE id=%s" self.conn.op_commit(sql, (self.hostname, self.account_id)) write_txt_time() else: print('Not Data!') def get_account_info(self, result): self.proxy_type = result['proxy_type'] self.account_id = result["id"] self.email = result["email"] self.pwd = result["pw"] self.port = result['port'] self.vpn = result['vpn'] self.upload_web = result['upload_web'] self.cookie = result['cookie'] self.created_boards = result['created_boards'] self.config_id = result['setting_num'] self.agent = result['agent'] if not self.agent: sql = 'SELECT * FROM user_agent WHERE terminal="computer" ORDER BY RAND() LIMIT 1' agent_in_sql = self.conn.op_select_one(sql) if agent_in_sql: self.agent = agent_in_sql['user_agent'] agent_id = agent_in_sql['Id'] sql = 'UPDATE account SET agent=%s WHERE id=%s' self.conn.op_commit(sql, (self.agent, self.account_id)) print("Start account processing..." + '\n' + "ID:", self.account_id, "Email:", self.email) def re_driver(self, step_flag): execute_path = os.path.abspath('.') webdriver_path = execute_path + '\\boot\\chromedriver.exe' options = webdriver.ChromeOptions() options.add_argument('disable-infobars') options.add_argument('user-agent="%s"' % self.agent) prefs = { 'profile.default_content_setting_values': { 'notifications': 2 } } options.add_experimental_option('prefs', prefs) if self.vpn: connect_vpn(self.conn, self.agent, self.vpn, execute_path) else: rasphone_vpn(execute_path) time.sleep(1) options.add_argument("--proxy-server=http://%s:%d" % (self.proxy_ip, self.port)) self.driver = webdriver.Chrome(executable_path=webdriver_path, options=options) self.driver.maximize_window() return step_flag def get_account_count(self): sql = 'SELECT * FROM account_count WHERE id=1' result = self.conn.op_select_one(sql) if result: all_count = result['all_count'] real_time_num = result['real_time_num'] max_account_num = result['max_account_num'] last_update_time = result['last_update_time'] if str(last_update_time) < self.current_time: recovery_mode = 'UPDATE account SET state=1 WHERE state=0' self.conn.op_commit(recovery_mode) recovery_proxy_state = 'UPDATE account set state=1 WHERE state=2 and proxy_err_times<4' self.conn.op_commit(recovery_proxy_state) sql = '''UPDATE account_count SET last_update_time=%s, all_count= (SELECT COUNT(1) FROM account WHERE state=1) WHERE id=1''' self.conn.op_commit(sql, self.current_time) else: sql = 'UPDATE account_count SET real_time_num=(SELECT count(1) FROM account WHERE state=1) WHERE id=1' self.conn.op_commit(sql) if all_count - real_time_num > max_account_num: print('Too many account errors today to suspend operations!') self.logs.error( 'The maximum error limit of %d accounts has been exceeded!' % max_account_num, exc_info=True) os.system('shutdown -r -t 1800') time.sleep(9999) def get_config(self): print('Run configuration:', self.config_id) sql = 'SELECT * FROM configuration WHERE id=%s' result = self.conn.op_select_one(sql, self.config_id) if result: self.random_browsing_control = result['random_browsing_control'] self.browsing_pic_min = result['bro_pic_min'] self.browsing_pic_max = result['bro_pic_max'] self.save_home_url_control = result['save_home_page'] self.save_pic_control = result['save_pic_control'] self.follow_num = result['follow_num'] self.pin_self_count = result['pin_self_count'] self.create_board_num = result['create_board_num'] self.search_words_count = result['search_words_count'] self.scroll_num = result['scroll_num'] self.click_our_pin_control = result['click_our_pin_control'] self.upload_pic_control = result['upload_pic_control'] self.upload_pic_min = result['upload_pic_min'] self.upload_pic_max = result['upload_pic_max']
import configparser from DBPools import OPMysql profile = 'config.ini' profile_info = configparser.ConfigParser() profile_info.read(profile, encoding='utf-8') pro_info_items = dict(profile_info.items('pinterest')) MYSQLINFO = { "host": pro_info_items['host'], "user": pro_info_items['user'], "password": pro_info_items['password'], "db": pro_info_items['db'], "port": 3306, "charset": 'utf8mb4' } conn = OPMysql(MYSQLINFO) sql = 'SELECT * from account where state=5 and setting_num=6' ccc = conn.op_select_all(sql) for i in ccc: account_id = i['id'] upload_web = i['upload_web'] print(account_id, upload_web) sql = 'UPDATE account SET state=99, upload_web="-", upload_done=0, setting_num=1 WHERE id=%s' conn.op_commit(sql, account_id) sql = 'UPDATE upload_url set status=0 where url=%s' conn.op_commit(sql, upload_web)
profile = 'config.ini' profile_info = configparser.ConfigParser() profile_info.read(profile, encoding='utf-8') pro_info_items = dict(profile_info.items('pinterest')) MYSQLINFO = { "host": pro_info_items['host'], "user": pro_info_items['user'], "password": pro_info_items['password'], "db": pro_info_items['db'], "port": 3306, "charset": 'utf8mb4' } conn = OPMysql(MYSQLINFO) def allocation(): sql = 'SELECT * from upload_url where status=0' all_domains = conn.op_select_all(sql) if all_domains: for i in all_domains: domain = i['url'] # domain = 'original' print(domain) sql = 'UPDATE account set upload_web=%s, upload_done=1, setting_num=6 where state=1 and upload_done=0 and upload_web="-" order by rand() limit 1' conn.op_commit(sql, domain) sql = 'UPDATE upload_url set status=1 where url=%s' conn.op_commit(sql, domain)
class Pinterest(): def __init__(self): super(Pinterest, self).__init__() self.conn = None self.logger = None self.hostname = socket.gethostname() self.current_time = (datetime.datetime.utcnow() + datetime.timedelta(hours=8)).strftime("%Y-%m-%d") self.login_url = 'https://www.pinterest.com/login/?referrer=home_page/' self.home_url = 'https://www.pinterest.com/homefeed/' self.main_url = 'https://www.pinterest.com/' self.browser = None self.proxy_type = 0 self.account_id = 0 self.email = None self.username = None self.password = None self.port = 0 self.host_ip = None self.proxy_ip = None self.zone = None self.customer = None self.customer_pwd = None self.upload_web = None self.upload_done = 0 self.cookies = None self.agent = None self.success_num = 0 self.config_id = 0 self.click_our_pin_control = 0 # Try to locate the home element. If there is off, you don't need to do all kinds of pop-ups # Steps and params to control self.upload_pic_control = 0 self.upload_pic_min = 0 self.upload_pic_max = 0 self.random_browsing_control = 0 self.browsing_pic_min = 0 self.browsing_pic_max = 0 self.get_user_name = 0 self.create_board_num = 0 self.save_pic_control = 0 self.follow_num = 0 self.pin_self_count = 0 self.created_boards = 0 self.search_words_count = 0 self.scroll_num = 0 def action(self): while True: process_flag = True if self.success_num > 10: os.system('shutdown -r') self.logger.info('Clear cache') time.sleep(9999) write_txt_time() self.conn = OPMysql(MYSQLINFO) self.get_account_count() self.get_account() self.logger = self.get_pinbot_logger(False, True) self.logger.info("Start account processing...") self.logger.info('Host Name: {}'.format(self.hostname)) self.logger.info('ID: {}'.format(self.account_id)) self.logger.info('Port: {}'.format(self.port)) if self.account_id > 0: self.get_config() self.success_num += 1 process_flag = wait_port_opening(self.logger, process_flag) if process_flag: gen_port_status_code = generate_configuration( self.conn, self.host_ip, self.port, self.proxy_ip, self.zone, self.customer, self.customer_pwd) if gen_port_status_code != 200: process_flag = False else: self.logger.info( 'Wait more than 60 seconds for the port to be ready, attempt to restart machine!' ) os.system('shutdown -r') time.sleep(9999) write_txt_time() if process_flag: self.browser = set_selenium_local_session( self.host_ip, self.port, False, None, 25, self.logger) login_state = login_user(self.browser, self.logger, self.conn, self.proxy_ip, self.login_url, self.main_url, self.account_id, self.email, self.username, self.password, self.cookies) write_txt_time() time.sleep(1) if login_state == 1 or login_state == 11: sql = "UPDATE account SET state=1, action_time=%s, proxy_err_times=0 WHERE id=%s" self.conn.op_commit( sql, (self.current_time, self.account_id)) if login_state == 11: cookies = get_coo(self.browser) sql = 'UPDATE account SET cookies=%s WHERE id=%s' self.conn.op_commit(sql, (cookies, self.account_id)) self.browser.get(self.home_url) write_txt_time() time.sleep(5) # handle_pop_up(self.browser) else: if login_state == 2: # sql = 'UPDATE account set proxy_err_times=proxy_err_times+1 where id=%s' # self.conn.op_commit(sql, self.account_id) # sql = 'SELECT proxy_err_times FROM account WHERE id=%s' # r_error = self.conn.op_select_one(sql, self.account_id) # if r_error: # proxy_err_times = r_error['proxy_err_times'] # if int(proxy_err_times) >= 4: sql = 'UPDATE port_info SET state=2 WHERE port=%s' self.conn.op_commit(sql, self.port) sql = 'UPDATE account SET state=2, action_computer="-" WHERE id=%s' self.conn.op_commit(sql, self.account_id) else: sql = 'UPDATE account SET state=%s, login_times=login_times+1, action_computer="-" WHERE id=%s' # sql = 'UPDATE account SET state=%s, login_times=login_times+1 WHERE id=%s' self.conn.op_commit(sql, (login_state, self.account_id)) process_flag = False self.logger.info( 'Login false! will exit the browser!') close_browser(self.browser, self.logger) time.sleep(5) self.conn.dispose() delete_port(self.port, self.host_ip) write_txt_time() continue if process_flag: if self.get_user_name == 1 and self.username == '-': self.logger.info('Get username!') self.username = get_user_link(self.browser, self.logger, self.conn, self.account_id) # save_home_url(self.browser, self.logger, self.conn, self.account_id) # if self.create_board_num > 0 and self.created_boards < self.create_board_num: # self.logger.info('Start create board') # create_board(self.browser, self.logger, self.conn, self.home_url, self.account_id, self.create_board_num) # if self.follow_num > 0: # follow(self.browser, self.logger, self.conn, self.home_url, self.account_id, self.follow_num, self.current_time) # if self.random_browsing_control == 1: # random_browsing( # self.browser, self.logger, self.conn, self.home_url, self.account_id, process_flag, self.save_pic_control, self.browsing_pic_min, self.browsing_pic_max) # if self.click_our_pin_control == 1: # click_our_pin(self.browser, self.logger, self.conn, self.home_url, process_flag, self.current_time, self.scroll_num, self.pin_self_count, self.search_words_count, self.account_id) if self.upload_done == 1 and self.upload_web != '-' and self.upload_pic_control == 1: upload_pic(self.browser, self.logger, self.conn, process_flag, self.current_time, self.account_id, self.username, self.upload_web, self.upload_pic_min, self.upload_pic_max) self.logger.info('End of account processing...') time.sleep(3) close_browser(self.browser, self.logger) sql = 'UPDATE account SET login_times=0, job_done=1, action_computer="-" WHERE id=%s' self.conn.op_commit(sql, self.account_id) self.conn.dispose() delete_port(self.port, self.host_ip) write_txt_time() time.sleep(10) else: self.logger.info( 'Not data! The system will reboot in 30 minutes...') write_txt_time() os.system('shutdown -s -t 5') break def get_pinbot_logger(self, save_logs: bool, show_logs: bool, log_handler=None): logger = logging.getLogger(self.email) logger.setLevel(logging.DEBUG) # log name and format general_log = "general.log" extra = {"username": self.email} logger_formatter = logging.Formatter( "%(levelname)s [%(asctime)s] [%(username)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S", ) if save_logs is True: file_handler = logging.FileHandler(general_log) # log rotation, 5 logs with 10MB size each one file_handler = RotatingFileHandler(general_log, maxBytes=10 * 1024 * 1024, backupCount=5) file_handler.setLevel(logging.DEBUG) file_handler.setFormatter(logger_formatter) logger.addHandler(file_handler) # add custom user handler if given if log_handler: logger.addHandler(log_handler) if show_logs is True: console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) console_handler.setFormatter(logger_formatter) logger.addHandler(console_handler) logger = logging.LoggerAdapter(logger, extra) return logger # Access to the account def get_account(self): if self.hostname == 'v-PC': self.host_ip = '127.0.0.1' sql = 'SELECT * FROM account WHERE id=5441' result = self.conn.op_select_one(sql) self.get_account_info(result) else: sql = 'SELECT * from virtual_machine_info where v_name="proxy"' machine_info = self.conn.op_select_one(sql) if machine_info: self.host_ip = machine_info['host_ip'] sql = "SELECT * FROM account WHERE action_computer=%s AND job_done=0 AND state=1 AND setting_num=6 AND login_times<3 ORDER BY action_time ASC LIMIT 1" result = self.conn.op_select_one(sql, self.hostname) if result: self.get_account_info(result) else: sql = "SELECT * FROM account WHERE action_computer='-' AND job_done=0 AND state=1 AND setting_num=6 AND login_times<3 ORDER BY action_time ASC limit 1" result = self.conn.op_select_one(sql) if result: self.get_account_info(result) sql = "UPDATE account SET action_computer=%s WHERE id=%s" self.conn.op_commit(sql, (self.hostname, self.account_id)) write_txt_time() def get_account_info(self, result): self.account_id = result["id"] self.email = result["email"] self.password = result["pw"] self.port = result['port'] self.username = result['username'] self.upload_web = result['upload_web'] self.upload_done = result['upload_done'] self.cookies = result['cookies'] self.created_boards = result['created_boards'] self.config_id = result['setting_num'] self.agent = result['agent'] sql = 'SELECT ip FROM port_info WHERE port=%s' port_res = self.conn.op_select_one(sql, self.port) if port_res: self.proxy_ip = port_res['ip'] sql = 'SELECT * FROM proxy_account_info WHERE id=1' lpm_info = self.conn.op_select_one(sql) if lpm_info: self.zone = lpm_info['zone'] self.customer = lpm_info['customer'] self.customer_pwd = lpm_info['customer_pwd'] if not self.agent: sql = 'SELECT * FROM user_agent WHERE terminal="computer" ORDER BY RAND() LIMIT 1' agent_in_sql = self.conn.op_select_one(sql) if agent_in_sql: self.agent = agent_in_sql['user_agent'] agent_id = agent_in_sql['Id'] sql = 'UPDATE account SET agent=%s WHERE id=%s' self.conn.op_commit(sql, (self.agent, self.account_id)) # 浏览器初始化配置 def re_driver(self): execute_path = os.path.abspath('.') webdriver_path = os.path.join(execute_path, 'boot', 'geckodriver.exe') user_data_path = os.path.join(os.environ['USERPROFILE'], 'AppData', 'Local', 'Google', 'Chrome', 'User Data') cookies_file_path = os.path.join(os.environ['USERPROFILE'], 'AppData', 'Local', 'Google', 'Chrome', 'User Data', 'default', 'Cookies') # 尝试关闭已经打开的浏览器进程 try: os.popen('taskkill /f /im chrome.exe') except: pass # 清除cookies try: sql = 'DROP table cookies' sqlite_conn = sqlite3.connect("cookies") sqlite_cursor = sqlite_conn.cursor() sqlite_cursor.execute(sql) except: pass options = webdriver.ChromeOptions() # options.add_argument('--incognito') # 无痕模式 options.add_experimental_option('useAutomationExtension', False) # 躲避网站检测webdriver options.add_experimental_option( "excludeSwitches", ['enable-automation']) # 关闭chrome正受到自动测试软件的控制的提示 # window.navigator.webdriver # 躲避网站检测webdriver options.add_argument("--disable-blink-features") options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument('--disable-gpu') # 规避bug options.add_argument( 'blink-settings=imagesEnabled=false') # 不加载图片, 提升速度 options.add_argument('--no-sandbox') # 以最高权限运行 options.add_argument('--disable-popup-blocking') # 禁止弹窗 # options.add_argument('user-data-dir=%s' % user_data_path) # 使用浏览器配置文件 options.add_argument('user-agent=%s' % self.agent) if self.hostname != 'DESKTOP-KHDQKRQ': options.add_argument("--proxy-server=http://%s:%d" % (self.host_ip, self.port)) self.browser = webdriver.Chrome(executable_path=webdriver_path, options=options) # self.logger.info(driver.execute_script("return navigator.userAgent;")) # UA设置是否成功 # 躲避网站检测webdriver, 此方法暂时弃用, 因为这会导致不一致 # self.browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { # "source": """ # Object.defineProperty(navigator, 'webdriver', { # get: () => undefined # }) # """ # }) self.browser.maximize_window() def get_account_count(self): sql = 'SELECT * FROM account_count WHERE id=1' result = self.conn.op_select_one(sql) if result: all_count = result['all_count'] real_time_num = result['real_time_num'] max_error_num = result['max_error_num'] last_update_time = result['last_update_time'] if str(last_update_time) < self.current_time: sql = 'SELECT state, COUNT(1) AS status_count FROM account GROUP BY state' all_status = self.conn.op_select_all(sql) if all_status: status_0 = status_1 = status_2 = status_9 = status_66 = status_99 = 0 for status in all_status: if int(status['state']) == 0: status_0 = status['status_count'] if int(status['state']) == 1: status_1 = status['status_count'] if int(status['state']) == 2: status_2 = status['status_count'] if int(status['state']) == 9: status_9 = status['status_count'] if int(status['state']) == 66: status_66 = status['status_count'] if int(status['state']) == 99: status_99 = status['status_count'] sql = 'INSERT INTO account_status_statistics (status_0, status_1, status_2, status_9, status_66, status_99, up_time) VALUES (%s, %s, %s, %s, %s, %s, %s)' self.conn.op_commit( sql, (status_0, status_1, status_2, status_9, status_66, status_99, self.current_time)) recovery_mode = 'UPDATE account SET state=1 WHERE state=0' self.conn.op_commit(recovery_mode) recovery_job = 'UPDATE account SET job_done=0 WHERE state=1' self.conn.op_commit(recovery_job) recovery_proxy_state = 'UPDATE account set state=1 WHERE state=2 and proxy_err_times<4' self.conn.op_commit(recovery_proxy_state) sql = '''UPDATE account_count SET last_update_time=%s, all_count= (SELECT COUNT(1) FROM account WHERE state=1), real_time_num=(SELECT count(1) FROM account WHERE state=1) WHERE id=1''' self.conn.op_commit(sql, self.current_time) else: sql = 'UPDATE account_count SET real_time_num=(SELECT count(1) FROM account WHERE state=1) WHERE id=1' self.conn.op_commit(sql) if all_count - real_time_num > max_error_num: self.logger.info( 'Too many account errors today to suspend operations!') self.logger.info( 'The maximum error limit of {} accounts has been exceeded!'. format(max_error_num)) os.system('shutdown -r -t 1800') time.sleep(9999) def get_config(self): self.logger.info('Run configuration: {}'.format(self.config_id)) sql = 'SELECT * FROM configuration WHERE id=%s' result = self.conn.op_select_one(sql, self.config_id) if result: self.random_browsing_control = result['random_browsing_control'] self.browsing_pic_min = result['bro_pic_min'] self.browsing_pic_max = result['bro_pic_max'] self.get_user_name = result['get_user_name'] self.save_pic_control = result['save_pic_control'] self.follow_num = result['follow_num'] self.pin_self_count = result['pin_self_count'] self.create_board_num = result['create_board_num'] self.search_words_count = result['search_words_count'] self.scroll_num = result['scroll_num'] self.click_our_pin_control = result['click_our_pin_control'] self.upload_pic_control = result['upload_pic_control'] self.upload_pic_min = result['upload_pic_min'] self.upload_pic_max = result['upload_pic_max']
def action(self): while True: process_flag = True if self.success_num > 10: os.system('shutdown -r') self.logger.info('Clear cache') time.sleep(9999) write_txt_time() self.conn = OPMysql(MYSQLINFO) self.get_account_count() self.get_account() self.logger = self.get_pinbot_logger(False, True) self.logger.info("Start account processing...") self.logger.info('Host Name: {}'.format(self.hostname)) self.logger.info('ID: {}'.format(self.account_id)) self.logger.info('Port: {}'.format(self.port)) if self.account_id > 0: self.get_config() self.success_num += 1 process_flag = wait_port_opening(self.logger, process_flag) if process_flag: gen_port_status_code = generate_configuration( self.conn, self.host_ip, self.port, self.proxy_ip, self.zone, self.customer, self.customer_pwd) if gen_port_status_code != 200: process_flag = False else: self.logger.info( 'Wait more than 60 seconds for the port to be ready, attempt to restart machine!' ) os.system('shutdown -r') time.sleep(9999) write_txt_time() if process_flag: self.browser = set_selenium_local_session( self.host_ip, self.port, False, None, 25, self.logger) login_state = login_user(self.browser, self.logger, self.conn, self.proxy_ip, self.login_url, self.main_url, self.account_id, self.email, self.username, self.password, self.cookies) write_txt_time() time.sleep(1) if login_state == 1 or login_state == 11: sql = "UPDATE account SET state=1, action_time=%s, proxy_err_times=0 WHERE id=%s" self.conn.op_commit( sql, (self.current_time, self.account_id)) if login_state == 11: cookies = get_coo(self.browser) sql = 'UPDATE account SET cookies=%s WHERE id=%s' self.conn.op_commit(sql, (cookies, self.account_id)) self.browser.get(self.home_url) write_txt_time() time.sleep(5) # handle_pop_up(self.browser) else: if login_state == 2: # sql = 'UPDATE account set proxy_err_times=proxy_err_times+1 where id=%s' # self.conn.op_commit(sql, self.account_id) # sql = 'SELECT proxy_err_times FROM account WHERE id=%s' # r_error = self.conn.op_select_one(sql, self.account_id) # if r_error: # proxy_err_times = r_error['proxy_err_times'] # if int(proxy_err_times) >= 4: sql = 'UPDATE port_info SET state=2 WHERE port=%s' self.conn.op_commit(sql, self.port) sql = 'UPDATE account SET state=2, action_computer="-" WHERE id=%s' self.conn.op_commit(sql, self.account_id) else: sql = 'UPDATE account SET state=%s, login_times=login_times+1, action_computer="-" WHERE id=%s' # sql = 'UPDATE account SET state=%s, login_times=login_times+1 WHERE id=%s' self.conn.op_commit(sql, (login_state, self.account_id)) process_flag = False self.logger.info( 'Login false! will exit the browser!') close_browser(self.browser, self.logger) time.sleep(5) self.conn.dispose() delete_port(self.port, self.host_ip) write_txt_time() continue if process_flag: if self.get_user_name == 1 and self.username == '-': self.logger.info('Get username!') self.username = get_user_link(self.browser, self.logger, self.conn, self.account_id) # save_home_url(self.browser, self.logger, self.conn, self.account_id) # if self.create_board_num > 0 and self.created_boards < self.create_board_num: # self.logger.info('Start create board') # create_board(self.browser, self.logger, self.conn, self.home_url, self.account_id, self.create_board_num) # if self.follow_num > 0: # follow(self.browser, self.logger, self.conn, self.home_url, self.account_id, self.follow_num, self.current_time) # if self.random_browsing_control == 1: # random_browsing( # self.browser, self.logger, self.conn, self.home_url, self.account_id, process_flag, self.save_pic_control, self.browsing_pic_min, self.browsing_pic_max) # if self.click_our_pin_control == 1: # click_our_pin(self.browser, self.logger, self.conn, self.home_url, process_flag, self.current_time, self.scroll_num, self.pin_self_count, self.search_words_count, self.account_id) if self.upload_done == 1 and self.upload_web != '-' and self.upload_pic_control == 1: upload_pic(self.browser, self.logger, self.conn, process_flag, self.current_time, self.account_id, self.username, self.upload_web, self.upload_pic_min, self.upload_pic_max) self.logger.info('End of account processing...') time.sleep(3) close_browser(self.browser, self.logger) sql = 'UPDATE account SET login_times=0, job_done=1, action_computer="-" WHERE id=%s' self.conn.op_commit(sql, self.account_id) self.conn.dispose() delete_port(self.port, self.host_ip) write_txt_time() time.sleep(10) else: self.logger.info( 'Not data! The system will reboot in 30 minutes...') write_txt_time() os.system('shutdown -s -t 5') break
import openpyxl from DBPools import OPMysql MYSQLINFO = { "host": 'localhost', "user": '******', "passwd": '123456', "db": 'pro_info', "port": 3306, "charset": 'utf8mb4' } conn = OPMysql(MYSQLINFO) sql = 'SELECT * FROM products WHERE have_picture=1' results = conn.op_select_all(sql) if results: workbook = openpyxl.Workbook() # wb=openpyxl.Workbook(encoding='UTF-8') worksheet = workbook.active worksheet.title = "Sheet1" # worksheet2 = workbook.create_sheet() # 默认插在工作簿末尾 # worksheet2 = workbook.create_sheet(0) #插入在工作簿的第一个位置 # worksheet2.title = "Sheet2" field = ['产品名称', 'Model', '分类', '现价', '原价', '尺码', '颜色', '图片', '描述'] for i in range(len(field)): worksheet.cell(1, i + 1, field[i]) row_num = 2 for row in results: field_value_lst = []