TaobaoOpenPlus.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using dodohold.core;
  2. using System.Net;
  3. using System.Text.Json;
  4. using System.Xml;
  5. using TencentCloud.Lighthouse.V20200324.Models;
  6. using TencentCloud.Weilingwith.V20230427.Models;
  7. using Top.Api;
  8. using Top.Api.Request;
  9. using Top.Api.Response;
  10. namespace molilian.core
  11. {
  12. public partial class TaobaoOpenPlus
  13. {
  14. public enum RecommendRequestType
  15. {
  16. first,
  17. second,
  18. ori,
  19. }
  20. string _app_key = string.Empty;
  21. string _app_secret = string.Empty;
  22. string _pid = string.Empty;
  23. string _server_url = "https://eco.taobao.com/router/rest";
  24. public TaobaoOpenPlus(string app_key, string app_secret, string pid)
  25. {
  26. _app_key = app_key;
  27. _app_secret = app_secret;
  28. _pid = pid;
  29. }
  30. public static PromotionQueryItemDTO ConverPromotionQueryItem(JsonElement item)
  31. {
  32. PromotionQueryItemDTO result = JsonSerializer.Deserialize<PromotionQueryItemDTO>(item.GetRawText());
  33. string h5_url = item.Read("couponShareUrl", string.Empty);
  34. if (string.IsNullOrEmpty(h5_url))
  35. {
  36. h5_url = item.Read("url", string.Empty);
  37. }
  38. if (h5_url.StartsWith("//")) h5_url = "https:" + h5_url;
  39. result.h5_url = h5_url;
  40. result.deeplink_url = AlimamaPlus.GetDeeplink(h5_url);
  41. if (result.pic.StartsWith("//")) result.pic = "https:" + result.pic;
  42. return result;
  43. }
  44. public TbkThorMfrsPromotionQueryResponse PromotionQueryRequest(string base64, string oaid)
  45. {
  46. ITopClient client = new DefaultTopClient(_server_url, _app_key, _app_secret, "json");
  47. TbkThorMfrsPromotionQueryRequest req = new();
  48. TbkThorMfrsPromotionQueryRequest.ManufacturerLaunchOptionDomain obj1 = new()
  49. {
  50. Img = base64,
  51. Pid = _pid,
  52. UniqueId = oaid,
  53. UniqueIdType = "oaid",
  54. SceneCode = "h5_img",
  55. SceneSubCode = "Hitoch"
  56. };
  57. req.Option_ = obj1;
  58. TbkThorMfrsPromotionQueryResponse rsp = client.Execute(req);
  59. return rsp;
  60. }
  61. public async Task<(List<PromotionQueryItemDTO>, string, int)> GetRecommendDataWithRetry(
  62. RecommendRequestType type, string oaid,
  63. string cookies,
  64. CancellationToken cancellationToken = default)
  65. {
  66. DateTime stime = DateTime.Now;
  67. // 假设传入了一个 CancellationToken cancellationToken 和 int maxRetries 表示最大重试次数
  68. int retryCount = 0;
  69. int maxRetries = 50;
  70. while (true)
  71. {
  72. try
  73. {
  74. (var data, string cookie) = await GetRecommendData(type, oaid, cookies, cancellationToken);
  75. var ts = DateTime.Now - stime;
  76. int elapsedTime = (int)ts.TotalMilliseconds;
  77. return (data, cookie, elapsedTime);
  78. }
  79. catch (Exception ex)
  80. {
  81. if (retryCount >= maxRetries || cancellationToken.IsCancellationRequested)
  82. {
  83. throw;
  84. }
  85. retryCount++;
  86. Thread.Sleep(100);
  87. }
  88. }
  89. }
  90. public async Task<(List<PromotionQueryItemDTO>, string)> GetRecommendData(RecommendRequestType type, string oaid,
  91. string cookies, CancellationToken cancellationToken = default)
  92. {
  93. string token = "undefined";
  94. if (!string.IsNullOrEmpty(cookies)) token = cookies.GetContentPart("_m_h5_tk=", "_");
  95. string url = GetRecommendUrl(_pid, oaid, token, type);
  96. string body = string.Empty;
  97. if (string.IsNullOrEmpty(cookies))
  98. {
  99. var res = await new WebClientUtility().RequestAsync(url, "GET", cancellationToken);
  100. if (!res.Successed) { throw res.ResponseException; }
  101. body = res.Body();
  102. cookies = res.ResponseMessage.GetCookiesString();
  103. token = cookies.GetContentPart("_m_h5_tk=", "_");
  104. }
  105. url = GetRecommendUrl(_pid, oaid, token, type);
  106. WebClientUtility client = new WebClientUtility();
  107. if (!string.IsNullOrEmpty(cookies)) client.SetCookies(cookies);
  108. var response = await client.RequestAsync(url, "GET", cancellationToken);
  109. body = response.Body();
  110. body = body.GetContentPart("mtopjsonp1(", ")");
  111. var root = body.Convert2JsonElement();
  112. var ret = root.PathRead("ret[0]", string.Empty);
  113. if (!"SUCCESS::调用成功".Equals(ret))
  114. {
  115. throw new Exception(ret);
  116. }
  117. var resultList = new List<PromotionQueryItemDTO>();
  118. foreach (var data in root.ElementRead("data").ElementRead("resultList").EnumerateArray())
  119. {
  120. PromotionQueryItemDTO item = ConverPromotionQueryItem(data);
  121. resultList.Add(item);
  122. }
  123. return (resultList, cookies);
  124. }
  125. public string GetRecommendUrl(string pid, string oaid, string token, RecommendRequestType type)
  126. {
  127. var first_data = $"{{\"scene\":\"pltSimilarPromotion\",\"variableMap\":\"{{\\\"pid\\\":\\\"{pid}\\\",\\\"oaid\\\":\\\"{oaid}\\\",\\\"type\\\":\\\"first\\\"}}\"}}";
  128. var second_data = $"{{\"scene\":\"pltSimilarPromotion\",\"variableMap\":\"{{\\\"pid\\\":\\\"{pid}\\\",\\\"oaid\\\":\\\"{oaid}\\\",\\\"type\\\":\\\"second\\\"}}\"}}";
  129. var ori_data = $"{{\"scene\":\"pltPromotionImg\",\"variableMap\":\"{{\\\"pid\\\":\\\"{pid}\\\",\\\"oaid\\\":\\\"{oaid}\\\",\\\"hasCoupon\\\":false,\\\"filterType\\\":\\\"ori\\\",\\\"seq\\\":\\\"\\\"}}\"}}";
  130. string ts = DateTime.Now.Convert2UnixTimestamp(true).ToString();
  131. string data = type switch
  132. {
  133. RecommendRequestType.first => first_data,
  134. RecommendRequestType.second => second_data,
  135. _ => ori_data,
  136. };
  137. string sign = GetSign(token, ts, data);
  138. string url = $"https://h5api.m.taobao.com/h5/mtop.union.thor.landing.page.recommend/1.0/" +
  139. $"?jsv=2.4.16&appKey=12574478&t={ts}&sign={sign}&v=1.0&api=mtop.union.thor.landing.page.recommend" +
  140. $"&timeout=20000&AntiCreep=true&AntiFlood=true&type=jsonp&dataType=jsonp&callback=mtopjsonp1" +
  141. $"&data={data.UrlEncode()}";
  142. return url;
  143. }
  144. private string GetSign(string token, string ts, string data)
  145. {
  146. string g = "12574478";
  147. //%7B%22scene%22%3A%22pltPromotionImg%22%2C%22variableMap%22%3A%22%7B%5C%22pid%5C%22%3A%5C%22mm_5993059531_3080100059_115689750259%5C%22%2C%5C%22oaid%5C%22%3A%5C%22-7775661578900138800%5C%22%2C%5C%22hasCoupon%5C%22%3Afalse%2C%5C%22filterType%5C%22%3A%5C%22ori%5C%22%2C%5C%22seq%5C%22%3A%5C%22%5C%22%7D%22%7D
  148. //%7B%22scene%22%3A%22pltPromotionImg%22%2C%22variableMap%22%3A%22%7B%5C%22pid%5C%22%3A%5C%22mm_5993059531_3080100059_115689750259%5C%22%2C%5C%22oaid%5C%22%3A%5C%22-7775661578900138800%5C%22%2C%5C%22hasCoupon%5C%22%3Afalse%2C%5C%22filterType%5C%22%3A%5C%22ori%5C%22%2C%5C%22seq%5C%22%3A%5C%22%5C%22%7D%22%7D
  149. var signString = $"{token}&{ts}&{g}&{data}";
  150. return signString.MD5();
  151. }
  152. }
  153. }