ssgdfs.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 = 'http://image2.ssgdfs.com/images/shop/cn/renewal/content/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.driver = webdriver.Remote(command_executor=Demeter.config['setting']['phantomjs'], desired_capabilities=DesiredCapabilities.PHANTOMJS)
  69. def login(self):
  70. self.driver.get(self.config['login_link'])
  71. self.driver.find_element_by_id('login-id').send_keys(self.config['username'])
  72. self.driver.find_element_by_id('login-password').send_keys(self.config['password'])
  73. self.driver.find_element_by_xpath('//input[@alt="login"]').click()
  74. self.driver.implicitly_wait(5)
  75. def order(self, product):
  76. self.driver.get(product)
  77. # 立刻购买
  78. self.driver.execute_script('directOrderProduct()')
  79. # 等待加载页面完成
  80. self.wait('long-sub')
  81. # 等待弹层关闭
  82. self.waitNot('pay_popup')
  83. # 输入下单信息
  84. self.order_content()
  85. # 确认订单
  86. self.driver.execute_script('goPaymentCheck()')
  87. # 等待加载页面完成
  88. self.wait('order-agree2')
  89. def order_content(self):
  90. # 选择护照信息
  91. #self.driver.execute_script('setExitInfoHistory("' + self.config['chuguo'] + '")')
  92. #self.driver.execute_script('setExitInfoHistory("3239636")')
  93. self.driver.find_element_by_class_name('btn-popup-exitInfo').click()
  94. exit = self.driver.find_elements_by_class_name('btn-red')
  95. exitLength = len(exit)-1
  96. rand = random.randint(0, exitLength)
  97. exit[rand].click()
  98. # 使用优惠券
  99. # 使用积分
  100. self.driver.find_element_by_xpath('//input[@name="nowUse" and @value="0"]').click()
  101. def pay(self):
  102. # 等待弹层关闭
  103. self.waitNot('loding_popup')
  104. # 输入支付信息
  105. self.pay_content()
  106. # 确认按钮
  107. self.driver.execute_script('window.confirm = function(msg) { return true; }');
  108. # 同意付款
  109. self.driver.execute_script('$("a.goPayment").click()')
  110. #self.driver.switch_to_alert().accept()
  111. #self.wait('qrcode-img-wrapper')
  112. self.wait('qrcode')
  113. sleep(2)
  114. # 获取当前链接,没啥用
  115. #link = self.driver.execute_script('return location.href')
  116. # 获取订单号 这里获取不到,直接生成吧
  117. order = False
  118. return order
  119. def pay_content(self):
  120. # 确认取货处
  121. self.driver.find_element_by_id('exitDestInfo').click()
  122. # 同意收集个人信息
  123. self.driver.find_element_by_id('agree').click()
  124. # 同意订购
  125. self.driver.find_element_by_id('agree01').click()
  126. # 选择支付方式:支付宝21 微信00
  127. self.driver.find_element_by_xpath('//input[@id="pymntMeansCode" and @value="00"]').click()
  128. def crop(self, order, id):
  129. path = Demeter.path + 'runtime/upload/'
  130. if not File.exists(path):
  131. File.mkdir(path)
  132. pic = path + str(id) + '_' + str(order) + '.png'
  133. self.driver.save_screenshot(pic)
  134. return pic
  135. def waitNot(self, name):
  136. WebDriverWait(self.driver, 100).until_not(lambda x: x.find_element_by_class_name(name).is_displayed())
  137. def wait(self, name):
  138. WebDriverWait(self.driver, 100).until(lambda x: x.find_element_by_class_name(name).is_displayed())
  139. def close(self):
  140. self.driver.close()