TaokeOpenCore.cs 11 KB

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