parse_2.cs 22 KB

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