fill_data.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # -*- coding: utf-8 -*-
  2. # Author : Charley
  3. # Python : 3.8.10
  4. # Date: 2024-09-24 10:50
  5. import requests
  6. from loguru import logger
  7. from retrying import retry
  8. from mysq_pool import MySQLConnectionPool
  9. base_url = "https://api.weikajia.com"
  10. def save_data(sql_pool,info):
  11. logger.debug("正在写入数据库............")
  12. sql = """
  13. UPDATE weikajia_bidding
  14. SET nickName=%s,
  15. avatarOss=%s,
  16. following=%s,
  17. following=%s,
  18. voteRate=%s,
  19. level=%s,
  20. introduceSign=%s,
  21. certifyStatus=%s,
  22. ipRegion=%s,
  23. blueVflag=%s,
  24. shopVflag=%s,
  25. credit=%s,
  26. agentLevel=%s,
  27. agentId=%s
  28. WHERE id=%s
  29. """
  30. sql_pool.update_one(sql, info)
  31. @retry(stop_max_attempt_number=3, wait_fixed=1000)
  32. def get_action(auctionId,headers):
  33. """
  34. 获取auction信息
  35. :param auctionId:
  36. :return: agentUserInfo
  37. """
  38. logger.debug(f'正在查询auctionId为: {auctionId}的信息..............')
  39. url = f"{base_url}/api/v2/auction/detail"
  40. params = {
  41. "auctionId": auctionId
  42. }
  43. response = requests.get(url, headers=headers, params=params, timeout=5)
  44. # print(f'get_action: {response.json()}')
  45. if response.json()["resultCode"] == 200:
  46. agentUserInfo = response.json()["data"].get("agentUserInfo")
  47. agentId = response.json()["data"].get("agentId")
  48. agentUserInfo["agentId"] = agentId
  49. return agentUserInfo
  50. else:
  51. logger.debug("get_action 请求失败,重试中...........")
  52. raise Exception("请求失败")
  53. @retry(stop_max_attempt_number=3, wait_fixed=1000)
  54. def get_cabinet(cabinetId,headers):
  55. """
  56. 获取cabinet信息
  57. :param cabinetId:
  58. :return: cab_dict
  59. """
  60. logger.debug(f'正在查询cabinetId为: {cabinetId}的信息..............')
  61. url = f"{base_url}/api/v2/cabinet/detail"
  62. params = {
  63. "cabinetId": cabinetId
  64. }
  65. response = requests.get(url, headers=headers, params=params, timeout=5)
  66. # print(f'get_cabinet: {response.json()}')
  67. if response.json()["resultCode"] == 200:
  68. data = response.json()["data"]
  69. cab_dict = {"rmbPrice": data.get("rmbPrice"), "brand": data.get("brand"), "status": data.get("status"),
  70. "switchSt": data.get("switchSt"), "cardNo": data.get("cardNo"),
  71. "barcodeId": data.get("barcodeId"), "year": data.get("year"), "grade": data.get("grade"),
  72. "setName": data.get("setName"), "player": data.get("player"),
  73. "onSaleExpireTs": data.get("onSaleExpireTs"), "authenticNumber": data.get("authenticNumber")
  74. }
  75. return cab_dict
  76. else:
  77. logger.debug("get_cabinet 请求失败,重试中...........")
  78. raise Exception("请求失败")
  79. def fill_main():
  80. try:
  81. sql_pool = MySQLConnectionPool(log=logger)
  82. if not sql_pool:
  83. logger.error("数据库连接失败")
  84. raise Exception("数据库连接失败")
  85. token = sql_pool.select_one("select token from wkj_token")
  86. headers = {
  87. "appVersion": "1.6.5",
  88. "osVersion": "9",
  89. "deviceModel": "M2007J22C",
  90. "appVersionCode": "168",
  91. "deviceBrand": "xiaomi",
  92. "platform": "android",
  93. "token": token[0],
  94. "user-agent": "Mozilla/5.0 (Linux; Android 9; M2007J22C Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/92.0.4515.131 Mobile Safari/537.36",
  95. "Content-Type": "application/json",
  96. "Connection": "Keep-Alive"
  97. }
  98. sql = """
  99. SELECT id,cabinetId, auctionItemId
  100. FROM weikajia_bidding
  101. WHERE nickName IS NULL
  102. AND avatarOss IS NULL
  103. AND following IS NULL
  104. AND voteRate IS NULL
  105. AND level IS NULL
  106. AND introduceSign IS NULL
  107. AND certifyStatus IS NULL
  108. AND ipRegion IS NULL
  109. AND blueVflag IS NULL
  110. AND shopVflag IS NULL
  111. AND credit IS NULL
  112. AND agentLevel IS NULL
  113. AND agentId IS NULL
  114. AND rmbPrice IS NULL
  115. AND brand IS NULL
  116. AND status IS NULL
  117. AND switchSt IS NULL
  118. AND cardNo IS NULL
  119. AND printNo IS NULL
  120. AND printNoS IS NULL
  121. AND sportId IS NULL
  122. AND barcodeId IS NULL
  123. AND year IS NULL
  124. AND grade IS NULL
  125. AND setName IS NULL
  126. AND playerIds IS NULL
  127. AND player IS NULL
  128. AND onSaleExpireTs IS NULL
  129. AND authenticNumber IS NULL;
  130. """
  131. res = sql_pool.select_all(sql)
  132. for ii in res:
  133. sql_id = ii[0]
  134. cabinetId = ii[1]
  135. auctionId = ii[2]
  136. try:
  137. act_dict = get_action(auctionId, headers)
  138. cab_dict = get_cabinet(cabinetId, headers)
  139. info = (act_dict, cab_dict,sql_id)
  140. save_data(sql_pool,info)
  141. # time.sleep(random.randint(3, 5))
  142. except Exception as e:
  143. logger.error(f'出错, {e}')
  144. except Exception as e:
  145. logger.error(e)
  146. finally:
  147. logger.info("爬虫程序运行结束,等待下一轮的采集任务.............")