TaokeOpenCore.cs 10 KB

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