ssgdfs.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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, 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, 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. # 缺货标识
  61. string = 'btn_soldout.gif'
  62. data = r.text
  63. if string in data:
  64. return False
  65. else:
  66. return True
  67. def open(self):
  68. #self.display = Display(visible=0, size=(800, 600))
  69. #self.display.start()
  70. self.driver = webdriver.Remote(command_executor=Demeter.config['setting']['phantomjs'], desired_capabilities=DesiredCapabilities.PHANTOMJS)
  71. #options = webdriver.FirefoxOptions()
  72. #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"')
  73. #self.driver = webdriver.Chrome(firefox_options=options)
  74. def login(self):
  75. self.get(self.config['login_link'])
  76. #source_code = self.driver.page_source
  77. self.driver.find_element_by_id('login-id').send_keys(self.config['username'])
  78. self.driver.find_element_by_id('login-password').send_keys(self.config['password'])
  79. self.driver.find_element_by_xpath('//input[@alt="login"]').click()
  80. self.driver.implicitly_wait(5)
  81. def order(self, product):
  82. #self.get('http://cn.ssgdfm.com/shop/common/cookieEnable?ce=null&returnUrl=%2Fshop%2Fproduct%2FproductDetail%3FprdtCode%3D05710004801')
  83. self.get(product)
  84. # 立刻购买
  85. self.driver.execute_script('directOrderProduct()')
  86. # 等待加载页面完成
  87. self.wait('long-sub')
  88. # 等待弹层关闭
  89. self.waitNot('pay_popup')
  90. # 输入下单信息
  91. self.order_content()
  92. # 确认订单
  93. self.driver.execute_script('goPaymentCheck()')
  94. # 等待加载页面完成
  95. self.wait('order-agree2')
  96. def order_content(self):
  97. # 选择护照信息
  98. #self.driver.execute_script('setExitInfoHistory("' + self.config['chuguo'] + '")')
  99. #self.driver.execute_script('setExitInfoHistory("3239636")')
  100. self.driver.find_element_by_class_name('btn-popup-exitInfo').click()
  101. exit = self.driver.find_elements_by_class_name('btn-red')
  102. exitLength = len(exit)-1
  103. rand = random.randint(0, exitLength)
  104. exit[rand].click()
  105. # 使用优惠券
  106. # 使用积分
  107. self.driver.find_element_by_xpath('//input[@name="nowUse" and @value="0"]').click()
  108. def pay(self):
  109. # 等待弹层关闭
  110. self.waitNot('loding_popup')
  111. # 输入支付信息
  112. self.pay_content()
  113. # 确认按钮
  114. self.driver.execute_script('window.confirm = function(msg) { return true; }');
  115. # 同意付款
  116. self.driver.execute_script('$("a.goPayment").click()')
  117. #self.driver.switch_to_alert().accept()
  118. #self.wait('qrcode-img-wrapper')
  119. self.wait('qrcode')
  120. sleep(2)
  121. # 获取当前链接,没啥用
  122. #link = self.driver.execute_script('return location.href')
  123. # 获取订单号 这里获取不到,直接生成吧
  124. order = False
  125. return order
  126. def pay_content(self):
  127. # 确认取货处
  128. self.driver.find_element_by_id('exitDestInfo').click()
  129. # 同意收集个人信息
  130. self.driver.find_element_by_id('agree').click()
  131. # 同意订购
  132. self.driver.find_element_by_id('agree01').click()
  133. # 选择支付方式:支付宝21 微信16
  134. self.driver.find_element_by_xpath('//input[@id="pymntMeansCode" and @value="16"]').click()
  135. def crop(self, order, id):
  136. path = Demeter.path + 'runtime/upload/'
  137. if not File.exists(path):
  138. File.mkdir(path)
  139. pic = path + str(id) + '_' + str(order) + '.png'
  140. self.driver.save_screenshot(pic)
  141. return pic
  142. def waitNot(self, name):
  143. WebDriverWait(self.driver, 100).until_not(lambda x: x.find_element_by_class_name(name).is_displayed())
  144. def wait(self, name):
  145. WebDriverWait(self.driver, 100).until(lambda x: x.find_element_by_class_name(name).is_displayed())
  146. def get(self, link):
  147. self.driver.get(link)
  148. self.driver.implicitly_wait(10)
  149. def close(self):
  150. self.driver.quit()
  151. #self.driver.close()
  152. #self.display.stop()