| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import time
- import requests as req
- class BUFFAPI():
- def __init__(self, parent=None, title="buffapi接口", initial_data=None):
- super().__init__(parent)
- # 获取商品磨损信息
- @staticmethod
- def getPaintwearChoices(self_buff,goods_id):
- print(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
- print(f"暂停 {wait_time:.2f} seconds before making the next request.")
- time.sleep(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)
- print(f'获取商品id:{goods_id}磨损信息请求的返回报文: {api2_res.text.strip()[:50]}')
- data = api2_res.json()['data']
- # 如果data中的code不等于OK,则跳出循环
- if api2_res.json()['code'] != 'OK':
- print(f'获取商品id:{goods_id}磨损信息接口请求返回报文: {api2_res.text.strip()[:50]}')
- print(f'获取商品id:{goods_id}磨损信息接口请求返回报文code不等于OK,跳出循环')
- return
- self_buff.paintwear_choices = data['paintwear_choices']
- print(f"获取商品id:{goods_id}的磨损信息:", self_buff.paintwear_choices)
- except Exception as e:
- print(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 == '':
- print(f"获取商品id:{goods_id}的磨损不限区间的最低价格")
- else:
- print(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
- print(f"暂停 {wait_time:.2f} seconds before making the next request.")
- time.sleep(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 != '':
- print('min_paint_wear 最低磨损值 不为空')
- url += '&min_paintwear=' + str(min_paint_wear)
- # max_paint_wear 不为空
- if max_paint_wear != '':
- print('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 == '':
- print(f'获取商品id:{goods_id}磨损不限区间最低价格请求的返回报文: {api2_res.text.strip()[:50]}')
- else:
- print(f'获取商品id:{goods_id}磨损区间{min_paint_wear}-{max_paint_wear}最低价格请求的返回报文: {api2_res.text.strip()[:50]}')
- data = api2_res.json()['data']
- # 如果data中的code不等于OK,则跳出循环
- if api2_res.json()['code'] != 'OK':
- if min_paint_wear == '' and max_paint_wear == '':
- print(
- f'获取商品id:{goods_id}磨损不限区间最低价格请求返回报文: {api2_res.text.strip()[:50]}')
- print(
- f'获取商品id:{goods_id}磨损不限区间最低价格返回报文code不等于OK,跳出循环')
- else:
- print(
- f'获取商品id:{goods_id}磨损区间{min_paint_wear}--{max_paint_wear}最低价格请求返回报文: {api2_res.text.strip()[:50]}')
- print(
- f'获取商品id:{goods_id}磨损区间{min_paint_wear}--{max_paint_wear}最低价格返回报文code不等于OK,跳出循环')
- return
- min_price = data['items'][0]['price']
- except Exception as e:
- if min_paint_wear == '' and max_paint_wear == '':
- print(f'获取商品id:{goods_id}的磨损不限区间的最低价格请求--失败,{e}')
- else:
- print(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 == '':
- print(f"商品id:{goods_id}的磨损不限区间的最低价格:", min_price)
- else:
- print(f"商品id:{goods_id}的磨损区间{min_paint_wear}-{max_paint_wear}的最低价格:", min_price)
- return min_price
|