算法暴露接口(xhs、dy、ks、wx、hnw)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
7.1 KiB

7 months ago
  1. import binascii
  2. import json
  3. import random
  4. import time
  5. from utils.tool import download_q
  6. from xhs.xhs_aes import aes_encrypt
  7. from xhs.xhs_des import MD5, des_encrypt, logical_right_shift
  8. import base64
  9. """
  10. xhs xs-xt aes
  11. """
  12. def get_xs(a1, url, data, x_t):
  13. """
  14. {"x-s":"", "x-t": ""}
  15. :param a1:
  16. :param url:
  17. :param data:
  18. :param x_t:
  19. :return:
  20. """
  21. key = [187050025, 472920585, 186915882, 876157969, 255199502, 806945584, 220596020, 958210835, 757275681, 940378667,
  22. 489892883, 705504304, 354103316, 688857884, 890312192, 219096591, 622400037, 254088489, 907618332, 52759587,
  23. 907877143, 53870614, 839463457, 389417746, 975774727, 372382245, 437136414, 909246726, 168694017, 473575703,
  24. 52697872, 1010440969]
  25. # 7.19 改
  26. key = [52833590, 1010372866, 188091914, 406398501, 255201040, 957421848, 741478954, 958217745, 758320394, 990653224,
  27. 958072630, 722273561, 890968096, 185282339, 890768915, 254222393, 890835209, 86457382, 907354431, 120004616,
  28. 906834724, 120984878, 841809977, 370543655, 405617431, 909250592, 439235128, 875174166, 187044111, 742001189,
  29. 184950816, 1010310941]
  30. def get_enviroment(api="/api/sns/web/v1/feed", data="{}"):
  31. url = f"url={api}"
  32. info = url + data
  33. info = MD5(info)
  34. return info
  35. x1 = get_enviroment(url, data)
  36. x2 = "0|0|0|1|0|0|1|0|0|0|1|0|0|0|0"
  37. x3 = a1
  38. x4 = x_t
  39. info = f"x1={x1};x2={x2};x3={x3};x4={x4};"
  40. key_46_base64 = base64.b64encode(info.encode('utf-8')).decode('utf-8')
  41. # base64 转为十进制数
  42. array = [ord(i) for i in key_46_base64]
  43. # 对上述数组进行位移操作 初始化明文 每四个数为一组进行计算
  44. # payload = des_encrypt(key, array)[0]
  45. payload = aes_encrypt(array)
  46. _base_data = {
  47. "signSvn": "53",
  48. "signType": "x2",
  49. "appId": "xhs-pc-web",
  50. "signVersion": "1",
  51. "payload": payload
  52. }
  53. data = json.dumps(_base_data, ensure_ascii=False, separators=(',', ':'))
  54. x_s = base64.b64encode(data.encode("utf-8")).decode("utf-8")
  55. return {"x-s": "XYW_" + x_s, "x-t": x4}
  56. def decrypt_info(captcha_info):
  57. """
  58. captchaInfo进行解密
  59. :param captcha_info:
  60. :return:
  61. """
  62. d1_h = base64.b64decode(captcha_info).hex()
  63. hex_string = [int(d1_h[i:i + 2], 16) for i in range(0, len(d1_h), 2)]
  64. key = [302776838, 875694381, 453784583, 103810086, 571285557, 371853838, 805843717, 657858610, 909711421, 117571586,
  65. 806950164, 52892707, 890572860, 184812328, 605753650, 151859252, 890381338, 556605977, 221318145, 688470832,
  66. 20581922, 940376595, 220597508, 706218773, 707144202, 940115729, 153491459, 892344601, 171182610, 337259779,
  67. 36577297, 271782150]
  68. payload = des_encrypt(key, hex_string)[-1]
  69. return payload
  70. def encrypt_info(plain_text, _type="mouseEnd"):
  71. """
  72. mouseEnd: 286/360
  73. time:
  74. track: [x,y,]
  75. width: 286
  76. :param plain_text:
  77. :param _type:
  78. :return:
  79. """
  80. key_list = {
  81. "mouseEnd": [187040774,941175553,188094512,672732199,87169561,957943856,758133532,957159952,756225326,588057094,890963239,185401634,889790517,185020707,874067993,103227439,874058250,119748888,906756366,119942921,571280678,104203531,304941601,372245811,439101969,808593969,170278418,876218908,186782722,1010306836,187048244,942146861],
  82. "time": [151392289,942357812,20069652,739772683,18094115,959001623,789850125,17376056,624043796,51713567,606673961,990187014,1007232771,571224112,286990908,36584204,872954931,235544848,18625028,103158589,35458076,86060551,908336391,19927066,572793385,842413842,170468621,907553325,422194994,806880270,170526239,738861861],
  83. "track": [187578120,538642213,135659559,941298489,221391896,489174819,724704537,688193565,605502240,723068432,890766372,453786172,691602974,50791978,874190097,656489730,337328660,120465962,856495123,218504222,805646351,104007424,373040900,371339527,170658349,622200847,539437315,876100658,455352379,975704860,456002850,940583190],
  84. "width": [187040774,941175553,188094512,672732199,87169561,957943856,758133532,957159952,756225326,588057094,890963239,185401634,889790517,185020707,874067993,103227439,874058250,119748888,906756366,119942921,571280678,104203531,304941601,372245811,439101969,808593969,170278418,876218908,186782722,1010306836,187048244,942146861]
  85. }
  86. key = key_list[_type]
  87. padding = 8 - len(plain_text) % 8
  88. plain_text = [ord(i) for i in plain_text]
  89. for c in range(padding): # 补位
  90. plain_text.append(padding)
  91. array1_hex2 = des_encrypt(key, plain_text)[0]
  92. byte_data = binascii.unhexlify(array1_hex2)
  93. base64_data = base64.b64encode(byte_data).decode("utf-8")
  94. return base64_data
  95. def get_a1(platform_type="Mac OS"):
  96. """
  97. cookie a1webId
  98. :param platform_type:
  99. :return:
  100. """
  101. def genRandomString(length):
  102. char_list = "abcdefghijklmnopqrstuvwxyz1234567890"
  103. return ''.join(random.sample(char_list, length))
  104. def crc32(data):
  105. table = []
  106. for num in range(256):
  107. temp = num
  108. for _ in range(8):
  109. temp = (logical_right_shift(temp, 1)) ^ 0XEDB88320 if temp & 1 else logical_right_shift(temp, 1)
  110. table.append(temp)
  111. i = -1
  112. for a in range(len(data)):
  113. i = logical_right_shift(i, 8) ^ table[255 & (i ^ ord(data[a]))]
  114. return logical_right_shift((-1 ^ i), 0)
  115. def getPlatformCode(t):
  116. # 根据不同系统返回结果
  117. if t == "Android":
  118. return "2"
  119. elif t == "iOS":
  120. return "1"
  121. elif t == "Mac OS":
  122. return "3"
  123. elif t == "Linux":
  124. return "4"
  125. # elif t == "Windows":
  126. # return "0"
  127. else:
  128. return "5"
  129. LOCAL_ID_SECRET_VERSION = "0"
  130. o = str(hex(int(time.time() * 1000))[2:])
  131. o = o + genRandomString(30)
  132. o = o + getPlatformCode(platform_type) + LOCAL_ID_SECRET_VERSION + "000"
  133. o = o + str(crc32(o))
  134. a1 = o[:52]
  135. web_id = MD5(a1)
  136. return a1, web_id
  137. def get_comment(a1, xs, xt):
  138. """
  139. node来获得x-comment参数
  140. :param a1:
  141. :param xs:
  142. :param xt:
  143. :return:
  144. """
  145. url = "http://127.0.0.1:3000/xhs/get_comment"
  146. data = {
  147. "a1": a1,
  148. "xs": xs,
  149. "xt": xt
  150. }
  151. status, response = download_q(url, {}, {}, data=data)
  152. return response.text
  153. if __name__ == '__main__':
  154. data = {
  155. "source_note_id": "64bf4ea7000000000c035e48",
  156. "image_formats": [
  157. "jpg",
  158. "webp",
  159. "avif"
  160. ],
  161. "extra": {
  162. "need_body_topic": "1"
  163. },
  164. "xsec_source": "pc_feed",
  165. "xsec_token": "ABSnHLvO9lnVSMKpZb-K1Cpn6d3xUC3z_mhRBvAbPGIe4="
  166. }
  167. data = json.dumps(data, ensure_ascii=False, separators=(',', ':'))
  168. res = get_xs("191452e29d5vt5m5noazsyty9z4z3z695yoiu1pn130000168225", "/api/sns/web/v1/feed",data, "1723443461839")
  169. print(res)