# -*- 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.open() # 登录 self.login() # 开始进入抢购下单核心程序 self.core() # 关闭 self.close() except BaseException, e: print e self.start() 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') while 1: state = self.check(config['link']) if state == True: try: 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, e: print e product.status(config['id'], 2) continue else: gevent.sleep(30) def check(self, product): r = requests.get(product) # 缺货标识 string = 'http://image2.ssgdfs.com/images/shop/cn/renewal/content/btn_soldout.gif' data = r.text if string in data: return False else: return True def open(self): self.driver = webdriver.Remote(command_executor=Demeter.config['setting']['phantomjs'], desired_capabilities=DesiredCapabilities.PHANTOMJS) def login(self): self.driver.get(self.config['login_link']) 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) def order(self, product): self.driver.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() 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 微信00 self.driver.find_element_by_xpath('//input[@id="pymntMeansCode" and @value="00"]').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 close(self): self.driver.close()