parse_2.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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://") || !url.StartsWith("https://")) url = "https://" + url;
  113. Uri uri = new(url);
  114. if (uri.Host.Equals("e.tb.cn", StringComparison.OrdinalIgnoreCase) ||
  115. uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase) ||
  116. uri.Host.Equals("u.tb.cn", StringComparison.OrdinalIgnoreCase) ||
  117. uri.Host.Equals("s.tb.cn", StringComparison.OrdinalIgnoreCase))
  118. {
  119. return true;
  120. }
  121. return false;
  122. }
  123. /// <summary>
  124. /// 判断淘客的¥羊角符号
  125. /// </summary>
  126. /// <param name="content"></param>
  127. /// <returns></returns>
  128. static bool ContainsSpecialFormat(string content)
  129. {
  130. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  131. //string pattern = @"¥[a-zA-Z0-9\s]+¥";
  132. string pattern = @"[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]*";
  133. return Regex.IsMatch(content, pattern);
  134. }
  135. public TimeSpan GetRequestTimeout()
  136. {
  137. int min = (int)(_config.rt_max * 0.5);
  138. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  139. int timeout = _config.rt_max;
  140. timeout -= min;
  141. return TimeSpan.FromMilliseconds(timeout);
  142. }
  143. public TimeSpan GetRequestTimeout2(int deduction = 0)
  144. {
  145. int min = (int)(_config.rt_max * 0.5);
  146. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  147. int timeout = _config.rt_max;
  148. timeout -= deduction;
  149. if (timeout <= min) return TimeSpan.FromMilliseconds(min);
  150. return TimeSpan.FromMilliseconds(timeout);
  151. }
  152. /// <summary>
  153. /// 从http获取跳转的url
  154. /// </summary>
  155. /// <param name="url"></param>
  156. /// <returns></returns>
  157. public async Task<(bool, string)> GetDesiredUrlAsync(WebProxy proxy, string url, CancellationToken cancellationToken = default)
  158. {
  159. string result;
  160. try
  161. {
  162. if (!IsShortLink(url)) return (false, "不是淘宝短网址");
  163. if (!_account.enable_deep_url) return (true, url);
  164. string desiredUrlPattern = "var url = '(.*?)'";
  165. WebClientUtility client = new()
  166. {
  167. Proxy = proxy,
  168. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  169. };
  170. if (url.Contains("u.tb.cn"))
  171. {
  172. client.AllowAutoRedirect = false;
  173. }
  174. url = url.Replace("http://u.tb.cn", "https://u.tb.cn");
  175. #if DEBUG
  176. client.Proxy = null;
  177. var response = await client.RequestAsync(url, "GET");
  178. #else
  179. client.Timeout = GetRequestTimeout();
  180. var response = await client.RequestAsync(url, "GET", cancellationToken);
  181. #endif
  182. if (response.ResponseMessage?.StatusCode == HttpStatusCode.Found &&
  183. !string.IsNullOrEmpty(response.ResponseMessage?.Headers?.Location?.OriginalString))
  184. {
  185. string redirectUrl = response.ResponseMessage?.Headers?.Location?.OriginalString;
  186. return (true, redirectUrl);
  187. }
  188. var responseBody = response.Body();
  189. result = responseBody;
  190. if (response.Successed)
  191. {
  192. if (response.ResponseMessage.IsSuccessStatusCode)
  193. {
  194. Match match = Regex.Match(responseBody, desiredUrlPattern);
  195. if (match.Success)
  196. {
  197. result = match.Groups[1].Value; // 返回匹配的 URL
  198. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  199. if (result.StartsWith("//"))
  200. result = "https:" + result;
  201. return (true, result);
  202. }
  203. }
  204. else
  205. {
  206. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  207. }
  208. }
  209. else
  210. {
  211. throw response.ResponseException;
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. _ = new LoggerLibrary("api_error", "desiredUrl_error").Info(ex.Message, ex.StackTrace).SaveAsync();
  217. result = "网络异常";
  218. }
  219. return (false, result);
  220. }
  221. /// <summary>
  222. /// 从http获取跳转的url
  223. /// </summary>
  224. /// <param name="url"></param>
  225. /// <returns></returns>
  226. public (bool, string) GetDesiredUrl(string url)
  227. {
  228. string result;
  229. try
  230. {
  231. if (!IsShortLink(url)) return (false, "不是淘宝短网址");
  232. string desiredUrlPattern = "var url = '(.*?)'";
  233. WebClientUtility client = new()
  234. {
  235. Proxy = ProxyNodesCore.RandomOne(_account.nodeName),
  236. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  237. };
  238. #if DEBUG
  239. client.Proxy = null;
  240. #endif
  241. client.Timeout = GetRequestTimeout();
  242. var response = client.Request(url);
  243. var responseBody = response.Body();
  244. result = responseBody;
  245. if (response.Successed)
  246. {
  247. if (response.ResponseMessage.IsSuccessStatusCode)
  248. {
  249. Match match = Regex.Match(responseBody, desiredUrlPattern);
  250. if (match.Success)
  251. {
  252. result = match.Groups[1].Value; // 返回匹配的 URL
  253. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  254. return (true, result);
  255. }
  256. }
  257. else
  258. {
  259. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  260. }
  261. }
  262. else
  263. {
  264. throw response.ResponseException;
  265. }
  266. }
  267. catch (Exception ex)
  268. {
  269. result = $"{ex.Message}\n{ex.StackTrace}";
  270. }
  271. return (false, result);
  272. }
  273. public static (bool, string) GetItemUrl(string url, WebProxy proxy = null)
  274. {
  275. string result;
  276. try
  277. {
  278. if (!IsShortLink(url)) return (false, "不是淘宝短网址");
  279. string desiredUrlPattern = "var url = '(.*?)'";
  280. WebClientUtility client = new()
  281. {
  282. Proxy = proxy,
  283. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  284. };
  285. #if DEBUG
  286. client.Proxy = null;
  287. #endif
  288. var response = client.Request(url);
  289. var responseBody = response.Body();
  290. result = responseBody;
  291. if (response.Successed)
  292. {
  293. if (response.ResponseMessage.IsSuccessStatusCode)
  294. {
  295. Match match = Regex.Match(responseBody, desiredUrlPattern);
  296. if (match.Success)
  297. {
  298. result = match.Groups[1].Value; // 返回匹配的 URL
  299. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  300. return (true, result);
  301. }
  302. }
  303. else
  304. {
  305. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  306. }
  307. }
  308. else
  309. {
  310. throw response.ResponseException;
  311. }
  312. }
  313. catch (Exception ex)
  314. {
  315. result = $"{ex.Message}\n{ex.StackTrace}";
  316. }
  317. return (false, result);
  318. }
  319. private void InternalTextProcessing(string content, ref TkDataDTO result)
  320. {
  321. //(result.success, result.content, result.link_type, result.shortLinkurl) = InternalTextProcessing(content,ref result);
  322. bool success;
  323. string resultText;
  324. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  325. string url = GetLink(content);
  326. if (!string.IsNullOrEmpty(url))
  327. {
  328. //排除已知的淘客链接
  329. if (IsAffLink(url))
  330. {
  331. result.success = false;
  332. result.content = "排除淘客链接";
  333. result.link_type = LinkTypeEnum.other_aff;
  334. result.shortLinkurl = url;
  335. return;
  336. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  337. }
  338. //直接提取
  339. (success, resultText) = IsTaobaoUrl(url);
  340. if (success)
  341. {
  342. result.success = true;
  343. result.content = resultText;
  344. result.link_type = LinkTypeEnum.goods;
  345. result.shortLinkurl = resultText;
  346. return;
  347. //return (true, result, LinkTypeEnum.goods, result);
  348. }
  349. //从http获取跳转的url
  350. Stopwatch stopwatch = Stopwatch.StartNew();
  351. stopwatch.Start();
  352. (success, resultText) = GetDesiredUrl(url);
  353. stopwatch.Stop();
  354. // 获取执行时间
  355. result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
  356. if (success)
  357. {
  358. //排除已知的淘客链接
  359. if (IsAffLink(resultText))
  360. {
  361. result.success = false;
  362. result.content = "排除淘客链接";
  363. result.link_type = LinkTypeEnum.other_aff;
  364. result.shortLinkurl = url;
  365. return;
  366. //return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  367. }
  368. (success, resultText) = IsTaobaoUrl(resultText);
  369. if (success)
  370. {
  371. result.success = true;
  372. result.content = resultText;
  373. result.link_type = LinkTypeEnum.goods;
  374. result.shortLinkurl = resultText;
  375. return;
  376. //return (true, resultText, LinkTypeEnum.goods, resultText);
  377. }
  378. link_type = GetLinkType(resultText);
  379. }
  380. else
  381. {
  382. link_type = GetLinkType(url);
  383. }
  384. result.success = false;
  385. result.content = resultText;
  386. result.link_type = link_type;
  387. result.shortLinkurl = url;
  388. return;
  389. //return (false, result, link_type, url);
  390. }
  391. //判断淘客的¥羊角符号
  392. if (ContainsSpecialFormat(content))
  393. {
  394. result.success = false;
  395. result.content = "排除淘口令";
  396. result.link_type = LinkTypeEnum.other_aff;
  397. result.shortLinkurl = null;
  398. return;
  399. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  400. }
  401. //todo 临时应急 0614
  402. //return (false, "纯口令 没链接", link_type, null);
  403. result.success = false;
  404. result.content = "纯口令 没链接";
  405. result.link_type = LinkTypeEnum.other_aff;
  406. result.shortLinkurl = null;
  407. }
  408. public async Task<TkDataDTO> InternalTextProcessingAsync(string content, TkDataDTO result,
  409. CancellationToken cancellationToken = default,
  410. Dictionary<string, long> swData = null)
  411. {
  412. bool success;
  413. string resultText;
  414. result.link_type = LinkTypeEnum.unknown;
  415. //============================== 放弃转链-其他推广链接 ==============================
  416. string url = GetLink(content);
  417. result.shortLinkurl = url;
  418. if (!string.IsNullOrEmpty(url))
  419. {
  420. bool is_aff = IsAffLink(url);
  421. if (is_aff)
  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, resultText) = IsTaobaoUrl(url);
  432. if (success)
  433. {
  434. result.success = true;
  435. result.content = resultText;
  436. result.link_type = LinkTypeEnum.goods;
  437. //result.shortLinkurl = resultText;
  438. return result;
  439. }
  440. Stopwatch sw = Stopwatch.StartNew();
  441. var proxy = ProxyNodesCore.RandomOne(_account.nodeName);
  442. if (_proxy != null && proxy != null && _proxy.Address != proxy.Address)
  443. {
  444. result.proxy_node = proxy?.Address?.Authority;
  445. }
  446. (success, resultText) = await GetDesiredUrlAsync(proxy, url, cancellationToken);
  447. sw.Stop();
  448. // 获取执行时间
  449. result.elapsedTime2 = (int)sw.ElapsedMilliseconds;
  450. swData?.Add("\tGetDesiredUrlAsync", sw.ElapsedMilliseconds);
  451. //============================== 放弃转链-请求超时 ==============================
  452. #if DEBUG
  453. #else
  454. if (result.elapsedTime2 >= _config.rt_max || (!success && resultText.Contains("was canceled")))
  455. {
  456. result.success = false;
  457. result.message = "放弃转链";
  458. result.reason = "请求超时";
  459. result.subCode = TkSubCodeEnum.Other;
  460. return result;
  461. }
  462. #endif
  463. if (success)
  464. {
  465. string desiredUrl = resultText;
  466. //============================== 放弃转链-其他推广链接 ==============================
  467. bool is_aff2 = IsAffLink(desiredUrl);
  468. if (is_aff2)
  469. {
  470. result.success = false;
  471. result.message = "放弃转链";
  472. result.reason = "其他推广链接";
  473. result.subCode = TkSubCodeEnum.OtherPromo;
  474. result.link_type = LinkTypeEnum.other_aff;
  475. //result.shortLinkurl = url;
  476. return result;
  477. }
  478. (success, desiredUrl) = IsTaobaoUrl(desiredUrl);
  479. if (success)
  480. {
  481. result.success = true;
  482. result.content = desiredUrl;
  483. result.link_type = LinkTypeEnum.goods;
  484. //result.shortLinkurl = desiredUrl;
  485. return result;
  486. }
  487. result.link_type = GetLinkType(desiredUrl);
  488. }
  489. else
  490. {
  491. result.link_type = GetLinkType(url);
  492. if ("网络错误".Equals(resultText))
  493. {
  494. result.link_type = LinkTypeEnum.unknown;
  495. result.content = resultText;
  496. result.reason = resultText;
  497. }
  498. }
  499. if (result.content.Contains("霸下通用 web 页面-验证码"))
  500. {
  501. result.subCode = TkSubCodeEnum.Captcha;
  502. result.reason = "霸下验证码";
  503. }
  504. else
  505. {
  506. switch (result.link_type)
  507. {
  508. case LinkTypeEnum.profile:
  509. case LinkTypeEnum.live:
  510. case LinkTypeEnum.video:
  511. case LinkTypeEnum.note:
  512. case LinkTypeEnum.goods:
  513. case LinkTypeEnum.baseDomain:
  514. result.reason = string.Empty;
  515. result.content = content;
  516. break;
  517. default:
  518. result.reason = "非标准链接";
  519. result.subCode = TkSubCodeEnum.NonStdLink;
  520. break;
  521. }
  522. //if (result.link_type == LinkTypeEnum.baseDomain)
  523. //{
  524. // result.content = content;
  525. //}
  526. //else
  527. //{
  528. // result.reason = "非标准链接";
  529. // result.subCode = TkSubCodeEnum.NonStdLink;
  530. //}
  531. }
  532. result.success = false;
  533. result.message = "放弃转链";
  534. result.content = resultText;
  535. result.shortLinkurl = url;
  536. return result;
  537. //return (false, result, link_type, url);
  538. }
  539. //判断淘客的¥羊角符号
  540. bool is_aff3 = ContainsSpecialFormat(content);
  541. if (is_aff3)
  542. {
  543. result.success = false;
  544. result.message = "放弃转链";
  545. result.reason = "排除淘口令";
  546. result.subCode = TkSubCodeEnum.OtherPromo;
  547. result.link_type = LinkTypeEnum.other_aff;
  548. result.shortLinkurl = null;
  549. return result;
  550. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  551. }
  552. //todo 临时应急 0614
  553. //return (false, "纯口令 没链接", link_type, null);
  554. result.success = false;
  555. result.message = "放弃转链";
  556. result.reason = "纯口令 没链接";
  557. result.subCode = TkSubCodeEnum.OtherPromo;
  558. result.link_type = LinkTypeEnum.other_aff;
  559. result.shortLinkurl = null;
  560. return result;
  561. }
  562. private LinkTypeEnum GetLinkType(string url)
  563. {
  564. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  565. if (IsShortLink(url)) return LinkTypeEnum.baseDomain;
  566. if (url.StartsWith("https://huodong.m.taobao.com/act/talent/live.html")) return LinkTypeEnum.live;
  567. if (url.StartsWith("https://web.m.taobao.com/app/tnode/web/index")) return LinkTypeEnum.video;
  568. if (url.StartsWith("https://shop.m.taobao.com/shop/shopIndex.htm")) return LinkTypeEnum.profile;
  569. string pattern = @"^https://shop\d+(\.m)*\.taobao\.com";
  570. if (Regex.IsMatch(url, pattern))
  571. {
  572. return LinkTypeEnum.profile;
  573. }
  574. //https://shop.m.taobao.com/shop/shopIndex.htm?
  575. return LinkTypeEnum.unknown;
  576. }
  577. }
  578. }