ssgdfs.py 6.3 KB

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