BUFFAPI.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import time
  2. import requests as req
  3. class BUFFAPI():
  4. def __init__(self, parent=None, title="buffapi接口", initial_data=None):
  5. super().__init__(parent)
  6. # 获取商品磨损信息
  7. @staticmethod
  8. def getPaintwearChoices(self_buff,goods_id):
  9. print(f"获取商品id:{goods_id}的磨损信息")
  10. self_buff.paintwear_choices = []
  11. current_time = time.time()
  12. time_since_last_request = current_time - self_buff.last_paintwear_request_time
  13. if time_since_last_request < float(self_buff.pause_time):
  14. wait_time = float(self_buff.pause_time) - time_since_last_request
  15. print(f"暂停 {wait_time:.2f} seconds before making the next request.")
  16. time.sleep(wait_time)
  17. self_buff.xp_my_headers.update({
  18. 'Cookie': self_buff.cookie_text
  19. })
  20. try:
  21. api2_res = req.get(url=self_buff.xp_getPaintwearChoicesUrl + str(goods_id), headers=self_buff.xp_my_headers)
  22. print(f'获取商品id:{goods_id}磨损信息请求的返回报文: {api2_res.text.strip()[:50]}')
  23. data = api2_res.json()['data']
  24. # 如果data中的code不等于OK,则跳出循环
  25. if api2_res.json()['code'] != 'OK':
  26. print(f'获取商品id:{goods_id}磨损信息接口请求返回报文: {api2_res.text.strip()[:50]}')
  27. print(f'获取商品id:{goods_id}磨损信息接口请求返回报文code不等于OK,跳出循环')
  28. return
  29. self_buff.paintwear_choices = data['paintwear_choices']
  30. print(f"获取商品id:{goods_id}的磨损信息:", self_buff.paintwear_choices)
  31. except Exception as e:
  32. print(f'获取商品id:{goods_id}的磨损信息接口请求--失败', e)
  33. finally:
  34. self_buff.last_paintwear_request_time = time.time() # 更新上次请求时间为当前时间
  35. # 获取磨损区间最低价格信息
  36. @staticmethod
  37. def getPaintwearChoicesMinPrice(self, goods_id, min_paint_wear, max_paint_wear):
  38. # min_paint_wear 为空 打印语句写成不限区间
  39. if min_paint_wear == '' and max_paint_wear == '':
  40. print(f"获取商品id:{goods_id}的磨损不限区间的最低价格")
  41. else:
  42. print(f"获取商品id:{goods_id}的磨损区间{min_paint_wear}--{max_paint_wear}的最低价格")
  43. # 开始时间为当前时间
  44. min_price = 0
  45. current_time = time.time()
  46. time_since_last_request = current_time - self.last_minprice_request_time
  47. if time_since_last_request < float(self.pause_time):
  48. wait_time = float(self.pause_time) - time_since_last_request
  49. print(f"暂停 {wait_time:.2f} seconds before making the next request.")
  50. time.sleep(wait_time)
  51. self.xp_my_headers.update({
  52. 'Cookie': self.cookie_text
  53. })
  54. url = self.xp_getPriceUrl + str(goods_id)
  55. # min_paint_wear 不为空
  56. if min_paint_wear != '':
  57. print('min_paint_wear 最低磨损值 不为空')
  58. url += '&min_paintwear=' + str(min_paint_wear)
  59. # max_paint_wear 不为空
  60. if max_paint_wear != '':
  61. print('max_paint_wear 最高磨损值 不为空')
  62. url += '&max_paintwear=' + str(max_paint_wear)
  63. try:
  64. api2_res = req.get(url=url, headers=self.xp_my_headers)
  65. if min_paint_wear == '' and max_paint_wear == '':
  66. print(f'获取商品id:{goods_id}磨损不限区间最低价格请求的返回报文: {api2_res.text.strip()[:50]}')
  67. else:
  68. print(f'获取商品id:{goods_id}磨损区间{min_paint_wear}-{max_paint_wear}最低价格请求的返回报文: {api2_res.text.strip()[:50]}')
  69. data = api2_res.json()['data']
  70. # 如果data中的code不等于OK,则跳出循环
  71. if api2_res.json()['code'] != 'OK':
  72. if min_paint_wear == '' and max_paint_wear == '':
  73. print(
  74. f'获取商品id:{goods_id}磨损不限区间最低价格请求返回报文: {api2_res.text.strip()[:50]}')
  75. print(
  76. f'获取商品id:{goods_id}磨损不限区间最低价格返回报文code不等于OK,跳出循环')
  77. else:
  78. print(
  79. f'获取商品id:{goods_id}磨损区间{min_paint_wear}--{max_paint_wear}最低价格请求返回报文: {api2_res.text.strip()[:50]}')
  80. print(
  81. f'获取商品id:{goods_id}磨损区间{min_paint_wear}--{max_paint_wear}最低价格返回报文code不等于OK,跳出循环')
  82. return
  83. min_price = data['items'][0]['price']
  84. except Exception as e:
  85. if min_paint_wear == '' and max_paint_wear == '':
  86. print(f'获取商品id:{goods_id}的磨损不限区间的最低价格请求--失败,{e}')
  87. else:
  88. print(f'获取商品id:{goods_id}的磨损区间{min_paint_wear}-{max_paint_wear}的最低价格请求--失败,{e}')
  89. finally:
  90. self.last_minprice_request_time = time.time() # 更新上次请求时间为当前时间
  91. if min_paint_wear == '' and max_paint_wear == '':
  92. print(f"商品id:{goods_id}的磨损不限区间的最低价格:", min_price)
  93. else:
  94. print(f"商品id:{goods_id}的磨损区间{min_paint_wear}-{max_paint_wear}的最低价格:", min_price)
  95. return min_price