TaokeOpenCore.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using dodohold.core;
  2. using System.Text.Json;
  3. using Top.Api.Response;
  4. namespace molilian.core
  5. {
  6. public partial class TaokeOpenCore
  7. {
  8. static string _test_base64String = string.Empty;
  9. public TkConfigDTO _config;
  10. private static string _end_point;
  11. static TaokeOpenCore()
  12. {
  13. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  14. }
  15. public TaokeOpenCore()
  16. {
  17. _config = TkConfigCore.Get();
  18. }
  19. public async Task<APIResult> PromotionQueryAsync(string img, string oaid = "", string ip = "", bool recommand = true)
  20. {
  21. PromotionQueryDTO result = new()
  22. {
  23. end_point = _end_point,
  24. ip = ip,
  25. oaid = oaid,
  26. img = img
  27. };
  28. if (string.IsNullOrEmpty(oaid))
  29. {
  30. result.success = false;
  31. result.message = "调用失败";
  32. result.reason = "无效OAID";
  33. _ = TkLogCore.PromotionImgLogAsync(result);
  34. return new APIResult(new
  35. {
  36. result.success,
  37. result.message,
  38. result.reason,
  39. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  40. {
  41. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  42. });
  43. }
  44. if (string.IsNullOrEmpty(img))
  45. {
  46. result.success = false;
  47. result.message = "调用失败";
  48. result.reason = "无效图片";
  49. _ = TkLogCore.PromotionImgLogAsync(result);
  50. return new APIResult(new
  51. {
  52. result.success,
  53. result.message,
  54. result.reason,
  55. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  56. {
  57. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  58. });
  59. }
  60. try
  61. {
  62. var account = TkPoolCore.GetOne(TkPoolCore.TkAction.promotionQuery);
  63. if (account == null)
  64. {
  65. result.success = false;
  66. result.message = "调用失败";
  67. result.reason = "没有匹配账号";
  68. _ = TkLogCore.PromotionImgLogAsync(result);
  69. return new APIResult(new
  70. {
  71. result.success,
  72. result.message,
  73. result.reason,
  74. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  75. {
  76. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  77. });
  78. }
  79. TaobaoOpenPlus taobao = new(account.appkey, account.appSecret, account.open_pid);
  80. int timeout = _config.similar_timeout;
  81. #if DEBUG
  82. timeout = 5000;
  83. #endif
  84. using var cts = new CancellationTokenSource();
  85. cts.CancelAfter(timeout);
  86. DateTime stime = DateTime.Now;
  87. var apiDelayTask = Task.Delay(500, cts.Token);
  88. var apiRequestTask = Task.Run(() =>
  89. {
  90. return taobao.PromotionQueryRequest(img, oaid);
  91. }, cts.Token);
  92. TbkThorMfrsPromotionQueryResponse data;
  93. var apiCompletedTask = await Task.WhenAny(apiRequestTask, apiDelayTask);
  94. if (apiCompletedTask == apiRequestTask)
  95. {
  96. data = await apiRequestTask;
  97. if (data.IsError)
  98. {
  99. result.success = false;
  100. result.message = "调用失败";
  101. result.reason = data.ErrMsg;
  102. _ = TkLogCore.PromotionImgLogAsync(result);
  103. return new APIResult(new
  104. {
  105. result.success,
  106. result.message,
  107. result.reason,
  108. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  109. {
  110. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  111. });
  112. }
  113. if (!"0".Equals(data.Status))
  114. {
  115. result.success = false;
  116. result.message = "调用失败";
  117. result.reason = data.Desc;
  118. _ = TkLogCore.PromotionImgLogAsync(result);
  119. return new APIResult(new
  120. {
  121. result.success,
  122. result.message,
  123. result.reason,
  124. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  125. {
  126. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  127. });
  128. }
  129. }
  130. var ts = DateTime.Now - stime;
  131. result.elapsedTime2 = (int)ts.TotalMilliseconds;
  132. //result.url = data.Url;
  133. result.url = $"https://mo.m.taobao.com/etao/search_pic_detail?appKey={account.appkey}&oaid={oaid}&pid={account.open_pid}";
  134. string cookies = string.Empty;
  135. var requestTask = Task.Run(async () =>
  136. {
  137. if (recommand)
  138. {
  139. var similarPromotionTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
  140. var promotionImgTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  141. await Task.WhenAll(similarPromotionTask, promotionImgTask);
  142. (result.SimilarPromotion, cookies, result.elapsedTime3) = await similarPromotionTask;
  143. (result.PromotionImg, cookies, result.elapsedTime4) = await promotionImgTask;
  144. result.similarPromotion = result.SimilarPromotion.Convert2Json();
  145. result.promotionImg = result.PromotionImg.Convert2Json();
  146. }
  147. else
  148. {
  149. //(result.SimilarPromotion, cookies) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
  150. (result.PromotionImg, cookies, result.elapsedTime4) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  151. result.similarPromotion = result.SimilarPromotion.Convert2Json();
  152. result.promotionImg = result.PromotionImg.Convert2Json();
  153. return result;
  154. }
  155. return result;
  156. }, cts.Token);
  157. var delayTask = Task.Delay(timeout, cts.Token);
  158. var completedTask = await Task.WhenAny(requestTask, delayTask);
  159. if (completedTask == requestTask)
  160. {
  161. result = await requestTask;
  162. }
  163. else
  164. {
  165. //============================== 放弃转链-请求超时 ==============================
  166. cts.Cancel();
  167. result.success = false;
  168. result.message = "调用失败";
  169. result.reason = "请求超时";
  170. _ = TkLogCore.PromotionImgLogAsync(result);
  171. return new APIResult(new
  172. {
  173. result.success,
  174. result.message,
  175. result.reason,
  176. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  177. {
  178. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  179. });
  180. }
  181. //(result.SimilarPromotion, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies);
  182. //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies);
  183. //result.similarPromotion = result.SimilarPromotion.Convert2Json();
  184. //result.promotionImg = result.PromotionImg.Convert2Json();
  185. result.success = true;
  186. result.message = "调用成功";
  187. _ = TkLogCore.PromotionImgLogAsync(result);
  188. return new APIResult(new
  189. {
  190. result.success,
  191. result.message,
  192. result.reason,
  193. similarPromotion = result.SimilarPromotion,
  194. promotionImg = result.PromotionImg,
  195. result.url,
  196. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  197. {
  198. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  199. });
  200. }
  201. catch (Exception ex)
  202. {
  203. result.success = false;
  204. result.message = "调用失败";
  205. result.reason = ex.Message;
  206. _ = TkLogCore.PromotionImgLogAsync(result);
  207. return new APIResult(new
  208. {
  209. result.success,
  210. result.message,
  211. result.reason,
  212. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  213. {
  214. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  215. });
  216. }
  217. }
  218. }
  219. }