# -*- coding: utf-8 -*- """ demeter service name:ssgdfs.py ssgdfs网站抢单业务 author:rabin """ from .__load__ import * class Ssgdfs(object): def init(self, config): self.config = config def start(self): if 'product' in self.config and self.config['product']: try: # 启动 self.restart() # 开始进入抢购下单核心程序 self.core() # 关闭 self.quit() except BaseException as e: print e self.start() def restart(self): # 打开浏览器 self.open() # 登录 self.login() def core(self): # 开启多任务 ''' task = [] for v in self.config['product']: task.append(gevent.spawn(self.buy, v)) gevent.joinall(task) ''' for v in self.config['product']: self.buy(v) def buy(self, config): product = Demeter.service('product') self.num = 0 while 1: state = self.check(config['link']) if state == True: try: self.num = 0 print('get' + config['link']) product.status(config['id'], 3) # 下单 self.order(config['link']) # 支付并获取支付id order = product.createOrderId(self.pay(), config['site_id'], config['id']) # 支付截图 pic = self.crop(order, config['id']) # 生成订单 product.order(order, pic, config['site_id'], config['id']) product.status(config['id'], 4) break except BaseException as e: print e product.status(config['id'], 2) continue else: if self.num == 100: print 'restart' self.restart() break else: time.sleep(30) ''' self.num = self.num + 1 #self.close() if self.num >= 10: self.quit() product.status(config['id'], 1) print 'kill' break else: time.sleep(30) ''' def check(self, product): #r = requests.get(product) try: self.get(product) data = self.driver.page_source #print(data) # 可以购买 string = 'btn_direct_buy.gif' #data = r.text if string in data: return True else: return False except BaseException as e: self.num = 100 return False def open(self): #self.display = Display(visible=0, size=(800, 600)) #self.display.start() dcap = dict(DesiredCapabilities.PHANTOMJS) #dcap["phantomjs.page.settings.userAgent"] = (random.choice(USER_AGENTS)) # 不载入图片 dcap["phantomjs.page.settings.loadImages"] = False # 设置代理 #service_args = ['--proxy=127.0.0.1:9999','--proxy-type=socks5'] #self.driver = webdriver.Remote(command_executor=Demeter.config['setting']['phantomjs'], desired_capabilities=DesiredCapabilities.PHANTOMJS) self.driver = webdriver.Remote(command_executor=Demeter.config['setting']['phantomjs'], desired_capabilities=DesiredCapabilities.PHANTOMJS) #options = webdriver.FirefoxOptions() #options.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"') #self.driver = webdriver.Chrome(firefox_options=options) def login(self): self.get(self.config['login_link']) #source_code = self.driver.page_source self.driver.find_element_by_id('login-id').send_keys(self.config['username']) self.driver.find_element_by_id('login-password').send_keys(self.config['password']) self.driver.find_element_by_xpath('//input[@alt="login"]').click() self.driver.implicitly_wait(5) #self.close() def order(self, product): #self.get('http://cn.ssgdfm.com/shop/common/cookieEnable?ce=null&returnUrl=%2Fshop%2Fproduct%2FproductDetail%3FprdtCode%3D05710004801') #self.get(product) # 立刻购买 self.driver.execute_script('directOrderProduct()') # 等待加载页面完成 self.wait('long-sub') # 等待弹层关闭 self.waitNot('pay_popup') # 输入下单信息 self.order_content() # 确认订单 self.driver.execute_script('goPaymentCheck()') # 等待加载页面完成 self.wait('order-agree2') def order_content(self): # 选择护照信息 #self.driver.execute_script('setExitInfoHistory("' + self.config['chuguo'] + '")') #self.driver.execute_script('setExitInfoHistory("3239636")') self.driver.find_element_by_class_name('btn-popup-exitInfo').click() exit = self.driver.find_elements_by_class_name('btn-red') exitLength = len(exit)-1 rand = random.randint(0, exitLength) exit[rand].click() # 使用优惠券 # 使用积分 self.driver.find_element_by_xpath('//input[@name="nowUse" and @value="0"]').click() self.driver.find_element_by_id('totalUse').click() def pay(self): # 等待弹层关闭 self.waitNot('loding_popup') # 输入支付信息 self.pay_content() # 确认按钮 self.driver.execute_script('window.confirm = function(msg) { return true; }'); # 同意付款 self.driver.execute_script('$("a.goPayment").click()') #self.driver.switch_to_alert().accept() #self.wait('qrcode-img-wrapper') self.wait('qrcode') sleep(2) # 获取当前链接,没啥用 #link = self.driver.execute_script('return location.href') # 获取订单号 这里获取不到,直接生成吧 order = False return order def pay_content(self): # 确认取货处 self.driver.find_element_by_id('exitDestInfo').click() # 同意收集个人信息 self.driver.find_element_by_id('agree').click() # 同意订购 self.driver.find_element_by_id('agree01').click() # 选择支付方式:支付宝21 微信16 self.driver.find_element_by_xpath('//input[@id="pymntMeansCode" and @value="16"]').click() def crop(self, order, id): path = Demeter.path + 'runtime/upload/' if not File.exists(path): File.mkdir(path) pic = path + str(id) + '_' + str(order) + '.png' self.driver.save_screenshot(pic) return pic def waitNot(self, name): WebDriverWait(self.driver, 100).until_not(lambda x: x.find_element_by_class_name(name).is_displayed()) def wait(self, name): WebDriverWait(self.driver, 100).until(lambda x: x.find_element_by_class_name(name).is_displayed()) def get(self, link): self.driver.get(link) self.driver.implicitly_wait(10) def close(self): self.driver.close() #self.driver.close() #self.display.stop() def quit(self): self.driver.quit() #self.driver.close() #self.display.stop()