BUFFAPI.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import time
  2. import requests as req
  3. import logging
  4. logging.basicConfig(level=logging.DEBUG, format='%(asctime)s: %(message)s')
  5. logger = logging.getLogger(__name__)
  6. class BUFFAPI():
  7. def __init__(self, parent=None, title="buffapi接口", initial_data=None):
  8. super().__init__(parent)
  9. # 获取商品磨损信息
  10. @staticmethod
  11. def getPaintwearChoices(self_buff,goods_id):
  12. self_buff.detail_logger.debug(f"获取商品id:{goods_id}的磨损信息")
  13. self_buff.paintwear_choices = []
  14. current_time = time.time()
  15. time_since_last_request = current_time - self_buff.last_paintwear_request_time
  16. if time_since_last_request < float(self_buff.pause_time):
  17. wait_time = float(self_buff.pause_time) - time_since_last_request
  18. self_buff.detail_logger.debug(f"暂停 {wait_time:.2f} seconds before making the next request.")
  19. time.sleep(float(wait_time))
  20. self_buff.xp_my_headers.update({
  21. 'Cookie': self_buff.cookie_text
  22. })
  23. try:
  24. api2_res = req.get(url=self_buff.xp_getPaintwearChoicesUrl + str(goods_id), headers=self_buff.xp_my_headers)
  25. self_buff.detail_logger.debug(f'获取商品id:{goods_id}磨损信息请求的返回报文: {api2_res.text.strip()[:50]}')
  26. # data = api2_res.json()['data']
  27. if api2_res.json()['code'] == 'Captcha Validate Required':
  28. self_buff.detail_logger.debug('选品获取磨损信息接口请求返回报文code为Captcha Validate Required,暂停30秒后重试')
  29. retry_count = 0
  30. while retry_count < int(self_buff.needed_msg_check_try_times):
  31. self_buff.detail_logger.debug(f'选品获取磨损信息接口第{retry_count+1}次重试')
  32. time.sleep(float(self_buff.needed_msg_check_pause_time))
  33. api2_res = req.get(url=self_buff.xp_getPaintwearChoicesUrl + str(goods_id), headers=self_buff.xp_my_headers)
  34. # data = api2_res.json()['data']
  35. if api2_res.json()['code'] == 'System Error':
  36. continue
  37. if api2_res.json()['code'] == 'OK':
  38. break
  39. if api2_res.json()['code'] == 'Captcha Validate Required':
  40. retry_count += 1
  41. if api2_res.json()['code'] == 'Captcha Validate Required':
  42. self_buff.detail_logger.debug('选品获取磨损信息接口请求返回报文code仍然为Captcha Validate Required,跳出循环')
  43. return 'stop_flag'
  44. elif api2_res.json()['code'] != 'OK':
  45. # 如果data中的code不等于OK,则跳出循环
  46. self_buff.detail_logger.debug(f'获取商品id:{goods_id}磨损信息接口请求返回报文: {api2_res.text.strip()[:50]}')
  47. self_buff.detail_logger.debug(f'获取商品id:{goods_id}磨损信息接口请求返回报文code不等于OK,跳出循环')
  48. return
  49. data = api2_res.json()['data']
  50. self_buff.paintwear_choices = data['paintwear_choices']
  51. self_buff.detail_logger.debug(f"获取商品id:{goods_id}的磨损信息: {self_buff.paintwear_choices}" )
  52. except Exception as e:
  53. self_buff.detail_logger.debug(f'获取商品id:{goods_id}的磨损信息接口请求--失败:{e}')
  54. finally:
  55. self_buff.last_paintwear_request_time = time.time() # 更新上次请求时间为当前时间
  56. # 获取磨损区间最低价格信息
  57. @staticmethod
  58. def getPaintwearChoicesMinPrice(self, goods_id, min_paint_wear, max_paint_wear):
  59. # min_paint_wear 为空 打印语句写成不限区间
  60. if min_paint_wear == '' and max_paint_wear == '':
  61. self.detail_logger.debug(f"获取商品id:{goods_id}的磨损不限区间的最低价格")
  62. else:
  63. self.detail_logger.debug(f"获取商品id:{goods_id}的磨损区间{min_paint_wear}--{max_paint_wear}的最低价格")
  64. # 开始时间为当前时间
  65. min_price = 0
  66. current_time = time.time()
  67. time_since_last_request = current_time - self.last_minprice_request_time
  68. if time_since_last_request < float(self.pause_time):
  69. wait_time = float(self.pause_time) - time_since_last_request
  70. self.detail_logger.debug(f"暂停 {wait_time:.2f} seconds before making the next request.")
  71. time.sleep(float(wait_time))
  72. self.xp_my_headers.update({
  73. 'Cookie': self.cookie_text
  74. })
  75. url = self.xp_getPriceUrl + str(goods_id)
  76. # min_paint_wear 不为空
  77. if min_paint_wear != '':
  78. self.detail_logger.debug('min_paint_wear 最低磨损值 不为空')
  79. url += '&min_paintwear=' + str(min_paint_wear)
  80. # max_paint_wear 不为空
  81. if max_paint_wear != '':
  82. self.detail_logger.debug('max_paint_wear 最高磨损值 不为空')
  83. url += '&max_paintwear=' + str(max_paint_wear)
  84. try:
  85. api2_res = req.get(url=url, headers=self.xp_my_headers)
  86. if min_paint_wear == '' and max_paint_wear == '':
  87. self.detail_logger.debug(f'获取商品id:{goods_id}磨损不限区间最低价格请求的返回报文: {api2_res.text.strip()[:50]}')
  88. else:
  89. self.detail_logger.debug(f'获取商品id:{goods_id}磨损区间{min_paint_wear}-{max_paint_wear}最低价格请求的返回报文: {api2_res.text.strip()[:50]}')
  90. self.detail_logger.debug(f"api2_res.json()['code']={api2_res.json()['code']}")
  91. if api2_res.json()['code'] == 'Captcha Validate Required':
  92. self.detail_logger.debug(f'选品获取磨损区间最低价格接口请求返回报文code为Captcha Validate Required,暂停{self.needed_msg_check_pause_time}秒后重试')
  93. retry_count = 0
  94. while retry_count < int(self.needed_msg_check_try_times):
  95. self.detail_logger.debug(f'选品获取磨损区间最低价格接口请求接口第{retry_count+1}次重试')
  96. time.sleep(float(self.needed_msg_check_pause_time))
  97. api2_res = req.get(url=url, headers=self.xp_my_headers)
  98. # data = api2_res.json()['data']
  99. if api2_res.json()['code'] == 'System Error':
  100. continue
  101. if api2_res.json()['code'] == 'OK':
  102. break
  103. if api2_res.json()['code'] == 'Captcha Validate Required':
  104. retry_count += 1
  105. if api2_res.json()['code'] == 'Captcha Validate Required':
  106. self.detail_logger.debug('选品获取磨损区间最低价格接口请求返回报文code仍然为Captcha Validate Required,跳出循环')
  107. min_price= 'stop_flag'
  108. return
  109. # 如果data中的code不等于OK,则跳出循环
  110. if api2_res.json()['code'] != 'OK':
  111. if min_paint_wear == '' and max_paint_wear == '':
  112. self.detail_logger.debug(
  113. f'获取商品id:{goods_id}磨损不限区间最低价格请求返回报文: {api2_res.text.strip()[:50]}')
  114. self.detail_logger.debug(
  115. f'获取商品id:{goods_id}磨损不限区间最低价格返回报文code不等于OK,跳出循环')
  116. else:
  117. self.detail_logger.debug(
  118. f'获取商品id:{goods_id}磨损区间{min_paint_wear}--{max_paint_wear}最低价格请求返回报文: {api2_res.text.strip()[:50]}')
  119. self.detail_logger.debug(
  120. f'获取商品id:{goods_id}磨损区间{min_paint_wear}--{max_paint_wear}最低价格返回报文code不等于OK,跳出循环')
  121. return
  122. data = api2_res.json()['data']
  123. min_price = data['items'][0]['price']
  124. except Exception as e:
  125. if min_paint_wear == '' and max_paint_wear == '':
  126. self.detail_logger.debug(f'获取商品id:{goods_id}的磨损不限区间的最低价格请求--失败: {e}')
  127. else:
  128. self.detail_logger.debug(f'获取商品id:{goods_id}的磨损区间{min_paint_wear}-{max_paint_wear}的最低价格请求--失败: {e}')
  129. finally:
  130. self.last_minprice_request_time = time.time() # 更新上次请求时间为当前时间
  131. if min_paint_wear == '' and max_paint_wear == '':
  132. self.detail_logger.debug(f"商品id:{goods_id}的磨损不限区间的最低价格:{min_price}")
  133. else:
  134. self.detail_logger.debug(f"商品id:{goods_id}的磨损区间{min_paint_wear}-{max_paint_wear}的最低价格:{min_price}" )
  135. return min_price