parse_2.cs 22 KB

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