parse_2.cs 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. using dodohold.core;
  2. using System.Text.RegularExpressions;
  3. using System.Diagnostics;
  4. using System.Text.Json;
  5. using TencentCloud.Fmu.V20191213.Models;
  6. using System;
  7. using MySqlX.XDevAPI;
  8. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  9. using System.Threading;
  10. using TencentCloud.Tcm.V20210413.Models;
  11. using COSXML.Network;
  12. namespace molilian.core
  13. {
  14. public partial class AlimamaPlus
  15. {
  16. public static async Task<TkDataDTO> testTaskAsync(TkDataDTO result, CancellationToken token)
  17. {
  18. string url = "https://www.taobao.com";
  19. try
  20. {
  21. WebClientUtility client = new WebClientUtility();
  22. Thread.Sleep(5000);
  23. //await Task.Delay(5000, token); // 使用Task.Delay代替Thread.Sleep
  24. client.Timeout = TimeSpan.FromMilliseconds(3000);
  25. var response = await client.RequestAsync(url);
  26. var body = response.Body();
  27. }
  28. catch
  29. {
  30. result.success = false;
  31. result.message = "Error";
  32. }
  33. return result;
  34. }
  35. public void UnionParse2(string content, ref TkDataDTO result)
  36. {
  37. if ("veapi".Equals(_api) && _api_id > 0)
  38. {
  39. var account = VeapiPoolCore.Get(_api_id);
  40. if (account != null)
  41. {
  42. var core = new VeapiPlus(account.name, account.key, account.sessionKey);
  43. core.UnionParse(content, ref result);
  44. return;
  45. }
  46. }
  47. UnionParse(content, ref result);
  48. }
  49. public async Task<TkDataDTO> UnionParse2Async(string content, int commerceType, TkDataDTO result,
  50. CancellationToken cancellationToken = default,
  51. Dictionary<string, long> swData = null)
  52. {
  53. if ("veapi".Equals(_api) && _api_id > 0)
  54. {
  55. var account = VeapiPoolCore.Get(_api_id);
  56. if (account != null)
  57. {
  58. var core = new VeapiPlus(account.name, account.key, account.sessionKey);
  59. result = await core.UnionParseAsync(content, result, cancellationToken);
  60. return result;
  61. }
  62. }
  63. result = await UnionParseAsync(content, commerceType, result, cancellationToken, swData);
  64. return result;
  65. }
  66. /// <summary>
  67. /// 提取url里面的id参数
  68. /// </summary>
  69. /// <param name="url"></param>
  70. /// <returns></returns>
  71. public static string? ExtractItemId(string url)
  72. {
  73. Match match = Regex.Match(url, @"(?:\?|&)id=(\d+)");
  74. if (match.Success)
  75. {
  76. return match.Groups[1].Value;
  77. }
  78. return null;
  79. }
  80. /// <summary>
  81. /// 是否淘宝链接,如果是商品链接就格式化成https://item.taobao.com/item.htm?id=****
  82. /// </summary>
  83. /// <param name="url"></param>
  84. /// <returns></returns>
  85. private static (bool, string) IsTaobaoUrl(string url)
  86. {
  87. if (url.Contains("//a.m.taobao.com/i") ||
  88. url.Contains("//internal.tt.detail.taobao.com/i"))
  89. {
  90. //第一种提取方法
  91. string itemId = url.GetContentPart(".com/i", ".htm");
  92. if (!string.IsNullOrEmpty(itemId) && int.TryParse(itemId, out int _))
  93. {
  94. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  95. return (true, resultUrl);
  96. }
  97. }
  98. //https://pages-g.m.taobao.com/wow/z/app/detail-next/item/index?x-ssr=true&id=753169018016
  99. if (url.Contains("//item.taobao.com/item.htm?") ||
  100. url.Contains("//pages-g.m.taobao.com/wow/z/app/detail-next/item/index?") ||
  101. url.Contains("//main.m.taobao.com/detail/index.html?") ||
  102. url.Contains("//h5.m.taobao.com/awp/core/detail.htm?") ||
  103. url.Contains("//new.m.taobao.com/detail.htm?") ||
  104. url.Contains("//outfliggys.m.taobao.com/app/trip/rx-travel-detail") ||
  105. url.Contains("//detail.m.tmall.com/item.htm?") ||
  106. url.Contains("//tmallx.tmall.com/app/tmallx-src/goods-detail") ||
  107. url.Contains("//detail.tmall.com/item.htm?") ||
  108. url.Contains("//internal.tt.detail.taobao.com/item.htm?") ||
  109. url.Contains("//internal.tt.detail.taobao.com/") ||
  110. url.Contains("//detail.tmall.hk/hk/item.htm?"))
  111. {
  112. string? itemId = ExtractItemId(url);
  113. if (!string.IsNullOrEmpty(itemId))
  114. {
  115. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  116. return (true, resultUrl);
  117. }
  118. }
  119. return (false, url);
  120. }
  121. /// <summary>
  122. /// 排除已知的淘客链接
  123. /// </summary>
  124. /// <param name="url"></param>
  125. /// <returns></returns>
  126. public static bool IsAffLink(string url)
  127. {
  128. if (!url.StartsWith("https://") || !url.StartsWith("https://")) url = "https://" + url;
  129. Uri uri = new(url);
  130. if (uri.Host.Equals("s.click.taobao.com", StringComparison.OrdinalIgnoreCase) ||
  131. uri.Host.Equals("uland.taobao.com", StringComparison.OrdinalIgnoreCase))
  132. {
  133. return true;
  134. }
  135. return false;
  136. }
  137. /// <summary>
  138. /// 判断淘客的¥羊角符号
  139. /// </summary>
  140. /// <param name="content"></param>
  141. /// <returns></returns>
  142. static bool ContainsSpecialFormat(string content)
  143. {
  144. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  145. //string pattern = @"¥[a-zA-Z0-9\s]+¥";
  146. string pattern = @"[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]*";
  147. return Regex.IsMatch(content, pattern);
  148. }
  149. public TimeSpan GetRequestTimeout(int deduction = 0)
  150. {
  151. #if DEBUG
  152. return TimeSpan.FromMilliseconds(10 * 1000);
  153. #endif
  154. int min = (int)(_config.rt_max * 0.3);
  155. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  156. int timeout = _config.rt_max;
  157. if (deduction == 0)
  158. {
  159. timeout -= min;
  160. return TimeSpan.FromMilliseconds(timeout);
  161. }
  162. timeout -= deduction;
  163. if (timeout <= min) return TimeSpan.FromMilliseconds(1);
  164. return TimeSpan.FromMilliseconds(min);
  165. }
  166. /// <summary>
  167. /// 从http获取跳转的url
  168. /// </summary>
  169. /// <param name="url"></param>
  170. /// <returns></returns>
  171. public async Task<(bool, string)> GetDesiredUrlAsync(string url, CancellationToken cancellationToken = default)
  172. {
  173. string result;
  174. try
  175. {
  176. if (!url.Contains("m.tb.cn") && !url.Contains("e.tb.cn") && !url.Contains("s.tb.cn"))
  177. return (false, "不是淘宝短网址");
  178. if (!_account.enable_deep_url) return (true, url);
  179. string desiredUrlPattern = "var url = '(.*?)'";
  180. WebClientUtility client = new()
  181. {
  182. Proxy = ProxyNodesCore.RandomOne(_account.nodeName),
  183. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  184. };
  185. #if DEBUG
  186. client.Proxy = null;
  187. var response = await client.RequestAsync(url, "GET");
  188. #else
  189. client.Timeout = GetRequestTimeout(0);
  190. var response = await client.RequestAsync(url, "GET", cancellationToken);
  191. #endif
  192. var responseBody = response.Body();
  193. result = responseBody;
  194. if (response.Successed)
  195. {
  196. if (response.ResponseMessage.IsSuccessStatusCode)
  197. {
  198. Match match = Regex.Match(responseBody, desiredUrlPattern);
  199. if (match.Success)
  200. {
  201. result = match.Groups[1].Value; // 返回匹配的 URL
  202. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  203. if (result.StartsWith("//"))
  204. result = "https:" + result;
  205. return (true, result);
  206. }
  207. }
  208. else
  209. {
  210. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  211. }
  212. }
  213. else
  214. {
  215. throw response.ResponseException;
  216. }
  217. }
  218. catch (Exception ex)
  219. {
  220. _ = new LoggerLibrary("api_error", "desiredUrl_error").Info(ex.Message, ex.StackTrace).SaveAsync();
  221. result = $"{ex.Message}\n{ex.StackTrace}";
  222. }
  223. return (false, result);
  224. }
  225. /// <summary>
  226. /// 从http获取跳转的url
  227. /// </summary>
  228. /// <param name="url"></param>
  229. /// <returns></returns>
  230. (bool, string) GetDesiredUrl(string url)
  231. {
  232. string result;
  233. try
  234. {
  235. if (!url.Contains("m.tb.cn") && !url.Contains("e.tb.cn") && !url.Contains("s.tb.cn"))
  236. return (false, "不是淘宝短网址");
  237. string desiredUrlPattern = "var url = '(.*?)'";
  238. WebClientUtility client = new()
  239. {
  240. Proxy = ProxyNodesCore.RandomOne(_account.nodeName),
  241. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  242. };
  243. #if DEBUG
  244. client.Proxy = null;
  245. #endif
  246. client.Timeout = GetRequestTimeout(0);
  247. var response = client.Request(url);
  248. var responseBody = response.Body();
  249. result = responseBody;
  250. if (response.Successed)
  251. {
  252. if (response.ResponseMessage.IsSuccessStatusCode)
  253. {
  254. Match match = Regex.Match(responseBody, desiredUrlPattern);
  255. if (match.Success)
  256. {
  257. result = match.Groups[1].Value; // 返回匹配的 URL
  258. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  259. return (true, result);
  260. }
  261. }
  262. else
  263. {
  264. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  265. }
  266. }
  267. else
  268. {
  269. throw response.ResponseException;
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. result = $"{ex.Message}\n{ex.StackTrace}";
  275. }
  276. return (false, result);
  277. }
  278. private void InternalTextProcessing(string content, ref TkDataDTO result)
  279. {
  280. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
  281. bool success;
  282. string resultText;
  283. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  284. string url = GetLink(content);
  285. if (!string.IsNullOrEmpty(url))
  286. {
  287. //排除已知的淘客链接
  288. if (IsAffLink(url))
  289. {
  290. result.success = false;
  291. result.content = "排除淘客链接";
  292. result.link_type = LinkTypeEnum.other_aff;
  293. result.shortLinkurl = url;
  294. return;
  295. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  296. }
  297. //直接提取
  298. (success, resultText) = IsTaobaoUrl(url);
  299. if (success)
  300. {
  301. result.success = true;
  302. result.content = resultText;
  303. result.link_type = LinkTypeEnum.goods;
  304. result.shortLinkurl = resultText;
  305. return;
  306. //return (true, result, LinkTypeEnum.goods, result);
  307. }
  308. //从http获取跳转的url
  309. Stopwatch stopwatch = Stopwatch.StartNew();
  310. stopwatch.Start();
  311. (success, resultText) = GetDesiredUrl(url);
  312. stopwatch.Stop();
  313. // 获取执行时间
  314. result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
  315. if (success)
  316. {
  317. //排除已知的淘客链接
  318. if (IsAffLink(resultText))
  319. {
  320. result.success = false;
  321. result.content = "排除淘客链接";
  322. result.link_type = LinkTypeEnum.other_aff;
  323. result.shortLinkurl = url;
  324. return;
  325. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  326. }
  327. (success, resultText) = IsTaobaoUrl(resultText);
  328. if (success)
  329. {
  330. result.success = true;
  331. result.content = resultText;
  332. result.link_type = LinkTypeEnum.goods;
  333. result.shortLinkurl = resultText;
  334. return;
  335. //return (true, resultText, LinkTypeEnum.goods, resultText);
  336. }
  337. link_type = GetLinkType(resultText);
  338. }
  339. else
  340. {
  341. link_type = GetLinkType(url);
  342. }
  343. result.success = false;
  344. result.content = resultText;
  345. result.link_type = link_type;
  346. result.shortLinkurl = url;
  347. return;
  348. //return (false, result, link_type, url);
  349. }
  350. //判断淘客的¥羊角符号
  351. if (ContainsSpecialFormat(content))
  352. {
  353. result.success = false;
  354. result.content = "排除淘口令";
  355. result.link_type = LinkTypeEnum.other_aff;
  356. result.shortLinkurl = null;
  357. return;
  358. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  359. }
  360. //todo 临时应急 0614
  361. //return (false, "纯口令 没链接", link_type, null);
  362. result.success = false;
  363. result.content = "纯口令 没链接";
  364. result.link_type = LinkTypeEnum.other_aff;
  365. result.shortLinkurl = null;
  366. }
  367. public async Task<TkDataDTO> InternalTextProcessingAsync(string content, TkDataDTO result,
  368. CancellationToken cancellationToken = default,
  369. Dictionary<string, long> swData = null)
  370. {
  371. bool success;
  372. string resultText;
  373. result.link_type = LinkTypeEnum.unknown;
  374. //============================== 放弃转链-其他推广链接 ==============================
  375. string url = GetLink(content);
  376. if (!string.IsNullOrEmpty(url))
  377. {
  378. bool is_aff = IsAffLink(url);
  379. if (is_aff)
  380. {
  381. result.success = false;
  382. result.message = "放弃转链";
  383. result.reason = "其他推广链接";
  384. result.subCode = TkSubCodeEnum.OtherPromo;
  385. result.link_type = LinkTypeEnum.other_aff;
  386. result.shortLinkurl = url;
  387. return result;
  388. }
  389. (success, resultText) = IsTaobaoUrl(url);
  390. if (success)
  391. {
  392. result.success = true;
  393. result.content = resultText;
  394. result.link_type = LinkTypeEnum.goods;
  395. result.shortLinkurl = resultText;
  396. return result;
  397. }
  398. Stopwatch sw = Stopwatch.StartNew();
  399. (success, resultText) = await GetDesiredUrlAsync(url, cancellationToken);
  400. sw.Stop();
  401. // 获取执行时间
  402. result.elapsedTime2 = (int)sw.ElapsedMilliseconds;
  403. swData?.Add("\tGetDesiredUrlAsync", sw.ElapsedMilliseconds);
  404. //============================== 放弃转链-请求超时 ==============================
  405. #if DEBUG
  406. #else
  407. if (result.elapsedTime2 >= _config.rt_max || (!success && resultText.Contains("was canceled")))
  408. {
  409. result.success = false;
  410. result.message = "放弃转链";
  411. result.reason = "请求超时";
  412. result.subCode = TkSubCodeEnum.Other;
  413. return result;
  414. }
  415. #endif
  416. if (success)
  417. {
  418. string desiredUrl = resultText;
  419. //============================== 放弃转链-其他推广链接 ==============================
  420. bool is_aff2 = IsAffLink(desiredUrl);
  421. if (is_aff2)
  422. {
  423. result.success = false;
  424. result.message = "放弃转链";
  425. result.reason = "其他推广链接";
  426. result.subCode = TkSubCodeEnum.OtherPromo;
  427. result.link_type = LinkTypeEnum.other_aff;
  428. result.shortLinkurl = url;
  429. return result;
  430. }
  431. (success, desiredUrl) = IsTaobaoUrl(desiredUrl);
  432. if (success)
  433. {
  434. result.success = true;
  435. result.content = desiredUrl;
  436. result.link_type = LinkTypeEnum.goods;
  437. result.shortLinkurl = desiredUrl;
  438. return result;
  439. }
  440. result.link_type = GetLinkType(desiredUrl);
  441. }
  442. else
  443. {
  444. result.link_type = GetLinkType(url);
  445. }
  446. if (result.content.Contains("霸下通用 web 页面-验证码"))
  447. {
  448. result.subCode = TkSubCodeEnum.Captcha;
  449. result.reason = "霸下验证码";
  450. }
  451. else
  452. {
  453. switch (result.link_type)
  454. {
  455. case LinkTypeEnum.profile:
  456. case LinkTypeEnum.live:
  457. case LinkTypeEnum.video:
  458. case LinkTypeEnum.note:
  459. case LinkTypeEnum.goods:
  460. case LinkTypeEnum.baseDomain:
  461. result.reason = string.Empty;
  462. result.content = content;
  463. break;
  464. default:
  465. result.reason = "非标准链接";
  466. result.subCode = TkSubCodeEnum.NonStdLink;
  467. break;
  468. }
  469. //if (result.link_type == LinkTypeEnum.baseDomain)
  470. //{
  471. // result.content = content;
  472. //}
  473. //else
  474. //{
  475. // result.reason = "非标准链接";
  476. // result.subCode = TkSubCodeEnum.NonStdLink;
  477. //}
  478. }
  479. result.success = false;
  480. result.message = "放弃转链";
  481. result.content = resultText;
  482. result.shortLinkurl = url;
  483. return result;
  484. //return (false, result, link_type, url);
  485. }
  486. //判断淘客的¥羊角符号
  487. bool is_aff3 = ContainsSpecialFormat(content);
  488. if (is_aff3)
  489. {
  490. result.success = false;
  491. result.message = "放弃转链";
  492. result.reason = "排除淘口令";
  493. result.subCode = TkSubCodeEnum.OtherPromo;
  494. result.link_type = LinkTypeEnum.other_aff;
  495. result.shortLinkurl = null;
  496. return result;
  497. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  498. }
  499. //todo 临时应急 0614
  500. //return (false, "纯口令 没链接", link_type, null);
  501. result.success = false;
  502. result.message = "放弃转链";
  503. result.reason = "纯口令 没链接";
  504. result.subCode = TkSubCodeEnum.OtherPromo;
  505. result.link_type = LinkTypeEnum.other_aff;
  506. result.shortLinkurl = null;
  507. return result;
  508. }
  509. private LinkTypeEnum GetLinkType(string url)
  510. {
  511. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  512. if (url.Contains("m.tb.cn") || url.Contains("s.tb.cn") || url.Contains("e.tb.cn")) return LinkTypeEnum.baseDomain;
  513. if (url.StartsWith("https://huodong.m.taobao.com/act/talent/live.html")) return LinkTypeEnum.live;
  514. if (url.StartsWith("https://web.m.taobao.com/app/tnode/web/index")) return LinkTypeEnum.video;
  515. if (url.StartsWith("https://shop.m.taobao.com/shop/shopIndex.htm")) return LinkTypeEnum.profile;
  516. string pattern = @"^https://shop\d+(\.m)*\.taobao\.com";
  517. if (Regex.IsMatch(url, pattern))
  518. {
  519. return LinkTypeEnum.profile;
  520. }
  521. //https://shop.m.taobao.com/shop/shopIndex.htm?
  522. return LinkTypeEnum.unknown;
  523. }
  524. public async Task<TkDataDTO> alimamaParseAsync(string content, TkDataDTO result, CancellationToken cancellationToken = default)
  525. {
  526. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  527. Random random = new();
  528. double randomNumber = random.NextDouble();
  529. string randomString = randomNumber.ToString()[2..];
  530. string cna = _cookies.GetContentPart("cna=", ";");
  531. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  532. var variableMap = new
  533. {
  534. url = content,
  535. union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  536. lensScene = "PUB",
  537. spmB = "_portal_v2_tool_links_page_home_index_htm"
  538. }.Convert2Json(true).UrlEncode();
  539. variableMap = variableMap.Replace(" ", "%20");
  540. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  541. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  542. Stopwatch stopwatch = Stopwatch.StartNew();
  543. stopwatch.Start();
  544. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  545. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  546. .AddHeaders("Cookie", _cookies);
  547. client.Proxy = _proxy;
  548. #if DEBUG
  549. client.Proxy = null;
  550. #endif
  551. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  552. client.Timeout = GetRequestTimeout(result.elapsedTime2);
  553. //var response = client.Request(url);
  554. #if DEBUG
  555. var response = await client.RequestAsync(url, "GET");
  556. #else
  557. var response = await client.RequestAsync(url, "GET", cancellationToken);
  558. #endif
  559. stopwatch.Stop();
  560. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  561. var body = string.Empty;
  562. try
  563. {
  564. if (!response.Successed)
  565. {
  566. result.channel = TkChannelEnum.tb;
  567. result.link_type = LinkTypeEnum.unknown;
  568. result.success = false;
  569. result.message = "fail";
  570. if (response.ResponseException != null)
  571. {
  572. _ = new LoggerLibrary("api_error", "fail")
  573. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  574. .SaveAsync();
  575. throw response.ResponseException;
  576. }
  577. return result;
  578. }
  579. if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.Found)
  580. {
  581. _ = new LoggerLibrary("api_error", "fail")
  582. .Info($"302:{response.ResponseMessage.Headers.Location}")
  583. .SaveAsync();
  584. throw new Exception("302 Found, was canceled");
  585. }
  586. JsonElement root;
  587. try
  588. {
  589. body = response.Body();
  590. root = body.Convert2JsonElement();
  591. }
  592. catch (Exception ex)
  593. {
  594. _ = new LoggerLibrary("api_error", "fail")
  595. .Info(ex.Message, ex.StackTrace)
  596. .SaveAsync();
  597. if (body.Contains("https://g.alicdn.com/sd/punish/waf_block.html"))
  598. {
  599. result.channel = TkChannelEnum.tb;
  600. result.link_type = LinkTypeEnum.unknown;
  601. result.success = false;
  602. result.message = "waf_block";
  603. return result;
  604. }
  605. throw new Exception(body);
  606. }
  607. bool success = root.Read<bool>("success", false);
  608. string message = root.Read("message", string.Empty);
  609. string info_message = root.PathRead("info.message", string.Empty);
  610. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  611. string taoToken = root.PathRead("data.taoToken", string.Empty);
  612. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  613. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  614. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  615. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  616. string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
  617. string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
  618. string itemId = root.PathRead("data.itemId", string.Empty);
  619. string itemName = root.PathRead("data.itemName", string.Empty);
  620. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  621. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  622. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  623. string pic = root.PathRead("data.pic", string.Empty);
  624. if (pic.StartsWith("//")) pic = "https:" + pic;
  625. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  626. if (response.ResponseMessage != null)
  627. {
  628. switch (response.ResponseMessage.StatusCode)
  629. {
  630. case System.Net.HttpStatusCode.OK:
  631. {
  632. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  633. {
  634. taoToken = couponLinkTaoToken;
  635. shortLinkurl = couponShortLinkUrl;
  636. }
  637. string shortLink = GetLink(taoToken);
  638. content = ReplaceUrls(content, shortLink);
  639. }
  640. break;
  641. case System.Net.HttpStatusCode.Found:
  642. string location = response.ResponseMessage.Headers.Location.OriginalString;
  643. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  644. {
  645. success = false;
  646. message = "nologin";
  647. }
  648. break;
  649. default:
  650. {
  651. success = false;
  652. message = "other";
  653. }
  654. break;
  655. }
  656. }
  657. if (!success)
  658. {
  659. shortLinkurl = GetLink(content);
  660. switch (message)
  661. {
  662. case "该商品已经下架或未加入淘宝客":
  663. case "该链接不支持转化,请更换链接尝试":
  664. result.subCode = TkSubCodeEnum.NoConvert;
  665. break;
  666. case "网络错误":
  667. result.subCode = TkSubCodeEnum.NetError;
  668. break;
  669. default:
  670. var _headers = response.ResponseMessage.Headers;
  671. var headers = "";
  672. foreach (var h in _headers)
  673. {
  674. foreach (var Value in h.Value)
  675. {
  676. headers += $"{h.Key}: {Value}\n";
  677. }
  678. }
  679. _ = new LoggerLibrary("api_error", "fail")
  680. .Info(message, content)
  681. .Info(headers, body)
  682. .SaveAsync();
  683. break;
  684. }
  685. }
  686. shortLinkurl = GetLink(taoToken);
  687. string deeplink_url = GetDeeplink(shortLinkurl);
  688. result.channel = TkChannelEnum.tb;
  689. if (success)
  690. {
  691. result.subCode = TkSubCodeEnum.Success;
  692. switch (result.link_type)
  693. {
  694. case LinkTypeEnum.profile:
  695. case LinkTypeEnum.goods:
  696. case LinkTypeEnum.video:
  697. case LinkTypeEnum.live:
  698. break;
  699. default:
  700. result.link_type = LinkTypeEnum.goods;
  701. break;
  702. }
  703. }
  704. result.success = success;
  705. result.message = message;
  706. result.reason = string.Empty;
  707. result.content = content;
  708. result.couponAmount = couponAmount;
  709. result.couponEffectiveEndTime = couponEffectiveEndTime;
  710. result.couponEffectiveStartTime = couponEffectiveStartTime;
  711. result.taoToken = taoToken;
  712. result.shortLinkurl = shortLinkurl;
  713. result.deeplink_url = deeplink_url;
  714. result.itemId = itemId;
  715. result.itemName = itemName;
  716. result.promotionPrice = promotionPrice;
  717. result.sellerNickName = sellerNickName;
  718. result.shopTitle = shopTitle;
  719. result.pic = pic;
  720. result.qrCodeUrl = qrCodeUrl;
  721. }
  722. catch (Exception ex)
  723. {
  724. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  725. {
  726. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  727. {
  728. throw new APIException("抱歉!页面无法访问……");
  729. }
  730. }
  731. throw;
  732. }
  733. return result;
  734. }
  735. private TkDataDTO alimamaParse(string content, TkDataDTO result)
  736. {
  737. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  738. Random random = new();
  739. double randomNumber = random.NextDouble();
  740. string randomString = randomNumber.ToString()[2..];
  741. string cna = _cookies.GetContentPart("cna=", ";");
  742. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  743. var variableMap = new
  744. {
  745. url = content,
  746. union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  747. lensScene = "PUB",
  748. spmB = "_portal_v2_tool_links_page_home_index_htm"
  749. }.Convert2Json(true).UrlEncode();
  750. variableMap = variableMap.Replace(" ", "%20");
  751. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  752. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  753. Stopwatch stopwatch = Stopwatch.StartNew();
  754. stopwatch.Start();
  755. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  756. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  757. .AddHeaders("Cookie", _cookies);
  758. client.Proxy = _proxy;
  759. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  760. #if DEBUG
  761. client.Proxy = null;
  762. url = "https://47.246.177.226/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  763. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  764. client.Headers["Host"] = "pub.alimama.com";
  765. #endif
  766. client.Timeout = GetRequestTimeout(result.elapsedTime2);
  767. //var response = client.Request(url);
  768. var response = client.Request(url);
  769. stopwatch.Stop();
  770. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  771. var body = string.Empty;
  772. try
  773. {
  774. if (!response.Successed)
  775. {
  776. result.channel = TkChannelEnum.tb;
  777. result.accountId = _accountId;
  778. result.accountName = _accountName;
  779. result.link_type = LinkTypeEnum.unknown;
  780. result.success = false;
  781. result.message = "fail";
  782. if (response.ResponseException != null)
  783. {
  784. _ = new LoggerLibrary("api_error", "fail")
  785. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  786. .SaveAsync();
  787. throw response.ResponseException;
  788. }
  789. return result;
  790. }
  791. body = response.Body();
  792. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  793. JsonElement root = body.Convert2JsonElement();
  794. bool success = root.Read<bool>("success", false);
  795. string message = root.Read("message", string.Empty);
  796. string info_message = root.PathRead("info.message", string.Empty);
  797. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  798. string taoToken = root.PathRead("data.taoToken", string.Empty);
  799. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  800. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  801. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  802. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  803. string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
  804. string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
  805. string itemId = root.PathRead("data.itemId", string.Empty);
  806. string itemName = root.PathRead("data.itemName", string.Empty);
  807. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  808. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  809. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  810. string pic = root.PathRead("data.pic", string.Empty);
  811. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  812. if (response.ResponseMessage != null)
  813. {
  814. switch (response.ResponseMessage.StatusCode)
  815. {
  816. case System.Net.HttpStatusCode.OK:
  817. {
  818. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  819. {
  820. taoToken = couponLinkTaoToken;
  821. shortLinkurl = couponShortLinkUrl;
  822. }
  823. string shortLink = GetLink(taoToken);
  824. content = ReplaceUrls(content, shortLink);
  825. }
  826. break;
  827. case System.Net.HttpStatusCode.Found:
  828. string location = response.ResponseMessage.Headers.Location.OriginalString;
  829. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  830. {
  831. success = false;
  832. message = "nologin";
  833. }
  834. break;
  835. default:
  836. {
  837. success = false;
  838. message = "other";
  839. }
  840. break;
  841. }
  842. }
  843. if (!success)
  844. {
  845. shortLinkurl = GetLink(content);
  846. switch (message)
  847. {
  848. case "该商品已经下架或未加入淘宝客":
  849. case "该链接不支持转化,请更换链接尝试":
  850. case "网络错误":
  851. break;
  852. default:
  853. var _headers = response.ResponseMessage.Headers;
  854. var headers = "";
  855. foreach (var h in _headers)
  856. {
  857. foreach (var Value in h.Value)
  858. {
  859. headers += $"{h.Key}: {Value}\n";
  860. }
  861. }
  862. _ = new LoggerLibrary("api_error", "fail")
  863. .Info(message, content)
  864. .Info(headers, body)
  865. .SaveAsync();
  866. break;
  867. }
  868. }
  869. if (success)
  870. {
  871. if (string.IsNullOrEmpty(itemName))
  872. {
  873. itemName = GetTitle(taoToken);
  874. }
  875. }
  876. else
  877. {
  878. itemName = GetTitle(content);
  879. }
  880. string deeplink_url = GetDeeplink(shortLinkurl);
  881. result.channel = TkChannelEnum.tb;
  882. result.accountId = _accountId;
  883. result.accountName = _accountName;
  884. if (success)
  885. {
  886. switch (result.link_type)
  887. {
  888. case LinkTypeEnum.profile:
  889. case LinkTypeEnum.goods:
  890. case LinkTypeEnum.video:
  891. case LinkTypeEnum.live:
  892. break;
  893. default:
  894. result.link_type = LinkTypeEnum.goods;
  895. break;
  896. }
  897. }
  898. result.success = success;
  899. result.message = message;
  900. result.content = content;
  901. result.couponAmount = couponAmount;
  902. result.couponEffectiveEndTime = couponEffectiveEndTime;
  903. result.couponEffectiveStartTime = couponEffectiveStartTime;
  904. result.taoToken = taoToken;
  905. result.shortLinkurl = shortLinkurl;
  906. result.deeplink_url = deeplink_url;
  907. result.itemId = itemId;
  908. result.itemName = itemName;
  909. result.promotionPrice = promotionPrice;
  910. result.sellerNickName = sellerNickName;
  911. result.shopTitle = shopTitle;
  912. result.pic = pic;
  913. result.qrCodeUrl = qrCodeUrl;
  914. }
  915. catch
  916. {
  917. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  918. {
  919. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  920. {
  921. throw new APIException("抱歉!页面无法访问……");
  922. }
  923. }
  924. throw;
  925. }
  926. return result;
  927. }
  928. }
  929. }