Пример #1
0
 def __init__(self, username=None, password=None):
     self.s = MySession()
     self.dm = Dama()
     self.username = username
     self.password = password
     self.uuid = ''
     self.msg = ''  # res
Пример #2
0
    def reg(self):
        """自动注册"""
        self.username = config['reg']['name_prx'] + ''.join(
            random.sample(string.digits, config['reg']['name_len']))
        self.password = ''.join(
            random.sample(string.letters +
                          string.digits, int(config['reg']['password']))
        ) if config['reg']['password'].isdigit() else config['reg']['password']
        self.s = MySession()

        try:
            r = self.s.get('https://reg.jd.com/reg/person')
            d = PyQuery(r.text)
            r = self.s.get('https://reg.jd.com/validate/isPinEngaged?pin=' +
                           self.username + '&r=' + str(time.time()))
        except:
            print "[error][reg]" + self.username
            return False
        code_url = d('.item-ifo img').attr('onclick')
        code_url = 'http:' + code_url[code_url.find("+") +
                                      2:code_url.find("'+")] + str(
                                          int(time.time()))
        try:
            vcode = self.dm.recv_byte(self.s.get(code_url).content)
            if not vcode:
                raise
        except:
            print "[info][reg]get vcode failed " + self.username
            return False

        last_input = d('input[type=hidden]')[-1]
        data = {
            'regType': 'person',
            'schoolid': '',
            'mobileCode': '',
            'uuid': d('input#uuid').val(),
            'regName': self.username,
            'pwd': self.password,
            'pwdRepeat': self.password,
            'authcode': vcode,
            last_input.name: last_input.value
        }
        r = self.s.post('https://reg.jd.com/reg/regService?r=' +
                        str(time.time()),
                        data=data)
        if r.content.find('info') > -1:
            return False
        else:
            confirm_url = r.content[r.content.find('http'):r.content.find('"}'
                                                                          )]
            r = self.s.get(confirm_url)
            with file('res/users.txt', 'a') as f:
                f.write(self.username + "---" + self.password + '\r\n')

            print "[success][reg]" + self.username
            self.__setSession()
            return True
Пример #3
0
	def __init__(self, username=None, password=None):
		self.s = MySession()
		self.dm = Dama()
		self.username = username
		self.password = password
		self.uuid = ''
		self.msg = ''		# res
Пример #4
0
	def reg(self):
		"""自动注册"""
		self.username = config['reg']['name_prx'] + ''.join(random.sample(string.digits, config['reg']['name_len']))
		self.password = ''.join(random.sample(string.letters + string.digits, int(config['reg']['password']))) if config['reg']['password'].isdigit() else config['reg']['password']
		self.s = MySession()

		try:
			r = self.s.get('https://reg.jd.com/reg/person')
			d = PyQuery(r.text)
			r = self.s.get('https://reg.jd.com/validate/isPinEngaged?pin=' + self.username + '&r=' + str(time.time()))
		except:
			print "[error][reg]" + self.username
			return False
		code_url = d('.item-ifo img').attr('onclick')
		code_url = 'http:' + code_url[code_url.find("+") + 2:code_url.find("'+")] + str(int(time.time()))
		try:
			vcode = self.dm.recv_byte(self.s.get(code_url).content)
			if not vcode:
				raise
		except:
			print "[info][reg]get vcode failed " + self.username
			return False

		last_input = d('input[type=hidden]')[-1]
		data = {
			'regType': 'person',
			'schoolid': '',
			'mobileCode': '',
			'uuid': d('input#uuid').val(),
			'regName': self.username,
			'pwd': self.password,
			'pwdRepeat': self.password,
			'authcode': vcode,
			last_input.name: last_input.value
		}
		r = self.s.post('https://reg.jd.com/reg/regService?r=' + str(time.time()), data=data)
		if r.content.find('info') > -1:
			return False
		else:
			confirm_url = r.content[r.content.find('http'):r.content.find('"}')]
			r = self.s.get(confirm_url)
			with file('res/users.txt', 'a') as f:
				f.write(self.username + "---" + self.password + '\r\n')

			print "[success][reg]" + self.username
			self.__setSession()
			return True
Пример #5
0
class JDongW():
	FLAG_DAMA = False
	event_start_code = threading.Event()
	event_start_buy = threading.Event()

	"""京东www站"""
	def __init__(self, username=None, password=None):
		self.s = MySession()
		self.dm = Dama()
		self.username = username
		self.password = password
		self.uuid = ''
		self.msg = ''		# res

	def __setSession(self):
		"""保存会话"""
		with file('session/www/' + self.username + '.session', 'w') as f:
			pickle.dump(self.s, f)
		return True

	def __getSession(self):
		"""恢复会话"""
		if not os.path.exists('session/www/' + self.username):
			return False

		with file('session/www/' + self.username + '.session') as f:
			self.s = pickle.load(f)

		try:
			r = self.s.get('http://order.jd.com/center/list.action')
		except:
			logging.debug('%s' % r)
			return False
		if r.url.find('list.action') < 0:
			return False
		else:
			return True

	def reg(self):
		"""自动注册"""
		self.username = config['reg']['name_prx'] + ''.join(random.sample(string.digits, config['reg']['name_len']))
		self.password = ''.join(random.sample(string.letters + string.digits, int(config['reg']['password']))) if config['reg']['password'].isdigit() else config['reg']['password']
		self.s = MySession()

		try:
			r = self.s.get('https://reg.jd.com/reg/person')
			d = PyQuery(r.text)
			r = self.s.get('https://reg.jd.com/validate/isPinEngaged?pin=' + self.username + '&r=' + str(time.time()))
		except:
			print "[error][reg]" + self.username
			return False
		code_url = d('.item-ifo img').attr('onclick')
		code_url = 'http:' + code_url[code_url.find("+") + 2:code_url.find("'+")] + str(int(time.time()))
		try:
			vcode = self.dm.recv_byte(self.s.get(code_url).content)
			if not vcode:
				raise
		except:
			print "[info][reg]get vcode failed " + self.username
			return False

		last_input = d('input[type=hidden]')[-1]
		data = {
			'regType': 'person',
			'schoolid': '',
			'mobileCode': '',
			'uuid': d('input#uuid').val(),
			'regName': self.username,
			'pwd': self.password,
			'pwdRepeat': self.password,
			'authcode': vcode,
			last_input.name: last_input.value
		}
		r = self.s.post('https://reg.jd.com/reg/regService?r=' + str(time.time()), data=data)
		if r.content.find('info') > -1:
			return False
		else:
			confirm_url = r.content[r.content.find('http'):r.content.find('"}')]
			r = self.s.get(confirm_url)
			with file('res/users.txt', 'a') as f:
				f.write(self.username + "---" + self.password + '\r\n')

			print "[success][reg]" + self.username
			self.__setSession()
			return True

	def login(self, retries=2):
		if not retries:
			return False
		if self.__getSession():
			print "[info][login] use session file"
			return True

		# 登录
		r = ''
		d = ''
		while True:
			try:
				r = self.s.get('http://passport.jd.com/uc/login?ltype=logout')
				d = PyQuery(r.text)
				if not d('#uuid').val():
					logging.debug(r.text)
					print '[error][login]loadpage'
					raise
				else:
					break
			except:
				time.sleep(2)
				continue

		inputs = d('input[type="hidden"]')
		self.uuid = d('#uuid').val()

		# 构建参数
		data = {
			'uuid': self.uuid,
			'loginname': self.username,
			'nloginpwd': self.password,
			'loginpwd': self.password,
			'authcode': '',
			'chkRememberMe': 'on',
			inputs[-1].name: inputs[-1].value,
		}
		vcode = ''
		if d('#o-authcode').attr('class').find('hide') < 0:
			code_url = d('#JD_Verification1').attr('src')
			if code_url.find('authcode.jd.com') < 0:
				print "[info][login] vcode url error"
				return False
			# 识别验证码
			while True:
				try:
					vcode = self.dm.recv_byte(self.s.get(code_url).content)
					if not vcode or len(vcode) != 4:
						raise
					else:
						break
				except:
					print "[info][login]vcode error " + self.username
					pass

			data['authcode'] = vcode

		time.sleep(2)
		r = self.s.post('http://passport.jd.com/uc/loginService?uuid=' + self.uuid + '&r=' + str(time.time()), data)
		if r.content.find('success') > 0:
			self.__setSession()
			return True
		else:
			print "[info][login] login failed " + self.username
			return False

	""" # @todo: 失效
Пример #6
0
class JDongW():
    FLAG_DAMA = False
    event_start_code = threading.Event()
    event_start_buy = threading.Event()
    """京东www站"""
    def __init__(self, username=None, password=None):
        self.s = MySession()
        self.dm = Dama()
        self.username = username
        self.password = password
        self.uuid = ''
        self.msg = ''  # res

    def __setSession(self):
        """保存会话"""
        with file('session/www/' + self.username + '.session', 'w') as f:
            pickle.dump(self.s, f)
        return True

    def __getSession(self):
        """恢复会话"""
        if not os.path.exists('session/www/' + self.username):
            return False

        with file('session/www/' + self.username + '.session') as f:
            self.s = pickle.load(f)

        try:
            r = self.s.get('http://order.jd.com/center/list.action')
        except:
            logging.debug('%s' % r)
            return False
        if r.url.find('list.action') < 0:
            return False
        else:
            return True

    def reg(self):
        """自动注册"""
        self.username = config['reg']['name_prx'] + ''.join(
            random.sample(string.digits, config['reg']['name_len']))
        self.password = ''.join(
            random.sample(string.letters +
                          string.digits, int(config['reg']['password']))
        ) if config['reg']['password'].isdigit() else config['reg']['password']
        self.s = MySession()

        try:
            r = self.s.get('https://reg.jd.com/reg/person')
            d = PyQuery(r.text)
            r = self.s.get('https://reg.jd.com/validate/isPinEngaged?pin=' +
                           self.username + '&r=' + str(time.time()))
        except:
            print "[error][reg]" + self.username
            return False
        code_url = d('.item-ifo img').attr('onclick')
        code_url = 'http:' + code_url[code_url.find("+") +
                                      2:code_url.find("'+")] + str(
                                          int(time.time()))
        try:
            vcode = self.dm.recv_byte(self.s.get(code_url).content)
            if not vcode:
                raise
        except:
            print "[info][reg]get vcode failed " + self.username
            return False

        last_input = d('input[type=hidden]')[-1]
        data = {
            'regType': 'person',
            'schoolid': '',
            'mobileCode': '',
            'uuid': d('input#uuid').val(),
            'regName': self.username,
            'pwd': self.password,
            'pwdRepeat': self.password,
            'authcode': vcode,
            last_input.name: last_input.value
        }
        r = self.s.post('https://reg.jd.com/reg/regService?r=' +
                        str(time.time()),
                        data=data)
        if r.content.find('info') > -1:
            return False
        else:
            confirm_url = r.content[r.content.find('http'):r.content.find('"}'
                                                                          )]
            r = self.s.get(confirm_url)
            with file('res/users.txt', 'a') as f:
                f.write(self.username + "---" + self.password + '\r\n')

            print "[success][reg]" + self.username
            self.__setSession()
            return True

    def login(self, retries=2):
        if not retries:
            return False
        if self.__getSession():
            print "[info][login] use session file"
            return True

        # 登录
        r = ''
        d = ''
        while True:
            try:
                r = self.s.get('http://passport.jd.com/uc/login?ltype=logout')
                d = PyQuery(r.text)
                if not d('#uuid').val():
                    logging.debug(r.text)
                    print '[error][login]loadpage'
                    raise
                else:
                    break
            except:
                time.sleep(2)
                continue

        inputs = d('input[type="hidden"]')
        self.uuid = d('#uuid').val()

        # 构建参数
        data = {
            'uuid': self.uuid,
            'loginname': self.username,
            'nloginpwd': self.password,
            'loginpwd': self.password,
            'authcode': '',
            'chkRememberMe': 'on',
            inputs[-1].name: inputs[-1].value,
        }
        vcode = ''
        if d('#o-authcode').attr('class').find('hide') < 0:
            code_url = d('#JD_Verification1').attr('src')
            if code_url.find('authcode.jd.com') < 0:
                print "[info][login] vcode url error"
                return False
            # 识别验证码
            while True:
                try:
                    vcode = self.dm.recv_byte(self.s.get(code_url).content)
                    if not vcode or len(vcode) != 4:
                        raise
                    else:
                        break
                except:
                    print "[info][login]vcode error " + self.username
                    pass

            data['authcode'] = vcode

        time.sleep(2)
        r = self.s.post(
            'http://passport.jd.com/uc/loginService?uuid=' + self.uuid +
            '&r=' + str(time.time()), data)
        if r.content.find('success') > 0:
            self.__setSession()
            return True
        else:
            print "[info][login] login failed " + self.username
            return False

    """ # @todo: 失效
Пример #7
0
	def login(self, retries=3):
		logging.info('login start')
		if not retries:
			return False

		# 尝试恢复会话
		if self.__getSession():
			logging.info(self.username + u' restore success')
			return True

		# 重新登录
		logging.info(self.username + u' try login')
		self.s = MySession()
		try:
			r = self.s.get('https://passport.m.jd.com/user/doLogin.action?v=t')
		except:
			return self.login(retries - 1)

		# 配置查询参数
		d = PyQuery(r.text)
		sid_all = d('form').attr('action')
		self.sid_all = sid_all[sid_all.find('sid'):]
		self.sid_val = self.sid_all[4:]
		data = {
			'returnurl': 'http://m.jd.com/user/home.action?' + self.sid_all,
			'username': self.username,
			'password': self.password,
			'remember': True
		}

		# 登录需要验证码
		vcode_flag = d('input#codeLevel').val()
		if vcode_flag:
			self.dm = Dama()
			vcode_url = d('.new-abtn-code #code').attr('src')
			vcode_val = ''
			while True:
				try:
					vcode_val = self.dm.recv_byte(self.s.get('https://passport.m.jd.com' + imgurl).content())
					if not vcode_val or len(vcode_val) != 4:
						raise
					else:
						break
				except:
					logging.debug('error vcode' + vcode_val)
					continue
			data['validateCode'] = vcode_val
			data['codeKey'] = self.sid_val,
			data['codeLevel'] = d('input#codeLevel').val()

		# 登录
		try:
			r = self.s.post('https://passport.m.jd.com/user/doLogin.action?' + self.sid_all, data)
			if r.url.find('home.action') < 0:
				if self.debug:
					print "[JDongM][login][9]login failed"
				raise
		except:
			logging.debug(r'login error, returned \n %s' % r.text)
			return self.login(retries - 1)

		self.__setSession()
		logging.info(self.username + ' login success')
		return True
Пример #8
0
class JDongM():
	"""jdong触屏版"""
	item = '1056969'

	def __init__(self, username, password, address=None):
		self.s = ''
		self.sid_val = ''			# 会话ID
		self.sid_all = ''			# 会话ID GET参数
		self.username = username
		self.password = password
		self.address = address
		self.address_id = ''		# 地址uuid
		self.date = time.strftime('%Y-%m-%d', time.localtime(time.time()))

	def __setSession(self):
		"""保存会话"""
		with file('session/wap/' + self.username + '.session', 'w') as f:
			pickle.dump(self.s, f)
		return True

	def __getSession(self):
		"""恢复会话"""
		if not os.path.exists('session/wap/' + self.username):
			return False

		with file('session/wap/' + self.username + '.session') as f:
			self.s = pickle.load(f)

		# 校验会话是否过期,若有效,设置sid
		r = self.s.get('https://passport.m.jd.com/user/home.action')
		if r.url.find('home.action') < 0:
			return False
		else:
			d = PyQuery(r.text)
			url = d('.new-a-message').attr('href')
			self.sid_all = url[url.find('sid='):]
			self.sid_val = self.sid_all[4:]
			return True

	def login(self, retries=3):
		logging.info('login start')
		if not retries:
			return False

		# 尝试恢复会话
		if self.__getSession():
			logging.info(self.username + u' restore success')
			return True

		# 重新登录
		logging.info(self.username + u' try login')
		self.s = MySession()
		try:
			r = self.s.get('https://passport.m.jd.com/user/doLogin.action?v=t')
		except:
			return self.login(retries - 1)

		# 配置查询参数
		d = PyQuery(r.text)
		sid_all = d('form').attr('action')
		self.sid_all = sid_all[sid_all.find('sid'):]
		self.sid_val = self.sid_all[4:]
		data = {
			'returnurl': 'http://m.jd.com/user/home.action?' + self.sid_all,
			'username': self.username,
			'password': self.password,
			'remember': True
		}

		# 登录需要验证码
		vcode_flag = d('input#codeLevel').val()
		if vcode_flag:
			self.dm = Dama()
			vcode_url = d('.new-abtn-code #code').attr('src')
			vcode_val = ''
			while True:
				try:
					vcode_val = self.dm.recv_byte(self.s.get('https://passport.m.jd.com' + imgurl).content())
					if not vcode_val or len(vcode_val) != 4:
						raise
					else:
						break
				except:
					logging.debug('error vcode' + vcode_val)
					continue
			data['validateCode'] = vcode_val
			data['codeKey'] = self.sid_val,
			data['codeLevel'] = d('input#codeLevel').val()

		# 登录
		try:
			r = self.s.post('https://passport.m.jd.com/user/doLogin.action?' + self.sid_all, data)
			if r.url.find('home.action') < 0:
				if self.debug:
					print "[JDongM][login][9]login failed"
				raise
		except:
			logging.debug(r'login error, returned \n %s' % r.text)
			return self.login(retries - 1)

		self.__setSession()
		logging.info(self.username + ' login success')
		return True

	def setAddress(self, retries=3):
		if not retries:
			return False

		# 删除已存在地址
		try:
			r = self.s.get('http://m.jd.com/address/addressList.action?' + self.sid_all)
			d = PyQuery(r.text)
			addressid = d('.new-addr-btn a:eq(1)').attr('addressid')
			if addressid:
				r = self.s.get('http://m.jd.com/address/doDelete.json?addressId=' + addressid)
				return self.setAddress(retries)
		except:
			logging.debug('del address error : %s' % (r.text))
			return self.setAddress(retries - 1)

		# 添加地址
		try:
			r = self.s.post('http://m.jd.com/address/doAdd.action', data=self.address)
			d = PyQuery(r.text)
			self.addressid = d('.new-addr-btn a:eq(1)').attr('addressid')
			if not self.addressid:
				raise
		except:
			logging.debug('not find addressid %s' % (r.text))
			return self.setAddress(retries - 1)

		return True

	def checkOrder(self, retries=3):
		if not retries:
			return False

		try:
			r = self.s.get('http://m.jd.com/user/unfinishedOrder.action?' + self.sid_all)
		except:
			return self.checkOrder(retries - 1)
		d = PyQuery(r.text)
		order_url = d('#div_orders a')
		if not order_url.size():
			return False
		# 有订单
		try:
			r = self.s.get('http://m.jd.com/' + order_url[0].attrib['href'])
		except:
			return self.checkOrder(retries - 1)

		try:
			d = PyQuery(r.text)
			order_id = d('input[type=hidden]')[0].value
			order_status = d('.new-txt-rd2:eq(0)').html().encode('utf-8')
			order_price = d('.new-mu_l2cw span:eq(1)').html().encode('utf-8')
			order_user = d('.new-order-details li:eq(2)').find('.new-txt').html().encode('utf-8')
			with file('res/' + str(self.date) + '.txt', 'a') as f:
				f.write(self.username + ' - ' + order_id + ' - ' + order_status + ' - ' + order_user + '\n')
		except:
			logging.debug('check_order error %s' % (r.text))
			pass

		return True

	def clearCart(self):
		"""清空购物车"""
		r = self.s.get('http://m.jd.com/cart/cart.action')
		d = PyQuery(r.text)
		items = d('.cart-list .common-input')
		if not items.size():
			return True
		for dom in items:
			item_id = d(dom).attr('id').replace('num', '')
			item_num = d(dom).attr('value')
			if not item_id or not item_num:
				return False
			self.s.get('http://m.jd.com/cart/remove.action?wareId=' + item_id + '&num=' + item_num + '&ran=' + str(time.time()))

		return True

	def checkCart(self):
		"""检查购物车是否有商品, 购买前检测"""
		try:
			r = self.s.get('http://m.jd.com/cart/cart.action')
		except:
			return self.checkCart()
		d = PyQuery(r.text)
		items = d('.cart-list .common-input')
		if not items.size():
			return False
		else:
			return True

	def add2cart(self):
		"""添加到购物车"""
		try:
			url = 'http://m.jd.com/cart/add.json?wareId=' + self.item + '&' + self.sid_all
			r = self.s.get(url)
			return True
		except:
			return False

	def setOrder(self):
		"""设置订单信息"""
		try:
			r = self.s.get('http://m.jd.com/order/order.action?enterOrder=true')
			r = self.s.get('http://m.jd.com/order/address.action?' + self.sid_all)
			r = self.s.post('http://m.jd.com/order/updateOrderAddressTouch.action?' + self.sid_all, data={'vtuanOrder': 'false', 'addressId': self.addressid})
			r = self.s.get('http://m.jd.com/order/paytype.action?' + self.sid_all)
			r = self.s.post('http://m.jd.com/order/updatePaytype.action?' + self.sid_all, data={'idPaymentType': '1'})
			r = self.s.get('http://m.jd.com/order/shipment.action?' + self.sid_all)
			data = {
				'order.idShipmentType': '65',
				'order.codTime': '3',
				'order.isCodInform': 'false',
				'order.paymentWay': '1'
			}
			r = self.s.post('http://m.jd.com/order/updateShipment.action?' + self.sid_all, data=data)
			if r.text.find('images/html5/t_confirm.png') > 0:
				raise
		except:
			return False

		return True

	""" 旧,待更新
Пример #9
0
    def login(self, retries=3):
        logging.info('login start')
        if not retries:
            return False

        # 尝试恢复会话
        if self.__getSession():
            logging.info(self.username + u' restore success')
            return True

        # 重新登录
        logging.info(self.username + u' try login')
        self.s = MySession()
        try:
            r = self.s.get('https://passport.m.jd.com/user/doLogin.action?v=t')
        except:
            return self.login(retries - 1)

        # 配置查询参数
        d = PyQuery(r.text)
        sid_all = d('form').attr('action')
        self.sid_all = sid_all[sid_all.find('sid'):]
        self.sid_val = self.sid_all[4:]
        data = {
            'returnurl': 'http://m.jd.com/user/home.action?' + self.sid_all,
            'username': self.username,
            'password': self.password,
            'remember': True
        }

        # 登录需要验证码
        vcode_flag = d('input#codeLevel').val()
        if vcode_flag:
            self.dm = Dama()
            vcode_url = d('.new-abtn-code #code').attr('src')
            vcode_val = ''
            while True:
                try:
                    vcode_val = self.dm.recv_byte(
                        self.s.get('https://passport.m.jd.com' +
                                   imgurl).content())
                    if not vcode_val or len(vcode_val) != 4:
                        raise
                    else:
                        break
                except:
                    logging.debug('error vcode' + vcode_val)
                    continue
            data['validateCode'] = vcode_val
            data['codeKey'] = self.sid_val,
            data['codeLevel'] = d('input#codeLevel').val()

        # 登录
        try:
            r = self.s.post(
                'https://passport.m.jd.com/user/doLogin.action?' +
                self.sid_all, data)
            if r.url.find('home.action') < 0:
                if self.debug:
                    print "[JDongM][login][9]login failed"
                raise
        except:
            logging.debug(r'login error, returned \n %s' % r.text)
            return self.login(retries - 1)

        self.__setSession()
        logging.info(self.username + ' login success')
        return True
Пример #10
0
class JDongM():
    """jdong触屏版"""
    item = '1056969'

    def __init__(self, username, password, address=None):
        self.s = ''
        self.sid_val = ''  # 会话ID
        self.sid_all = ''  # 会话ID GET参数
        self.username = username
        self.password = password
        self.address = address
        self.address_id = ''  # 地址uuid
        self.date = time.strftime('%Y-%m-%d', time.localtime(time.time()))

    def __setSession(self):
        """保存会话"""
        with file('session/wap/' + self.username + '.session', 'w') as f:
            pickle.dump(self.s, f)
        return True

    def __getSession(self):
        """恢复会话"""
        if not os.path.exists('session/wap/' + self.username):
            return False

        with file('session/wap/' + self.username + '.session') as f:
            self.s = pickle.load(f)

        # 校验会话是否过期,若有效,设置sid
        r = self.s.get('https://passport.m.jd.com/user/home.action')
        if r.url.find('home.action') < 0:
            return False
        else:
            d = PyQuery(r.text)
            url = d('.new-a-message').attr('href')
            self.sid_all = url[url.find('sid='):]
            self.sid_val = self.sid_all[4:]
            return True

    def login(self, retries=3):
        logging.info('login start')
        if not retries:
            return False

        # 尝试恢复会话
        if self.__getSession():
            logging.info(self.username + u' restore success')
            return True

        # 重新登录
        logging.info(self.username + u' try login')
        self.s = MySession()
        try:
            r = self.s.get('https://passport.m.jd.com/user/doLogin.action?v=t')
        except:
            return self.login(retries - 1)

        # 配置查询参数
        d = PyQuery(r.text)
        sid_all = d('form').attr('action')
        self.sid_all = sid_all[sid_all.find('sid'):]
        self.sid_val = self.sid_all[4:]
        data = {
            'returnurl': 'http://m.jd.com/user/home.action?' + self.sid_all,
            'username': self.username,
            'password': self.password,
            'remember': True
        }

        # 登录需要验证码
        vcode_flag = d('input#codeLevel').val()
        if vcode_flag:
            self.dm = Dama()
            vcode_url = d('.new-abtn-code #code').attr('src')
            vcode_val = ''
            while True:
                try:
                    vcode_val = self.dm.recv_byte(
                        self.s.get('https://passport.m.jd.com' +
                                   imgurl).content())
                    if not vcode_val or len(vcode_val) != 4:
                        raise
                    else:
                        break
                except:
                    logging.debug('error vcode' + vcode_val)
                    continue
            data['validateCode'] = vcode_val
            data['codeKey'] = self.sid_val,
            data['codeLevel'] = d('input#codeLevel').val()

        # 登录
        try:
            r = self.s.post(
                'https://passport.m.jd.com/user/doLogin.action?' +
                self.sid_all, data)
            if r.url.find('home.action') < 0:
                if self.debug:
                    print "[JDongM][login][9]login failed"
                raise
        except:
            logging.debug(r'login error, returned \n %s' % r.text)
            return self.login(retries - 1)

        self.__setSession()
        logging.info(self.username + ' login success')
        return True

    def setAddress(self, retries=3):
        if not retries:
            return False

        # 删除已存在地址
        try:
            r = self.s.get('http://m.jd.com/address/addressList.action?' +
                           self.sid_all)
            d = PyQuery(r.text)
            addressid = d('.new-addr-btn a:eq(1)').attr('addressid')
            if addressid:
                r = self.s.get(
                    'http://m.jd.com/address/doDelete.json?addressId=' +
                    addressid)
                return self.setAddress(retries)
        except:
            logging.debug('del address error : %s' % (r.text))
            return self.setAddress(retries - 1)

        # 添加地址
        try:
            r = self.s.post('http://m.jd.com/address/doAdd.action',
                            data=self.address)
            d = PyQuery(r.text)
            self.addressid = d('.new-addr-btn a:eq(1)').attr('addressid')
            if not self.addressid:
                raise
        except:
            logging.debug('not find addressid %s' % (r.text))
            return self.setAddress(retries - 1)

        return True

    def checkOrder(self, retries=3):
        if not retries:
            return False

        try:
            r = self.s.get('http://m.jd.com/user/unfinishedOrder.action?' +
                           self.sid_all)
        except:
            return self.checkOrder(retries - 1)
        d = PyQuery(r.text)
        order_url = d('#div_orders a')
        if not order_url.size():
            return False
        # 有订单
        try:
            r = self.s.get('http://m.jd.com/' + order_url[0].attrib['href'])
        except:
            return self.checkOrder(retries - 1)

        try:
            d = PyQuery(r.text)
            order_id = d('input[type=hidden]')[0].value
            order_status = d('.new-txt-rd2:eq(0)').html().encode('utf-8')
            order_price = d('.new-mu_l2cw span:eq(1)').html().encode('utf-8')
            order_user = d('.new-order-details li:eq(2)').find(
                '.new-txt').html().encode('utf-8')
            with file('res/' + str(self.date) + '.txt', 'a') as f:
                f.write(self.username + ' - ' + order_id + ' - ' +
                        order_status + ' - ' + order_user + '\n')
        except:
            logging.debug('check_order error %s' % (r.text))
            pass

        return True

    def clearCart(self):
        """清空购物车"""
        r = self.s.get('http://m.jd.com/cart/cart.action')
        d = PyQuery(r.text)
        items = d('.cart-list .common-input')
        if not items.size():
            return True
        for dom in items:
            item_id = d(dom).attr('id').replace('num', '')
            item_num = d(dom).attr('value')
            if not item_id or not item_num:
                return False
            self.s.get('http://m.jd.com/cart/remove.action?wareId=' + item_id +
                       '&num=' + item_num + '&ran=' + str(time.time()))

        return True

    def checkCart(self):
        """检查购物车是否有商品, 购买前检测"""
        try:
            r = self.s.get('http://m.jd.com/cart/cart.action')
        except:
            return self.checkCart()
        d = PyQuery(r.text)
        items = d('.cart-list .common-input')
        if not items.size():
            return False
        else:
            return True

    def add2cart(self):
        """添加到购物车"""
        try:
            url = 'http://m.jd.com/cart/add.json?wareId=' + self.item + '&' + self.sid_all
            r = self.s.get(url)
            return True
        except:
            return False

    def setOrder(self):
        """设置订单信息"""
        try:
            r = self.s.get(
                'http://m.jd.com/order/order.action?enterOrder=true')
            r = self.s.get('http://m.jd.com/order/address.action?' +
                           self.sid_all)
            r = self.s.post(
                'http://m.jd.com/order/updateOrderAddressTouch.action?' +
                self.sid_all,
                data={
                    'vtuanOrder': 'false',
                    'addressId': self.addressid
                })
            r = self.s.get('http://m.jd.com/order/paytype.action?' +
                           self.sid_all)
            r = self.s.post('http://m.jd.com/order/updatePaytype.action?' +
                            self.sid_all,
                            data={'idPaymentType': '1'})
            r = self.s.get('http://m.jd.com/order/shipment.action?' +
                           self.sid_all)
            data = {
                'order.idShipmentType': '65',
                'order.codTime': '3',
                'order.isCodInform': 'false',
                'order.paymentWay': '1'
            }
            r = self.s.post('http://m.jd.com/order/updateShipment.action?' +
                            self.sid_all,
                            data=data)
            if r.text.find('images/html5/t_confirm.png') > 0:
                raise
        except:
            return False

        return True

    """ 旧,待更新