ssgdfs.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. """
  3. demeter service
  4. name:ssgdfs.py ssgdfs网站抢单业务
  5. author:rabin
  6. """
  7. from .__load__ import *
  8. class Ssgdfs(object):
  9. def init(self, config):
  10. self.config = config
  11. def start(self):
  12. if 'product' in self.config and self.config['product']:
  13. try:
  14. # 打开浏览器
  15. self.open()
  16. # 登录
  17. self.login()
  18. # 开始进入抢购下单核心程序
  19. self.core()
  20. # 关闭
  21. self.close()
  22. except BaseException as e:
  23. print e
  24. self.start()
  25. def core(self):
  26. # 开启多任务
  27. '''
  28. task = []
  29. for v in self.config['product']:
  30. task.append(gevent.spawn(self.buy, v))
  31. gevent.joinall(task)
  32. '''
  33. for v in self.config['product']:
  34. self.buy(v)
  35. def buy(self, config):
  36. product = Demeter.service('product')
  37. while 1:
  38. state = self.check(config['link'])
  39. if state == True:
  40. try:
  41. product.status(config['id'], 3)
  42. # 下单
  43. self.order(config['link'])
  44. # 支付并获取支付id
  45. order = product.createOrderId(self.pay(), config['site_id'], config['id'])
  46. # 支付截图
  47. pic = self.crop(order, config['id'])
  48. # 生成订单
  49. product.order(order, pic, config['site_id'], config['id'])
  50. product.status(config['id'], 4)
  51. break
  52. except BaseException as e:
  53. print e
  54. product.status(config['id'], 2)
  55. continue
  56. else:
  57. gevent.sleep(30)
  58. def check(self, product):
  59. #r = requests.get(product)
  60. self.get(product)
  61. data = self.driver.page_source
  62. # 缺货标识
  63. string = 'btn_soldout.gif'
  64. #data = r.text
  65. if string in data:
  66. return False
  67. else:
  68. return True
  69. def open(self):
  70. #self.display = Display(visible=0, size=(800, 600))
  71. #self.display.start()
  72. self.driver = webdriver.Remote(command_executor=Demeter.config['setting']['phantomjs'], desired_capabilities=DesiredCapabilities.PHANTOMJS)
  73. #options = webdriver.FirefoxOptions()
  74. #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"')
  75. #self.driver = webdriver.Chrome(firefox_options=options)
  76. def login(self):
  77. self.get(self.config['login_link'])
  78. #source_code = self.driver.page_source
  79. self.driver.find_element_by_id('login-id').send_keys(self.config['username'])
  80. self.driver.find_element_by_id('login-password').send_keys(self.config['password'])
  81. self.driver.find_element_by_xpath('//input[@alt="login"]').click()
  82. self.driver.implicitly_wait(5)
  83. def order(self, product):
  84. #self.get('http://cn.ssgdfm.com/shop/common/cookieEnable?ce=null&returnUrl=%2Fshop%2Fproduct%2FproductDetail%3FprdtCode%3D05710004801')
  85. self.get(product)
  86. # 立刻购买
  87. self.driver.execute_script('directOrderProduct()')
  88. # 等待加载页面完成
  89. self.wait('long-sub')
  90. # 等待弹层关闭
  91. self.waitNot('pay_popup')
  92. # 输入下单信息
  93. self.order_content()
  94. # 确认订单
  95. self.driver.execute_script('goPaymentCheck()')
  96. # 等待加载页面完成
  97. self.wait('order-agree2')
  98. def order_content(self):
  99. # 选择护照信息
  100. #self.driver.execute_script('setExitInfoHistory("' + self.config['chuguo'] + '")')
  101. #self.driver.execute_script('setExitInfoHistory("3239636")')
  102. self.driver.find_element_by_class_name('btn-popup-exitInfo').click()
  103. exit = self.driver.find_elements_by_class_name('btn-red')
  104. exitLength = len(exit)-1
  105. rand = random.randint(0, exitLength)
  106. exit[rand].click()
  107. # 使用优惠券
  108. # 使用积分
  109. self.driver.find_element_by_xpath('//input[@name="nowUse" and @value="0"]').click()
  110. self.driver.find_element_by_id('totalUse').click()
  111. def pay(self):
  112. # 等待弹层关闭
  113. self.waitNot('loding_popup')
  114. # 输入支付信息
  115. self.pay_content()
  116. # 确认按钮
  117. self.driver.execute_script('window.confirm = function(msg) { return true; }');
  118. # 同意付款
  119. self.driver.execute_script('$("a.goPayment").click()')
  120. #self.driver.switch_to_alert().accept()
  121. #self.wait('qrcode-img-wrapper')
  122. self.wait('qrcode')
  123. sleep(2)
  124. # 获取当前链接,没啥用
  125. #link = self.driver.execute_script('return location.href')
  126. # 获取订单号 这里获取不到,直接生成吧
  127. order = False
  128. return order
  129. def pay_content(self):
  130. # 确认取货处
  131. self.driver.find_element_by_id('exitDestInfo').click()
  132. # 同意收集个人信息
  133. self.driver.find_element_by_id('agree').click()
  134. # 同意订购
  135. self.driver.find_element_by_id('agree01').click()
  136. # 选择支付方式:支付宝21 微信16
  137. self.driver.find_element_by_xpath('//input[@id="pymntMeansCode" and @value="16"]').click()
  138. def crop(self, order, id):
  139. path = Demeter.path + 'runtime/upload/'
  140. if not File.exists(path):
  141. File.mkdir(path)
  142. pic = path + str(id) + '_' + str(order) + '.png'
  143. self.driver.save_screenshot(pic)
  144. return pic
  145. def waitNot(self, name):
  146. WebDriverWait(self.driver, 100).until_not(lambda x: x.find_element_by_class_name(name).is_displayed())
  147. def wait(self, name):
  148. WebDriverWait(self.driver, 100).until(lambda x: x.find_element_by_class_name(name).is_displayed())
  149. def get(self, link):
  150. self.driver.get(link)
  151. self.driver.implicitly_wait(10)
  152. def close(self):
  153. self.driver.quit()
  154. #self.driver.close()
  155. #self.display.stop()