| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import time
- import requests as req
- import logging
- logging.basicConfig(level=logging.DEBUG, format='%(asctime)s: %(message)s')
- logger = logging.getLogger(__name__)
- class BUFFAPI():
- def __init__(self, parent=None, title="buffapi接口", initial_data=None):
- super().__init__(parent)
- # 获取商品磨损信息
- @staticmethod
- def getPaintwearChoices(self_buff,goods_id):
- self_buff.detail_logger.debug(f"获取商品id:{goods_id}的磨损信息")
- self_buff.paintwear_choices = []
- current_time = time.time()
- time_since_last_request = current_time - self_buff.last_paintwear_request_time
- if time_since_last_request < float(self_buff.pause_time):
- wait_time = float(self_buff.pause_time) - time_since_last_request
- self_buff.detail_logger.debug(f"暂停 {wait_time:.2f} seconds before making the next request.")
- time.sleep(float(wait_time))
- self_buff.xp_my_headers.update({
- 'Cookie': self_buff.cookie_text
- })
- try:
- api2_res = req.get(url=self_buff.xp_getPaintwearChoicesUrl + str(goods_id), headers=self_buff.xp_my_headers)
- self_buff.detail_logger.debug(f'获取商品id:{goods_id}磨损信息请求的返回报文: {api2_res.text.strip()[:50]}')
- # data = api2_res.json()['data']
- if api2_res.json()['code'] == 'Captcha Validate Required':
- self_buff.detail_logger.debug('选品获取磨损信息接口请求返回报文code为Captcha Validate Required,暂停30秒后重试')
- retry_count = 0
- while retry_count < int(self_buff.needed_msg_check_try_times):
- self_buff.detail_logger.debug(f'选品获取磨损信息接口第{retry_count+1}次重试')
- time.sleep(float(self_buff.needed_msg_check_pause_time))
- api2_res = req.get(url=self_buff.xp_getPaintwearChoicesUrl + str(goods_id), headers=self_buff.xp_my_headers)
- # data = api2_res.json()['data']
- if api2_res.json()['code'] == 'System Error':
- continue
- if api2_res.json()['code'] == 'OK':
- break
- if api2_res.json()['code'] == 'Captcha Validate Required':
- retry_count += 1
- if api2_res.json()['code'] == 'Captcha Validate Required':
- self_buff.detail_logger.debug('选品获取磨损信息接口请求返回报文code仍然为Captcha Validate Required,跳出循环')
- return 'stop_flag'
- elif api2_res.json()['code'] != 'OK':
- # 如果data中的code不等于OK,则跳出循环
- self_buff.detail_logger.debug(f'获取商品id:{goods_id}磨损信息接口请求返回报文: {api2_res.text.strip()[:50]}')
- self_buff.detail_logger.debug(f'获取商品id:{goods_id}磨损信息接口请求返回报文code不等于OK,跳出循环')
- return
- data = api2_res.json()['data']
- self_buff.paintwear_choices = data['paintwear_choices']
- self_buff.detail_logger.debug(f"获取商品id:{goods_id}的磨损信息: {self_buff.paintwear_choices}" )
- except Exception as e:
- self_buff.detail_logger.debug(f'获取商品id:{goods_id}的磨损信息接口请求--失败:{e}')
- finally:
- self_buff.last_paintwear_request_time = time.time() # 更新上次请求时间为当前时间
- # 获取磨损区间最低价格信息
- @staticmethod
- def getPaintwearChoicesMinPrice(self, goods_id, min_paint_wear, max_paint_wear):
- # min_paint_wear 为空 打印语句写成不限区间
- if min_paint_wear == '' and max_paint_wear == '':
- self.detail_logger.debug(f"获取商品id:{goods_id}的磨损不限区间的最低价格")
- else:
- self.detail_logger.debug(f"获取商品id:{goods_id}的磨损区间{min_paint_wear}--{max_paint_wear}的最低价格")
- # 开始时间为当前时间
- min_price = 0
- current_time = time.time()
- time_since_last_request = current_time - self.last_minprice_request_time
- if time_since_last_request < float(self.pause_time):
- wait_time = float(self.pause_time) - time_since_last_request
- self.detail_logger.debug(f"暂停 {wait_time:.2f} seconds before making the next request.")
- time.sleep(float(wait_time))
- self.xp_my_headers.update({
- 'Cookie': self.cookie_text
- })
- url = self.xp_getPriceUrl + str(goods_id)
- # min_paint_wear 不为空
- if min_paint_wear != '':
- self.detail_logger.debug('min_paint_wear 最低磨损值 不为空')
- url += '&min_paintwear=' + str(min_paint_wear)
- # max_paint_wear 不为空
- if max_paint_wear != '':
- self.detail_logger.debug('max_paint_wear 最高磨损值 不为空')
- url += '&max_paintwear=' + str(max_paint_wear)
- try:
- api2_res = req.get(url=url, headers=self.xp_my_headers)
- if min_paint_wear == '' and max_paint_wear == '':
- self.detail_logger.debug(f'获取商品id:{goods_id}磨损不限区间最低价格请求的返回报文: {api2_res.text.strip()[:50]}')
- else:
- self.detail_logger.debug(f'获取商品id:{goods_id}磨损区间{min_paint_wear}-{max_paint_wear}最低价格请求的返回报文: {api2_res.text.strip()[:50]}')
- self.detail_logger.debug(f"api2_res.json()['code']={api2_res.json()['code']}")
- if api2_res.json()['code'] == 'Captcha Validate Required':
- self.detail_logger.debug(f'选品获取磨损区间最低价格接口请求返回报文code为Captcha Validate Required,暂停{self.needed_msg_check_pause_time}秒后重试')
- retry_count = 0
- while retry_count < int(self.needed_msg_check_try_times):
- self.detail_logger.debug(f'选品获取磨损区间最低价格接口请求接口第{retry_count+1}次重试')
- time.sleep(float(self.needed_msg_check_pause_time))
- api2_res = req.get(url=url, headers=self.xp_my_headers)
- # data = api2_res.json()['data']
- if api2_res.json()['code'] == 'System Error':
- continue
- if api2_res.json()['code'] == 'OK':
- break
- if api2_res.json()['code'] == 'Captcha Validate Required':
- retry_count += 1
- if api2_res.json()['code'] == 'Captcha Validate Required':
- self.detail_logger.debug('选品获取磨损区间最低价格接口请求返回报文code仍然为Captcha Validate Required,跳出循环')
- min_price= 'stop_flag'
- return
- # 如果data中的code不等于OK,则跳出循环
- if api2_res.json()['code'] != 'OK':
- if min_paint_wear == '' and max_paint_wear == '':
- self.detail_logger.debug(
- f'获取商品id:{goods_id}磨损不限区间最低价格请求返回报文: {api2_res.text.strip()[:50]}')
- self.detail_logger.debug(
- f'获取商品id:{goods_id}磨损不限区间最低价格返回报文code不等于OK,跳出循环')
- else:
- self.detail_logger.debug(
- f'获取商品id:{goods_id}磨损区间{min_paint_wear}--{max_paint_wear}最低价格请求返回报文: {api2_res.text.strip()[:50]}')
- self.detail_logger.debug(
- f'获取商品id:{goods_id}磨损区间{min_paint_wear}--{max_paint_wear}最低价格返回报文code不等于OK,跳出循环')
- return
- data = api2_res.json()['data']
- min_price = data['items'][0]['price']
- except Exception as e:
- if min_paint_wear == '' and max_paint_wear == '':
- self.detail_logger.debug(f'获取商品id:{goods_id}的磨损不限区间的最低价格请求--失败: {e}')
- else:
- self.detail_logger.debug(f'获取商品id:{goods_id}的磨损区间{min_paint_wear}-{max_paint_wear}的最低价格请求--失败: {e}')
- finally:
- self.last_minprice_request_time = time.time() # 更新上次请求时间为当前时间
- if min_paint_wear == '' and max_paint_wear == '':
- self.detail_logger.debug(f"商品id:{goods_id}的磨损不限区间的最低价格:{min_price}")
- else:
- self.detail_logger.debug(f"商品id:{goods_id}的磨损区间{min_paint_wear}-{max_paint_wear}的最低价格:{min_price}" )
- return min_price
|