parse_2.cs 43 KB

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