OtherApiPlus.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using dodohold.core;
  2. using Org.BouncyCastle.Ocsp;
  3. using System.Text.RegularExpressions;
  4. namespace molilian.core
  5. {
  6. public partial class OtherApiPlus
  7. {
  8. private string _accountName;
  9. private string _key;
  10. private string _sessionKey;
  11. private string _style;
  12. private string _api_url;
  13. public OtherApiPlus(string accountName, string key, string sessionKey, string style, string api_url)
  14. {
  15. _accountName = accountName;
  16. _key = key;
  17. _sessionKey = sessionKey;
  18. _style = style;
  19. _api_url = api_url;
  20. }
  21. public TkDataDTO UnionParse(string content, ref TkDataDTO result)
  22. {
  23. string para = content.UrlEncode();
  24. string url = $"http://api.veapi.cn/tbk/hcapi?vekey={_key}&sessionkey={_sessionKey}&para={para}";
  25. var response = new WebClientUtility().Request(url);
  26. var body = response.Body();
  27. var root = body.Convert2JsonElement();
  28. int error = root.Read<int>("error", 0);
  29. bool success = error == 0;
  30. string message = root.Read("msg", string.Empty);
  31. string taoToken = root.PathRead("data.ios_tbk_pwd", string.Empty);
  32. string num_iid = root.PathRead("data.num_iid", string.Empty);
  33. decimal couponAmount = root.PathRead<decimal>("data.coupon_amount", 0);
  34. string shortLinkurl = root.PathRead("data.coupon_short_url", string.Empty);
  35. string shortLink = AlimamaPlus.GetLink(taoToken);
  36. content = AlimamaPlus.ReplaceUrls(content, shortLink);
  37. if (!success)
  38. {
  39. shortLinkurl = AlimamaPlus.GetLink(content);
  40. }
  41. string deeplink_url = AlimamaPlus.GetDeeplink(shortLinkurl);
  42. result.channel = TkChannelEnum.tb;
  43. result.accountName = _accountName;
  44. result.success = success;
  45. result.message = message;
  46. result.content = content;
  47. result.couponAmount = couponAmount;
  48. result.taoToken = taoToken;
  49. result.shortLinkurl = shortLinkurl;
  50. result.deeplink_url = deeplink_url;
  51. result.num_iid = num_iid;
  52. return result;
  53. }
  54. public PromotionQueryDTO picSimilaritem(string pic, ref PromotionQueryDTO result)
  55. {
  56. string url = $"http://api.veapi.cn/tbk/picsimilaritem";
  57. string base64 = $"data:image/jpeg;base64,{pic}";
  58. var client = new WebClientUtility();
  59. client.SetContentType("application/x-www-form-urlencoded");
  60. client.Post("vekey", _key);
  61. client.Post("pic", base64);
  62. var response = client.Request(url);
  63. var body = response.Body();
  64. var root = body.Convert2JsonElement();
  65. int error = root.Read<int>("error", 0);
  66. bool success = error == 0;
  67. string message = root.Read("msg", string.Empty);
  68. result.success = success;
  69. if (!success)
  70. {
  71. result.message = "调用失败";
  72. result.reason = message;
  73. return result;
  74. }
  75. List<PromotionQueryItemDTO> arr = [];
  76. var list = root.ElementRead("data");
  77. foreach (var e in list.EnumerateArray())
  78. {
  79. var item = e.ElementRead("Result");
  80. string itemId = item.Read<string>("ItemId", string.Empty);
  81. string h5_url = $"https://uland.taobao.com/item/edetail?id={itemId}";
  82. string deeplink_url = AlimamaPlus.GetDeeplink(h5_url);
  83. PromotionQueryItemDTO newItem = new PromotionQueryItemDTO();
  84. newItem.itemId = itemId;
  85. newItem.h5_url = h5_url;
  86. newItem.deeplink_url = deeplink_url;
  87. newItem.userType = item.Read<string>("UserType", string.Empty);
  88. newItem.shopTitle = item.Read<string>("ShopTitle", string.Empty);
  89. newItem.itemName = item.Read<string>("Title", string.Empty);
  90. newItem.pic = item.Read<string>("PicUrl", string.Empty);
  91. newItem.price = item.Read<string>("ReservePrice", string.Empty);
  92. newItem.zkFinalPrice = item.Read<string>("ZkFinalPrice", string.Empty);
  93. newItem.promotionPrice = item.Read<string>("PriceAfterCoupon", string.Empty);
  94. newItem.couponAmount = item.Read<string>("CouponAmount", string.Empty);
  95. newItem.couponEffectiveStartTime = item.Read<string>("CouponStartTime", string.Empty);
  96. newItem.couponEffectiveEndTime = item.Read<string>("CouponEndTime", string.Empty);
  97. newItem.sellCountStr = item.Read<string>("sellCountStr", string.Empty);
  98. newItem.provcity = item.Read<string>("Provcity", string.Empty);
  99. newItem.exposeTracks = $"https://api.molilian.com/tracks/track?track_id=7&unique_id={itemId}";
  100. newItem.clickTracks = $"https://api.molilian.com/tracks/track?track_id=8&unique_id={itemId}";
  101. arr.Add(newItem);
  102. }
  103. result.message = "OK";
  104. result.reason = string.Empty;
  105. result.PromotionImg = arr;
  106. //PromotionImg
  107. /*
  108. *
  109. public string h5_url { get; set; } = string.Empty;
  110. public string deeplink_url { get; set; } = string.Empty;
  111. /// <summary>
  112. /// 店铺信息-卖家类型:1天猫,0淘宝,3淘特
  113. /// </summary>
  114. public string userType { get; set; } = string.Empty;
  115. public string shopTitle { get; set; } = string.Empty;
  116. public string itemId { get; set; } = string.Empty;
  117. public string itemName { get; set; } = string.Empty;
  118. public string pic { get; set; } = string.Empty;
  119. public string price { get; set; } = string.Empty;
  120. public string promotionPrice { get; set; } = string.Empty;
  121. public string couponAmount { get; set; } = string.Empty;
  122. public string couponEffectiveStartTime { get; set; } = string.Empty;
  123. public string couponEffectiveEndTime { get; set; } = string.Empty;
  124. public string sellCountStr { get; set; } = string.Empty;
  125. public string provcity { get; set; } = string.Empty;
  126. {
  127. "error": "0",
  128. "msg": "查询成功",
  129. "data": [
  130. {
  131. "RankScore": "0.8065",
  132. "Result": {
  133. "CategoryName": "跑步鞋",
  134. "CommissionRate": "135",
  135. "CouponAmount": 50,
  136. "CouponEndTime": "2025-01-31",
  137. "CouponInfo": "满99.0元减50.0元",
  138. "CouponRemainCount": 99656,
  139. "CouponStartFee": "99.0",
  140. "CouponStartTime": "2024-12-24",
  141. "CouponTotalCount": "100000",
  142. "ItemId": "jOZk7gxcAfJZ6zGz8bFKjwSJte-Z9Z023UAg5djJmJSRp",
  143. "LevelOneCategoryName": "运动鞋new",
  144. "MaxCommission": {},
  145. "Nick": "",
  146. "PicUrl": "https://img.alicdn.com/i2/1994113638/O1CN015M0Aqf1ckFs5UI2eJ_!!0-item_pic.jpg",
  147. "PriceAfterCoupon": "109.00",
  148. "Provcity": "福建 泉州",
  149. "ReservePrice": "259.00",
  150. "SellerId": "1994113638",
  151. "ShopTitle": "361度户外旗舰店",
  152. "ShortTitle": "加绒保暖老爹鞋361女鞋运动鞋女",
  153. "SubTitle": "新款加绒女鞋运动鞋",
  154. "Title": "361女鞋运动鞋女2024冬季新款加绒保暖棉鞋轻便老爹鞋休闲跑步鞋",
  155. "UserType": 1,
  156. "Volume": 10,
  157. "ZkFinalPrice": "159"
  158. }
  159. }
  160. ]
  161. ],
  162. "request_id": "4eZfpBu"
  163. }
  164. */
  165. return result;
  166. }
  167. public async Task<TkDataDTO> UnionParseAsync(string content, string pid, TkDataDTO result, CancellationToken cancellationToken)
  168. {
  169. switch (_style)
  170. {
  171. case "ddx":
  172. return await DdxParseAsync(content, pid, result, cancellationToken);
  173. case "veapi":
  174. default:
  175. return await VeapiParseAsync(content, pid, result, cancellationToken);
  176. }
  177. }
  178. private async Task<TkDataDTO> VeapiParseAsync(string content, string pid, TkDataDTO result, CancellationToken cancellationToken)
  179. {
  180. string para = content.UrlEncode();
  181. string url = $"http://api.veapi.cn/tbk/generalconvert?vekey={_key}&para={para}&pid={pid}";
  182. if (!string.IsNullOrEmpty(_api_url))
  183. {
  184. url = _api_url;
  185. url = url.Replace("{apikey}", _key);
  186. url = url.Replace("{pid}", pid);
  187. url = url.Replace("{content}", para);
  188. }
  189. var response = await new WebClientUtility().RequestAsync(url, "GET", cancellationToken);
  190. var body = response.Body();
  191. var root = body.Convert2JsonElement();
  192. int error = root.Read<int>("error", 0);
  193. bool success = error == 0;
  194. string message = root.Read("msg", string.Empty);
  195. var data = root.ElementRead("data");
  196. result.channel = TkChannelEnum.tb;
  197. result.accountName = _accountName;
  198. result.success = success;
  199. result.message = message;
  200. if (success)
  201. {
  202. result.content = content;
  203. result.message = string.Empty;
  204. result.commercial = true;
  205. result.couponAmount = data.Read<decimal>("coupon_amount", 0);
  206. result.mktId = data.Read<string>("isv_mktid", string.Empty);
  207. result.itemId = data.Read<string>("item_id", string.Empty);
  208. result.itemId = content.GetContentPart("item.htm?id=", "");
  209. // veapi 缺少商品pic 和 promotionPrice
  210. result.taoToken = data.Read<string>("cps_full_tpwd", string.Empty);
  211. result.item_url = AlimamaPlus.GetLink(result.taoToken);
  212. result.item_deeplink_url = AlimamaPlus.GetDeeplink(result.shortLinkurl);
  213. string coupon_full_tpwd = data.Read<string>("coupon_full_tpwd", string.Empty);
  214. if (!string.IsNullOrEmpty(coupon_full_tpwd)) result.taoToken = coupon_full_tpwd;
  215. result.shortLinkurl = AlimamaPlus.GetLink(result.taoToken);
  216. result.deeplink_url = AlimamaPlus.GetDeeplink(result.shortLinkurl);
  217. result.itemName = result.taoToken.GetContentPart(result.shortLinkurl, "").Trim();
  218. return result;
  219. }
  220. else
  221. {
  222. result.message = "转链失败";
  223. result.reason = message;
  224. }
  225. return result;
  226. }
  227. private async Task<TkDataDTO> DdxParseAsync(string content, string pid, TkDataDTO result, CancellationToken cancellationToken)
  228. {
  229. string para = result.rawContent.UrlEncode();
  230. string url = $"https://api.tbk.dingdanxia.com/tbk/tkl_privilege?apikey={_key}&pid={pid}&relation_id=3197256735&activityId=&extspk=&tb_auth_id=&tkl=XXX+{para}";
  231. if (!string.IsNullOrEmpty(_api_url))
  232. {
  233. url = _api_url;
  234. url = url.Replace("{apikey}", _key);
  235. url = url.Replace("{pid}", pid);
  236. url = url.Replace("{content}", para);
  237. }
  238. var client = new WebClientUtility();
  239. #if DEBUG
  240. var response = await client.RequestAsync(url, "GET");
  241. #else
  242. var response = await client.RequestAsync(url, "GET", cancellationToken);
  243. #endif
  244. var body = response.Body();
  245. var root = body.Convert2JsonElement();
  246. int code = root.Read<int>("code", 0);
  247. bool success = code == 200;
  248. string message = root.Read("msg", string.Empty);
  249. if (message.StartsWith("数据返回失败【"))
  250. {
  251. message = message.GetContentPart("数据返回失败【", "】");
  252. }
  253. var data = root.ElementRead("data");
  254. result.channel = TkChannelEnum.tb;
  255. result.accountName = _accountName;
  256. result.success = success;
  257. result.message = message;
  258. if (success)
  259. {
  260. result.content = content;
  261. result.message = string.Empty;
  262. result.commercial = true;
  263. result.couponAmount = data.Read<decimal>("coupon", 0);
  264. result.couponEffectiveStartTime = data.Read<string>("coupon_start_time", string.Empty);
  265. result.couponEffectiveEndTime = data.Read<string>("coupon_end_time", string.Empty);
  266. result.mktId = data.Read<string>("item_id", string.Empty);
  267. result.itemId = content.GetContentPart("item.htm?id=", "");
  268. result.pic = data.PathRead<string>("itemInfo.pict_url", string.Empty);
  269. result.sellerNickName = data.PathRead<string>("itemInfo.nick", string.Empty);
  270. result.promotionPrice = data.PathRead<decimal>("itemInfo.qh_final_price", 0);
  271. if (result.promotionPrice == 0)
  272. {
  273. result.promotionPrice = data.PathRead<decimal>("itemInfo.zk_final_price", 0);
  274. }
  275. result.taoToken = data.Read<string>("long_item_tpwd", string.Empty);
  276. //string long_coupon_tpwd = data.Read<string>("long_coupon_tpwd", string.Empty);
  277. //if (!string.IsNullOrEmpty(long_coupon_tpwd)) result.taoToken = long_coupon_tpwd;
  278. result.shortLinkurl = AlimamaPlus.GetLink(result.taoToken);
  279. result.itemName = result.taoToken.GetContentPart(result.shortLinkurl, "").Trim();
  280. result.deeplink_url = AlimamaPlus.GetDeeplink(result.shortLinkurl);
  281. return result;
  282. }
  283. else
  284. {
  285. result.content = result.rawContent;
  286. switch (message)
  287. {
  288. case "该口令无效,请检查后重新提交":
  289. case "该商品已下架":
  290. result.message = "转链失败";
  291. result.reason = message;
  292. result.subCode = TkSubCodeEnum.NoConvert;
  293. break;
  294. default:
  295. result.message = "转链失败";
  296. result.reason = message;
  297. result.subCode = TkSubCodeEnum.NetError;
  298. break;
  299. }
  300. return result;
  301. }
  302. }
  303. }
  304. }