TaokeOpenCore.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using dodohold.core;
  2. using Google.Protobuf.WellKnownTypes;
  3. using System.Text.Json;
  4. using System.Threading;
  5. using Top.Api.Response;
  6. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  7. namespace molilian.core
  8. {
  9. public partial class TaokeOpenCore
  10. {
  11. static string _test_base64String = string.Empty;
  12. public TkConfigDTO _config;
  13. private static string _end_point;
  14. static TaokeOpenCore()
  15. {
  16. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  17. }
  18. public TaokeOpenCore()
  19. {
  20. _config = TkConfigCore.Get();
  21. }
  22. public async Task<(APIResult, PromotionQueryDTO)> PromotionQueryByItemIdAsync(string itemid, string oaid = "", string ip = "", string scene = "", bool recommand = true, bool debug = false, int aid = 0, bool useCouponLinkFirst = true)
  23. {
  24. PromotionQueryDTO result = new()
  25. {
  26. end_point = _end_point,
  27. ip = ip,
  28. oaid = oaid,
  29. scene = scene,
  30. itemid = itemid
  31. };
  32. if (string.IsNullOrEmpty(oaid))
  33. {
  34. result.success = false;
  35. result.message = "调用失败";
  36. result.reason = "无效OAID";
  37. _ = TkLogCore.PromotionImgLogAsync(result);
  38. return (new APIResult(new
  39. {
  40. result.success,
  41. result.message,
  42. result.reason,
  43. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  44. {
  45. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  46. }), null);
  47. }
  48. if (string.IsNullOrEmpty(itemid))
  49. {
  50. result.success = false;
  51. result.message = "调用失败";
  52. result.reason = "无效商品ID";
  53. _ = TkLogCore.PromotionImgLogAsync(result);
  54. return (new APIResult(new
  55. {
  56. result.success,
  57. result.message,
  58. result.reason,
  59. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  60. {
  61. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  62. }), null);
  63. }
  64. try
  65. {
  66. TkPoolDTO account;
  67. if (aid == 0)
  68. {
  69. account = await TkPoolCore.GetOneAsync(TkPoolCore.TkAction.promotionQuery, false, string.Empty, 0);
  70. }
  71. else
  72. {
  73. account = await TkPoolCore.GetOneAsync(aid);
  74. }
  75. if (account == null)
  76. {
  77. result.success = false;
  78. result.message = "调用失败";
  79. result.reason = "没有匹配账号";
  80. _ = TkLogCore.PromotionImgLogAsync(result);
  81. return (new APIResult(new
  82. {
  83. result.success,
  84. result.message,
  85. result.reason,
  86. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  87. {
  88. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  89. }), result);
  90. }
  91. string nodeName = ProxyNodesCore.GetSelectOne(account.nodeName);
  92. var proxy = ProxyNodesCore.GetOne(nodeName);
  93. TaobaoOpenPlus taobao = new(account.appkey, account.appSecret, account.open_pid, debug, proxy);
  94. int timeout = _config.similar_timeout;
  95. #if DEBUG
  96. timeout = 50000;
  97. #endif
  98. result.accountId = account.id;
  99. result.accountName = account.name;
  100. result.proxy_nodename = nodeName;
  101. result.proxy_node = proxy?.Address?.Host;
  102. using var cts = new CancellationTokenSource();
  103. cts.CancelAfter(timeout);
  104. DateTime stime = DateTime.Now;
  105. #if DEBUG
  106. account.api_id = 17;
  107. account.api = "aliyun";
  108. account.refpid = "mm_6695915495_3161300068_115818650332";
  109. #endif
  110. result.proxy_nodename = null;
  111. result.proxy_node = null;
  112. AliyunCore api;
  113. if (account.api_id == 0)
  114. {
  115. api = new AliyunCore(account.appkey, account.appSecret);
  116. }
  117. else
  118. {
  119. api = new AliyunCore(account.api_id);
  120. }
  121. var aliDelayTask = Task.Delay(1500, cts.Token);
  122. var aliRequestTask = api.itemSimilaritemAsync(itemid, account.open_pid, result);
  123. var aliCompletedTask = await Task.WhenAny(aliDelayTask, aliRequestTask);
  124. if (aliCompletedTask == aliRequestTask)
  125. {
  126. result = await aliRequestTask;
  127. }
  128. else
  129. {
  130. result.success = false;
  131. result.message = "调用失败";
  132. result.reason = "请求超时";
  133. }
  134. var ts2 = DateTime.Now - stime;
  135. result.elapsedTime2 = (int)ts2.TotalMilliseconds;
  136. _ = TkLogCore.PromotionImgLogAsync(result);
  137. return (new APIResult(new
  138. {
  139. result.success,
  140. result.message,
  141. result.reason,
  142. promotionImg = result.PromotionImg,
  143. exposeTracks = $"https://api.molilian.com/tracks/track?track_id=7&unique_id=",
  144. clickTracks = $"https://api.molilian.com/tracks/track?track_id=8&unique_id="
  145. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  146. {
  147. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  148. }), result);
  149. }
  150. catch (Exception ex)
  151. {
  152. result.success = false;
  153. result.message = "调用失败";
  154. result.reason = ex.Message;
  155. _ = TkLogCore.PromotionImgLogAsync(result);
  156. return (new APIResult(new
  157. {
  158. result.success,
  159. result.message,
  160. result.reason,
  161. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  162. {
  163. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  164. }), result);
  165. }
  166. }
  167. public async Task<APIResult> PromotionQueryAsync(string img, string oaid = "", string ip = "", string scene = "", bool recommand = true, bool debug = false, int aid = 0, bool useCouponLinkFirst = true)
  168. {
  169. PromotionQueryDTO result = new()
  170. {
  171. end_point = _end_point,
  172. ip = ip,
  173. oaid = oaid,
  174. scene = scene,
  175. img = img
  176. };
  177. if (string.IsNullOrEmpty(oaid))
  178. {
  179. result.success = false;
  180. result.message = "调用失败";
  181. result.reason = "无效OAID";
  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. if (string.IsNullOrEmpty(img))
  194. {
  195. result.success = false;
  196. result.message = "调用失败";
  197. result.reason = "无效图片";
  198. _ = TkLogCore.PromotionImgLogAsync(result);
  199. return new APIResult(new
  200. {
  201. result.success,
  202. result.message,
  203. result.reason,
  204. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  205. {
  206. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  207. });
  208. }
  209. try
  210. {
  211. TkPoolDTO account;
  212. if (aid == 0)
  213. {
  214. account = await TkPoolCore.GetOneAsync(TkPoolCore.TkAction.promotionQuery, false, string.Empty, 0);
  215. }
  216. else
  217. {
  218. account = await TkPoolCore.GetOneAsync(aid);
  219. }
  220. if (account == null)
  221. {
  222. result.success = false;
  223. result.message = "调用失败";
  224. result.reason = "没有匹配账号";
  225. _ = TkLogCore.PromotionImgLogAsync(result);
  226. return new APIResult(new
  227. {
  228. result.success,
  229. result.message,
  230. result.reason,
  231. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  232. {
  233. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  234. });
  235. }
  236. string nodeName = ProxyNodesCore.GetSelectOne(account.nodeName);
  237. var proxy = ProxyNodesCore.GetOne(nodeName);
  238. TaobaoOpenPlus taobao = new(account.appkey, account.appSecret, account.open_pid, debug, proxy);
  239. int timeout = _config.similar_timeout;
  240. #if DEBUG
  241. timeout = 50000;
  242. #endif
  243. result.accountId = account.id;
  244. result.accountName = account.name;
  245. result.proxy_nodename = nodeName;
  246. result.proxy_node = proxy?.Address?.Host;
  247. using var cts = new CancellationTokenSource();
  248. cts.CancelAfter(timeout);
  249. DateTime stime = DateTime.Now;
  250. #if DEBUG
  251. account.api_id = 17;
  252. account.api = "aliyun";
  253. account.refpid = "mm_6695915495_3161300068_115818650332";
  254. #endif
  255. switch (account.api)
  256. {
  257. case "veapi":
  258. case "ddx":
  259. case "other_api":
  260. if (account.api_id > 0)
  261. {
  262. result.proxy_nodename = null;
  263. result.proxy_node = null;
  264. var api = OtherApiPoolCore.Get(account.api_id);
  265. var core = new OtherApiPlus(account.name, api.key, api.sessionKey, account.api, api.api_url);
  266. core.picSimilaritem(img, ref result);
  267. var ts2 = DateTime.Now - stime;
  268. result.elapsedTime2 = (int)ts2.TotalMilliseconds;
  269. _ = TkLogCore.PromotionImgLogAsync(result);
  270. return new APIResult(new
  271. {
  272. result.success,
  273. result.message,
  274. result.reason,
  275. promotionImg = result.PromotionImg,
  276. exposeTracks = "https://api.molilian.com/tracks/track?track_id=7&unique_id=",
  277. clickTracks = "https://api.molilian.com/tracks/track?track_id=8&unique_id=",
  278. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  279. {
  280. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  281. });
  282. }
  283. break;
  284. case "aliyun":
  285. {
  286. result.proxy_nodename = null;
  287. result.proxy_node = null;
  288. AliyunCore api;
  289. if (account.api_id == 0)
  290. {
  291. api = new AliyunCore(account.appkey, account.appSecret);
  292. }
  293. else
  294. {
  295. api = new AliyunCore(account.api_id);
  296. }
  297. var aliDelayTask = Task.Delay(1500, cts.Token);
  298. var aliRequestTask = api.picSimilaritemAsync(img, account.open_pid, result, useCouponLinkFirst);
  299. var aliCompletedTask = await Task.WhenAny(aliDelayTask, aliRequestTask);
  300. if (aliCompletedTask == aliRequestTask)
  301. {
  302. result = await aliRequestTask;
  303. }
  304. else
  305. {
  306. result.success = false;
  307. result.message = "调用失败";
  308. result.reason = "请求超时";
  309. }
  310. var ts2 = DateTime.Now - stime;
  311. result.elapsedTime2 = (int)ts2.TotalMilliseconds;
  312. _ = TkLogCore.PromotionImgLogAsync(result);
  313. return new APIResult(new
  314. {
  315. result.success,
  316. result.message,
  317. result.reason,
  318. promotionImg = result.PromotionImg,
  319. exposeTracks = $"https://api.molilian.com/tracks/track?track_id=7&unique_id=",
  320. clickTracks = $"https://api.molilian.com/tracks/track?track_id=8&unique_id="
  321. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  322. {
  323. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  324. });
  325. }
  326. }
  327. var apiDelayTask = Task.Delay(500, cts.Token);
  328. var apiRequestTask = Task.Run(() =>
  329. {
  330. return taobao.PromotionQueryRequest(img, oaid);
  331. }, cts.Token);
  332. TbkThorMfrsPromotionQueryResponse data;
  333. var apiCompletedTask = await Task.WhenAny(apiRequestTask, apiDelayTask);
  334. if (apiCompletedTask == apiRequestTask)
  335. {
  336. data = await apiRequestTask;
  337. if (data.IsError)
  338. {
  339. result.success = false;
  340. result.message = "调用失败";
  341. result.reason = data.ErrMsg;
  342. _ = TkLogCore.PromotionImgLogAsync(result);
  343. return new APIResult(new
  344. {
  345. result.success,
  346. result.message,
  347. result.reason,
  348. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  349. {
  350. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  351. });
  352. }
  353. if (!"0".Equals(data.Status))
  354. {
  355. result.success = false;
  356. result.message = "调用失败";
  357. result.reason = data.Desc;
  358. _ = TkLogCore.PromotionImgLogAsync(result);
  359. return new APIResult(new
  360. {
  361. result.success,
  362. result.message,
  363. result.reason,
  364. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  365. {
  366. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  367. });
  368. }
  369. }
  370. else
  371. {
  372. }
  373. //500ms后再提交一次
  374. _ = Task.Run(() =>
  375. {
  376. return taobao.PromotionQueryRequest(img, oaid);
  377. });
  378. var ts = DateTime.Now - stime;
  379. result.elapsedTime2 = (int)ts.TotalMilliseconds;
  380. //result.url = data.Url;
  381. result.url = $"https://mo.m.taobao.com/etao/search_pic_detail?appKey={account.appkey}&oaid={oaid}&pid={account.open_pid}";
  382. string cookies = string.Empty;
  383. var requestTask = Task.Run(async () =>
  384. {
  385. if (recommand)
  386. {
  387. //DateTime stime = DateTime.Now;
  388. //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  389. //var ts = DateTime.Now - stime;
  390. //result.elapsedTime4 = (int)ts.TotalMilliseconds;
  391. var similarPromotionTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies, cts.Token);
  392. var promotionImgTask = taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  393. await Task.WhenAll(similarPromotionTask, promotionImgTask);
  394. (result.SimilarPromotion, cookies, result.elapsedTime3) = await similarPromotionTask;
  395. (result.PromotionImg, cookies, result.elapsedTime4) = await promotionImgTask;
  396. }
  397. else
  398. {
  399. result.elapsedTime3 = 1;
  400. (result.PromotionImg, cookies, result.elapsedTime4) = await taobao.GetRecommendDataWithRetry(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies, cts.Token);
  401. }
  402. result.similarPromotion = result.SimilarPromotion.Convert2Json();
  403. result.promotionImg = result.PromotionImg.Convert2Json();
  404. return result;
  405. }, cts.Token);
  406. var delayTask = Task.Delay(timeout, cts.Token);
  407. var completedTask = await Task.WhenAny(requestTask, delayTask);
  408. if (completedTask == requestTask)
  409. {
  410. result = await requestTask;
  411. }
  412. else
  413. {
  414. //============================== 放弃转链-请求超时 ==============================
  415. cts.Cancel();
  416. result.success = false;
  417. result.message = "调用失败";
  418. result.reason = "请求超时";
  419. if (!recommand) result.elapsedTime3 = 1;
  420. _ = TkLogCore.PromotionImgLogAsync(result);
  421. return new APIResult(new
  422. {
  423. result.success,
  424. result.message,
  425. result.reason,
  426. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  427. {
  428. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  429. });
  430. }
  431. //(result.SimilarPromotion, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.first, oaid, cookies);
  432. //(result.PromotionImg, cookies) = await taobao.GetRecommendData(TaobaoOpenPlus.RecommendRequestType.ori, oaid, cookies);
  433. //result.similarPromotion = result.SimilarPromotion.Convert2Json();
  434. //result.promotionImg = result.PromotionImg.Convert2Json();
  435. result.success = true;
  436. result.message = "调用成功";
  437. _ = TkLogCore.PromotionImgLogAsync(result);
  438. return new APIResult(new
  439. {
  440. result.success,
  441. result.message,
  442. result.reason,
  443. similarPromotion = result.SimilarPromotion,
  444. promotionImg = result.PromotionImg,
  445. result.url,
  446. exposeTracks = "https://api.molilian.com/tracks/track?track_id=7&unique_id=",
  447. clickTracks = "https://api.molilian.com/tracks/track?track_id=8&unique_id=",
  448. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  449. {
  450. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  451. });
  452. }
  453. catch (Exception ex)
  454. {
  455. result.success = false;
  456. result.message = "调用失败";
  457. result.reason = ex.Message;
  458. _ = TkLogCore.PromotionImgLogAsync(result);
  459. return new APIResult(new
  460. {
  461. result.success,
  462. result.message,
  463. result.reason,
  464. }, APIResultCodeEnum.OK, new JsonSerializerOptions
  465. {
  466. Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
  467. });
  468. }
  469. }
  470. }
  471. }