TaokeOpenCore.cs 10 KB

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