parse_2.cs 43 KB

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