parse_2.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. using System.Net;
  13. using TencentCloud.Cdb.V20170320.Models;
  14. using System.Security.Cryptography;
  15. namespace molilian.core
  16. {
  17. public partial class AlimamaPlus
  18. {
  19. public static async Task<TkDataDTO> testTaskAsync(TkDataDTO result, CancellationToken token)
  20. {
  21. string url = "https://www.taobao.com";
  22. try
  23. {
  24. WebClientUtility client = new WebClientUtility();
  25. await Task.Delay(5000);
  26. client.Timeout = TimeSpan.FromMilliseconds(3000);
  27. var response = await client.RequestAsync(url);
  28. var body = response.Body();
  29. }
  30. catch
  31. {
  32. result.success = false;
  33. result.message = "Error";
  34. }
  35. return result;
  36. }
  37. /// <summary>
  38. /// 提取url里面的商品id参数
  39. /// </summary>
  40. /// <param name="url"></param>
  41. /// <returns></returns>
  42. public static string? ExtractItemId(string url)
  43. {
  44. Match match = Regex.Match(url, @"(?:\?|&)(?:id|shareDetailItemId|itemIds)=(\d+)");
  45. if (match.Success)
  46. {
  47. return match.Groups[1].Value;
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// 是否淘宝链接,如果是商品链接就格式化成https://item.taobao.com/item.htm?id=****
  53. /// </summary>
  54. /// <param name="url"></param>
  55. /// <returns></returns>
  56. public static (bool, string) IsTaobaoUrl(string url)
  57. {
  58. if (url.Contains("//a.m.taobao.com/i") ||
  59. url.Contains("//internal.tt.detail.taobao.com/i"))
  60. {
  61. //第一种提取方法
  62. string itemId = url.GetContentPart(".com/i", ".htm");
  63. if (!string.IsNullOrEmpty(itemId) && long.TryParse(itemId, out long _))
  64. {
  65. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  66. return (true, resultUrl);
  67. }
  68. }
  69. //https://pages-g.m.taobao.com/wow/z/app/detail-next/item/index?x-ssr=true&id=753169018016
  70. if (url.Contains("//item.taobao.com/item.htm?") ||
  71. url.Contains("//pages-g.m.taobao.com/wow/z/app/detail-next/item/index?") ||
  72. (url.Contains("//pages-fast.m.taobao.com/wow/bz/jingmi/") && url.Contains("shareDetailItemId=")) ||
  73. url.Contains("//main.m.taobao.com/detail/index.html?") ||
  74. url.Contains("//main.m.taobao.com/security-h5-detail/home?") ||
  75. url.Contains("//h5.m.taobao.com/awp/core/detail.htm?") ||
  76. url.Contains("//new.m.taobao.com/detail.htm?") ||
  77. url.Contains("//outfliggys.m.taobao.com/app/trip/rx-travel-detail") ||
  78. url.Contains("//detail.m.tmall.com/item.htm?") ||
  79. url.Contains("//tmallx.tmall.com/app/tmallx-src/goods-detail") ||
  80. url.Contains("//detail.tmall.com/item.htm?") ||
  81. url.Contains("//internal.tt.detail.taobao.com/item.htm?") ||
  82. url.Contains("//internal.tt.detail.taobao.com/") ||
  83. url.Contains("//detail.tmall.hk/hk/item.htm?"))
  84. {
  85. string? itemId = ExtractItemId(url);
  86. if (!string.IsNullOrEmpty(itemId))
  87. {
  88. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  89. return (true, resultUrl);
  90. }
  91. }
  92. return (false, url);
  93. }
  94. /// <summary>
  95. /// 排除已知的淘客链接
  96. /// </summary>
  97. /// <param name="url"></param>
  98. /// <returns></returns>
  99. public static bool IsAffLink(string url)
  100. {
  101. if (!url.StartsWith("https://") || !url.StartsWith("https://")) url = "https://" + url;
  102. Uri uri = new(url);
  103. if (uri.Host.Equals("s.click.taobao.com", StringComparison.OrdinalIgnoreCase) ||
  104. uri.Host.Equals("uland.taobao.com", StringComparison.OrdinalIgnoreCase))
  105. {
  106. return true;
  107. }
  108. return false;
  109. }
  110. public static bool IsShortLink(string url)
  111. {
  112. if (!url.StartsWith("https://", StringComparison.OrdinalIgnoreCase) &&
  113. !url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) url = "https://" + url;
  114. Uri uri = new(url);
  115. if (uri.Host.Equals("e.tb.cn", StringComparison.OrdinalIgnoreCase) ||
  116. uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase) ||
  117. uri.Host.Equals("u.tb.cn", StringComparison.OrdinalIgnoreCase) ||
  118. uri.Host.Equals("s.tb.cn", StringComparison.OrdinalIgnoreCase))
  119. {
  120. return true;
  121. }
  122. return false;
  123. }
  124. /// <summary>
  125. /// 判断淘客的¥羊角符号
  126. /// </summary>
  127. /// <param name="content"></param>
  128. /// <returns></returns>
  129. static bool ContainsSpecialFormat(string content)
  130. {
  131. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  132. //string pattern = @"¥[a-zA-Z0-9\s]+¥";
  133. string pattern = @"[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]*";
  134. return Regex.IsMatch(content, pattern);
  135. }
  136. public TimeSpan GetRequestTimeout()
  137. {
  138. int min = (int)(_config.rt_max * 0.5);
  139. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  140. int timeout = _config.rt_max;
  141. timeout -= min;
  142. return TimeSpan.FromMilliseconds(timeout);
  143. }
  144. public TimeSpan GetRequestTimeout2(int deduction = 0)
  145. {
  146. int min = (int)(_config.rt_max * 0.5);
  147. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  148. int timeout = _config.rt_max;
  149. timeout -= deduction;
  150. if (timeout <= min) return TimeSpan.FromMilliseconds(min);
  151. return TimeSpan.FromMilliseconds(timeout);
  152. }
  153. /// <summary>
  154. /// 从http获取跳转的url
  155. /// </summary>
  156. /// <param name="url"></param>
  157. /// <returns></returns>
  158. public async Task<(bool, string)> GetDesiredUrlAsync(WebProxy proxy, string url, CancellationToken cancellationToken = default)
  159. {
  160. string result;
  161. try
  162. {
  163. if (!IsShortLink(url)) return (false, "不是淘宝短网址");
  164. if (!_account.enable_deep_url) return (true, url);
  165. string desiredUrlPattern = "var url = '(.*?)'";
  166. WebClientUtility client = new()
  167. {
  168. Proxy = proxy,
  169. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  170. };
  171. if (url.Contains("u.tb.cn"))
  172. {
  173. client.AllowAutoRedirect = false;
  174. }
  175. url = url.Replace("http://u.tb.cn", "https://u.tb.cn");
  176. #if DEBUG
  177. client.Proxy = null;
  178. var response = await client.RequestAsync(url, "GET");
  179. #else
  180. client.Timeout = GetRequestTimeout();
  181. var response = await client.RequestAsync(url, "GET", cancellationToken);
  182. #endif
  183. if (response.ResponseMessage?.StatusCode == HttpStatusCode.Found &&
  184. !string.IsNullOrEmpty(response.ResponseMessage?.Headers?.Location?.OriginalString))
  185. {
  186. string redirectUrl = response.ResponseMessage?.Headers?.Location?.OriginalString;
  187. return (true, redirectUrl);
  188. }
  189. var responseBody = response.Body();
  190. result = responseBody;
  191. if (response.Successed)
  192. {
  193. if (response.ResponseMessage.IsSuccessStatusCode)
  194. {
  195. Match match = Regex.Match(responseBody, desiredUrlPattern);
  196. if (match.Success)
  197. {
  198. result = match.Groups[1].Value; // 返回匹配的 URL
  199. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  200. if (result.StartsWith("//"))
  201. result = "https:" + result;
  202. return (true, result);
  203. }
  204. }
  205. else
  206. {
  207. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  208. }
  209. }
  210. else
  211. {
  212. throw response.ResponseException;
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. _ = new LoggerLibrary("api_error", "desiredUrl_error").Info(ex.Message, ex.StackTrace).SaveAsync();
  218. result = "网络异常";
  219. }
  220. return (false, result);
  221. }
  222. /// <summary>
  223. /// 从http获取跳转的url
  224. /// </summary>
  225. /// <param name="url"></param>
  226. /// <returns></returns>
  227. public (bool, string) GetDesiredUrl(string url)
  228. {
  229. string result;
  230. try
  231. {
  232. if (!IsShortLink(url)) return (false, "不是淘宝短网址");
  233. string desiredUrlPattern = "var url = '(.*?)'";
  234. WebClientUtility client = new()
  235. {
  236. Proxy = ProxyNodesCore.RandomOne(_account.nodeName),
  237. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  238. };
  239. #if DEBUG
  240. client.Proxy = null;
  241. #endif
  242. client.Timeout = GetRequestTimeout();
  243. var response = client.Request(url);
  244. var responseBody = response.Body();
  245. result = responseBody;
  246. if (response.Successed)
  247. {
  248. if (response.ResponseMessage.IsSuccessStatusCode)
  249. {
  250. Match match = Regex.Match(responseBody, desiredUrlPattern);
  251. if (match.Success)
  252. {
  253. result = match.Groups[1].Value; // 返回匹配的 URL
  254. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  255. return (true, result);
  256. }
  257. }
  258. else
  259. {
  260. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  261. }
  262. }
  263. else
  264. {
  265. throw response.ResponseException;
  266. }
  267. }
  268. catch (Exception ex)
  269. {
  270. result = $"{ex.Message}\n{ex.StackTrace}";
  271. }
  272. return (false, result);
  273. }
  274. public static (bool, string) GetItemUrl(string url, WebProxy proxy = null)
  275. {
  276. string result;
  277. try
  278. {
  279. if (!IsShortLink(url)) return (false, "不是淘宝短网址");
  280. string desiredUrlPattern = "var url = '(.*?)'";
  281. WebClientUtility client = new()
  282. {
  283. Proxy = proxy,
  284. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  285. };
  286. #if DEBUG
  287. client.Proxy = null;
  288. #endif
  289. var response = client.Request(url);
  290. var responseBody = response.Body();
  291. result = responseBody;
  292. if (response.Successed)
  293. {
  294. if (response.ResponseMessage.IsSuccessStatusCode)
  295. {
  296. Match match = Regex.Match(responseBody, desiredUrlPattern);
  297. if (match.Success)
  298. {
  299. result = match.Groups[1].Value; // 返回匹配的 URL
  300. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  301. return (true, result);
  302. }
  303. }
  304. else
  305. {
  306. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  307. }
  308. }
  309. else
  310. {
  311. throw response.ResponseException;
  312. }
  313. }
  314. catch (Exception ex)
  315. {
  316. result = $"{ex.Message}\n{ex.StackTrace}";
  317. }
  318. return (false, result);
  319. }
  320. private void InternalTextProcessing(string content, ref TkDataDTO result)
  321. {
  322. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
  323. bool success;
  324. string resultText;
  325. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  326. string url = GetLink(content);
  327. if (!string.IsNullOrEmpty(url))
  328. {
  329. //排除已知的淘客链接
  330. if (IsAffLink(url))
  331. {
  332. result.success = false;
  333. result.content = "排除淘客链接";
  334. result.link_type = LinkTypeEnum.other_aff;
  335. result.shortLinkurl = url;
  336. return;
  337. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  338. }
  339. //直接提取
  340. (success, resultText) = IsTaobaoUrl(url);
  341. if (success)
  342. {
  343. result.success = true;
  344. result.content = resultText;
  345. result.link_type = LinkTypeEnum.goods;
  346. result.shortLinkurl = resultText;
  347. return;
  348. //return (true, result, LinkTypeEnum.goods, result);
  349. }
  350. //从http获取跳转的url
  351. Stopwatch stopwatch = Stopwatch.StartNew();
  352. stopwatch.Start();
  353. (success, resultText) = GetDesiredUrl(url);
  354. stopwatch.Stop();
  355. // 获取执行时间
  356. result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
  357. if (success)
  358. {
  359. //排除已知的淘客链接
  360. if (IsAffLink(resultText))
  361. {
  362. result.success = false;
  363. result.content = "排除淘客链接";
  364. result.link_type = LinkTypeEnum.other_aff;
  365. result.shortLinkurl = url;
  366. return;
  367. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  368. }
  369. (success, resultText) = IsTaobaoUrl(resultText);
  370. if (success)
  371. {
  372. result.success = true;
  373. result.content = resultText;
  374. result.link_type = LinkTypeEnum.goods;
  375. result.shortLinkurl = resultText;
  376. return;
  377. //return (true, resultText, LinkTypeEnum.goods, resultText);
  378. }
  379. link_type = GetLinkType(resultText);
  380. }
  381. else
  382. {
  383. link_type = GetLinkType(url);
  384. }
  385. result.success = false;
  386. result.content = resultText;
  387. result.link_type = link_type;
  388. result.shortLinkurl = url;
  389. return;
  390. //return (false, result, link_type, url);
  391. }
  392. //判断淘客的¥羊角符号
  393. if (ContainsSpecialFormat(content))
  394. {
  395. result.success = false;
  396. result.content = "排除淘口令";
  397. result.link_type = LinkTypeEnum.other_aff;
  398. result.shortLinkurl = null;
  399. return;
  400. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  401. }
  402. //todo 临时应急 0614
  403. //return (false, "纯口令 没链接", link_type, null);
  404. result.success = false;
  405. result.content = "纯口令 没链接";
  406. result.link_type = LinkTypeEnum.other_aff;
  407. result.shortLinkurl = null;
  408. }
  409. public async Task<TkDataDTO> InternalTextProcessingAsync(string content, TkDataDTO result,
  410. CancellationToken cancellationToken = default,
  411. Dictionary<string, long> swData = null,
  412. bool preserveShortLinks = false)
  413. {
  414. bool success;
  415. string resultText;
  416. result.link_type = LinkTypeEnum.unknown;
  417. //============================== 放弃转链-其他推广链接 ==============================
  418. string url = GetLink(content);
  419. result.shortLinkurl = url;
  420. if (!string.IsNullOrEmpty(url))
  421. {
  422. bool is_aff = IsAffLink(url);
  423. if (is_aff)
  424. {
  425. result.success = false;
  426. result.message = "放弃转链";
  427. result.reason = "其他推广链接";
  428. result.subCode = TkSubCodeEnum.OtherPromo;
  429. result.link_type = LinkTypeEnum.other_aff;
  430. //result.shortLinkurl = url;
  431. return result;
  432. }
  433. (success, resultText) = IsTaobaoUrl(url);
  434. if (success)
  435. {
  436. result.success = true;
  437. result.content = resultText;
  438. result.link_type = LinkTypeEnum.goods;
  439. //result.shortLinkurl = resultText;
  440. return result;
  441. }
  442. if (preserveShortLinks && IsShortLink(url))
  443. {
  444. result.success = false;
  445. result.message = string.Empty;
  446. result.reason = string.Empty;
  447. result.content = content;
  448. result.link_type = LinkTypeEnum.baseDomain;
  449. result.elapsedTime2 = 0;
  450. return result;
  451. }
  452. Stopwatch sw = Stopwatch.StartNew();
  453. var proxy = ProxyNodesCore.RandomOne(_account.nodeName);
  454. if (_proxy != null && proxy != null && _proxy.Address != proxy.Address)
  455. {
  456. result.proxy_node = proxy?.Address?.Authority;
  457. }
  458. (success, resultText) = await GetDesiredUrlAsync(proxy, url, cancellationToken);
  459. sw.Stop();
  460. // 获取执行时间
  461. result.elapsedTime2 = (int)sw.ElapsedMilliseconds;
  462. swData?.Add("\tGetDesiredUrlAsync", sw.ElapsedMilliseconds);
  463. //============================== 放弃转链-请求超时 ==============================
  464. #if DEBUG
  465. #else
  466. if (result.elapsedTime2 >= _config.rt_max || (!success && resultText.Contains("was canceled")))
  467. {
  468. result.success = false;
  469. result.message = "放弃转链";
  470. result.reason = "请求超时";
  471. result.subCode = TkSubCodeEnum.Other;
  472. return result;
  473. }
  474. #endif
  475. if (!success && resultText.Contains("<!DOCTYPE html>"))
  476. {
  477. result.success = false;
  478. result.message = "放弃转链";
  479. result.reason = "短链解析错误";
  480. result.subCode = TkSubCodeEnum.Other;
  481. return result;
  482. }
  483. if (success)
  484. {
  485. string desiredUrl = resultText;
  486. //============================== 放弃转链-其他推广链接 ==============================
  487. bool is_aff2 = IsAffLink(desiredUrl);
  488. if (is_aff2)
  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 = url;
  496. return result;
  497. }
  498. (success, desiredUrl) = IsTaobaoUrl(desiredUrl);
  499. if (success)
  500. {
  501. result.success = true;
  502. result.content = desiredUrl;
  503. result.link_type = LinkTypeEnum.goods;
  504. //result.shortLinkurl = desiredUrl;
  505. return result;
  506. }
  507. result.link_type = GetLinkType(desiredUrl);
  508. }
  509. else
  510. {
  511. result.link_type = GetLinkType(url);
  512. if ("网络错误".Equals(resultText))
  513. {
  514. result.link_type = LinkTypeEnum.unknown;
  515. result.content = resultText;
  516. result.reason = resultText;
  517. }
  518. }
  519. if (result.content.Contains("霸下通用 web 页面-验证码"))
  520. {
  521. result.subCode = TkSubCodeEnum.Captcha;
  522. result.reason = "霸下验证码";
  523. }
  524. else
  525. {
  526. switch (result.link_type)
  527. {
  528. case LinkTypeEnum.profile:
  529. case LinkTypeEnum.live:
  530. case LinkTypeEnum.video:
  531. case LinkTypeEnum.note:
  532. case LinkTypeEnum.goods:
  533. case LinkTypeEnum.baseDomain:
  534. result.reason = string.Empty;
  535. result.content = content;
  536. break;
  537. default:
  538. result.reason = "非标准链接";
  539. result.subCode = TkSubCodeEnum.NonStdLink;
  540. break;
  541. }
  542. //if (result.link_type == LinkTypeEnum.baseDomain)
  543. //{
  544. // result.content = content;
  545. //}
  546. //else
  547. //{
  548. // result.reason = "非标准链接";
  549. // result.subCode = TkSubCodeEnum.NonStdLink;
  550. //}
  551. }
  552. result.success = false;
  553. result.message = "放弃转链";
  554. result.content = resultText;
  555. result.shortLinkurl = url;
  556. return result;
  557. //return (false, result, link_type, url);
  558. }
  559. //判断淘客的¥羊角符号
  560. bool is_aff3 = ContainsSpecialFormat(content);
  561. if (is_aff3)
  562. {
  563. result.success = false;
  564. result.message = "放弃转链";
  565. result.reason = "排除淘口令";
  566. result.subCode = TkSubCodeEnum.OtherPromo;
  567. result.link_type = LinkTypeEnum.other_aff;
  568. result.shortLinkurl = null;
  569. return result;
  570. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  571. }
  572. //todo 临时应急 0614
  573. //return (false, "纯口令 没链接", link_type, null);
  574. result.success = false;
  575. result.message = "放弃转链";
  576. result.reason = "纯口令 没链接";
  577. result.subCode = TkSubCodeEnum.OtherPromo;
  578. result.link_type = LinkTypeEnum.other_aff;
  579. result.shortLinkurl = null;
  580. return result;
  581. }
  582. private LinkTypeEnum GetLinkType(string url)
  583. {
  584. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  585. if (IsShortLink(url)) return LinkTypeEnum.baseDomain;
  586. if (url.StartsWith("https://huodong.m.taobao.com/act/talent/live.html")) return LinkTypeEnum.live;
  587. if (url.StartsWith("https://web.m.taobao.com/app/tnode/web/index")) return LinkTypeEnum.video;
  588. if (url.StartsWith("https://shop.m.taobao.com/shop/shopIndex.htm")) return LinkTypeEnum.profile;
  589. string pattern = @"^https://shop\d+(\.m)*\.taobao\.com";
  590. if (Regex.IsMatch(url, pattern))
  591. {
  592. return LinkTypeEnum.profile;
  593. }
  594. //https://shop.m.taobao.com/shop/shopIndex.htm?
  595. return LinkTypeEnum.unknown;
  596. }
  597. }
  598. }