TaobaoOpenPlus.cs 8.5 KB

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