TaobaoOpenPlus.cs 8.7 KB

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