parse_2.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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,
  50. CancellationToken cancellationToken = default,
  51. Dictionary<string, long> swData = null)
  52. {
  53. if ("veapi".Equals(_api) && _api_id > 0)
  54. {
  55. var account = VeapiPoolCore.Get(_api_id);
  56. if (account != null)
  57. {
  58. var core = new VeapiPlus(account.name, account.key, account.sessionKey);
  59. result = await core.UnionParseAsync(content, result, cancellationToken);
  60. return result;
  61. }
  62. }
  63. result = await UnionParseAsync(content, result, cancellationToken, swData);
  64. return result;
  65. }
  66. /// <summary>
  67. /// 提取url里面的id参数
  68. /// </summary>
  69. /// <param name="url"></param>
  70. /// <returns></returns>
  71. static string? ExtractItemId(string url)
  72. {
  73. Match match = Regex.Match(url, @"(?:\?|&)id=(\d+)");
  74. if (match.Success)
  75. {
  76. return match.Groups[1].Value;
  77. }
  78. return null;
  79. }
  80. /// <summary>
  81. /// 是否淘宝链接,如果是商品链接就格式化成https://item.taobao.com/item.htm?id=****
  82. /// </summary>
  83. /// <param name="url"></param>
  84. /// <returns></returns>
  85. private static (bool, string) IsTaobaoUrl(string url)
  86. {
  87. if (url.Contains("//a.m.taobao.com/i"))
  88. {
  89. //第一种提取方法
  90. string itemId = url.GetContentPart("//a.m.taobao.com/i", ".htm");
  91. if (!string.IsNullOrEmpty(itemId))
  92. {
  93. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  94. return (true, resultUrl);
  95. }
  96. }
  97. if (url.Contains("//item.taobao.com/item.htm?") ||
  98. url.Contains("//h5.m.taobao.com/awp/core/detail.htm?") ||
  99. url.Contains("//new.m.taobao.com/detail.htm?") ||
  100. url.Contains("//outfliggys.m.taobao.com/app/trip/rx-travel-detail") ||
  101. url.Contains("//detail.m.tmall.com/item.htm?") ||
  102. url.Contains("//tmallx.tmall.com/app/tmallx-src/goods-detail") ||
  103. url.Contains("//detail.tmall.com/item.htm?") ||
  104. url.Contains("//detail.tmall.hk/hk/item.htm?"))
  105. {
  106. string? itemId = ExtractItemId(url);
  107. if (!string.IsNullOrEmpty(itemId))
  108. {
  109. string resultUrl = $"https://item.taobao.com/item.htm?id={itemId}";
  110. return (true, resultUrl);
  111. }
  112. }
  113. return (false, url);
  114. }
  115. /// <summary>
  116. /// 排除已知的淘客链接
  117. /// </summary>
  118. /// <param name="url"></param>
  119. /// <returns></returns>
  120. static bool IsAffLink(string url)
  121. {
  122. if (!url.StartsWith("https://") || !url.StartsWith("https://")) url = "https://" + url;
  123. Uri uri = new(url);
  124. if (uri.Host.Equals("s.click.taobao.com", StringComparison.OrdinalIgnoreCase) ||
  125. uri.Host.Equals("uland.taobao.com", StringComparison.OrdinalIgnoreCase))
  126. {
  127. return true;
  128. }
  129. //if (uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase) &&
  130. // uri.AbsolutePath.StartsWith("/h.") &&
  131. // string.IsNullOrEmpty(uri.Query) && // No query parameters
  132. // !url.Contains("?tk=")) // No "tk" parameter
  133. //{
  134. // return true;
  135. //}
  136. return false;
  137. }
  138. /// <summary>
  139. /// 判断淘客的¥羊角符号
  140. /// </summary>
  141. /// <param name="content"></param>
  142. /// <returns></returns>
  143. static bool ContainsSpecialFormat(string content)
  144. {
  145. // 使用正则表达式匹配内容中是否含有特定格式的字符串
  146. //string pattern = @"¥[a-zA-Z0-9\s]+¥";
  147. string pattern = @"[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]+[a-zA-Z0-9\s]+[$¥🗝₴€₤£₳✔《💰🔑🎵✔️💲🔐📲₡]*";
  148. return Regex.IsMatch(content, pattern);
  149. }
  150. public TimeSpan GetRequestTimeout(int deduction = 0)
  151. {
  152. #if DEBUG
  153. //return TimeSpan.FromMilliseconds(10 * 1000);
  154. #endif
  155. int min = (int)(_config.rt_max * 0.3);
  156. if (_config.rt_max == 0) return TimeSpan.FromMilliseconds(1000);
  157. int timeout = _config.rt_max;
  158. if (deduction == 0)
  159. {
  160. timeout -= min;
  161. return TimeSpan.FromMilliseconds(timeout);
  162. }
  163. timeout -= deduction;
  164. if (timeout <= min) return TimeSpan.FromMilliseconds(1);
  165. return TimeSpan.FromMilliseconds(min);
  166. }
  167. /// <summary>
  168. /// 从http获取跳转的url
  169. /// </summary>
  170. /// <param name="url"></param>
  171. /// <returns></returns>
  172. async Task<(bool, string)> GetDesiredUrlAsync(string url, CancellationToken cancellationToken = default)
  173. {
  174. string result;
  175. try
  176. {
  177. Uri uri = new(url);
  178. if (!uri.Host.Equals("m.tb.cn", StringComparison.OrdinalIgnoreCase)) return (false, "不是淘宝短网址");
  179. string desiredUrlPattern = "var url = '(.*?)'";
  180. WebClientUtility client = new()
  181. {
  182. Proxy = _proxy,
  183. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  184. };
  185. #if DEBUG
  186. client.Proxy = null;
  187. #endif
  188. client.Timeout = GetRequestTimeout(0);
  189. var response = await client.RequestAsync(url, "GET", cancellationToken);
  190. var responseBody = response.Body();
  191. result = responseBody;
  192. if (response.Successed)
  193. {
  194. if (response.ResponseMessage.IsSuccessStatusCode)
  195. {
  196. Match match = Regex.Match(responseBody, desiredUrlPattern);
  197. if (match.Success)
  198. {
  199. result = match.Groups[1].Value; // 返回匹配的 URL
  200. if (string.IsNullOrEmpty(result)) return (false, "没有匹配的URL");
  201. return (true, result);
  202. }
  203. }
  204. else
  205. {
  206. result = $"{response.ResponseMessage.StatusCode}\n{responseBody}";
  207. }
  208. }
  209. else
  210. {
  211. throw response.ResponseException;
  212. }
  213. }
  214. catch (Exception ex)
  215. {
  216. _ = new LoggerLibrary("api_error", "desiredUrl_error").Info(ex.Message, ex.StackTrace).SaveAsync();
  217. result = $"{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. bool success;
  368. string resultText;
  369. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  370. //============================== 放弃转链-其他推广链接 ==============================
  371. string url = GetLink(content);
  372. if (!string.IsNullOrEmpty(url))
  373. {
  374. bool is_aff = IsAffLink(url);
  375. if (is_aff)
  376. {
  377. result.success = false;
  378. result.message = "放弃转链";
  379. result.reason = "其他推广链接";
  380. result.link_type = LinkTypeEnum.other_aff;
  381. result.shortLinkurl = url;
  382. return result;
  383. }
  384. (success, resultText) = IsTaobaoUrl(url);
  385. if (success)
  386. {
  387. result.success = true;
  388. result.content = resultText;
  389. result.link_type = LinkTypeEnum.goods;
  390. result.shortLinkurl = resultText;
  391. return result;
  392. }
  393. Stopwatch sw = Stopwatch.StartNew();
  394. (success, resultText) = await GetDesiredUrlAsync(url, cancellationToken);
  395. sw.Stop();
  396. // 获取执行时间
  397. result.elapsedTime2 = (int)sw.ElapsedMilliseconds;
  398. swData?.Add("\tGetDesiredUrlAsync", sw.ElapsedMilliseconds);
  399. //============================== 放弃转链-请求超时 ==============================
  400. if (result.elapsedTime2 >= _config.rt_max || (!success && resultText.Contains("was canceled")))
  401. {
  402. result.success = false;
  403. result.message = "放弃转链";
  404. result.reason = "请求超时";
  405. return result;
  406. }
  407. if (success)
  408. {
  409. string desiredUrl = resultText;
  410. //============================== 放弃转链-其他推广链接 ==============================
  411. bool is_aff2 = IsAffLink(desiredUrl);
  412. if (is_aff2)
  413. {
  414. result.success = false;
  415. result.message = "放弃转链";
  416. result.reason = "其他推广链接";
  417. result.link_type = LinkTypeEnum.other_aff;
  418. result.shortLinkurl = url;
  419. return result;
  420. }
  421. (success, desiredUrl) = IsTaobaoUrl(desiredUrl);
  422. if (success)
  423. {
  424. result.success = true;
  425. result.content = desiredUrl;
  426. result.link_type = LinkTypeEnum.goods;
  427. result.shortLinkurl = desiredUrl;
  428. return result;
  429. }
  430. }
  431. link_type = GetLinkType(url);
  432. result.success = false;
  433. result.message = "放弃转链";
  434. result.reason = "非标准链接";
  435. result.content = resultText;
  436. result.link_type = link_type;
  437. result.shortLinkurl = url;
  438. return result;
  439. //return (false, result, link_type, url);
  440. }
  441. //判断淘客的¥羊角符号
  442. bool is_aff3 = ContainsSpecialFormat(content);
  443. if (is_aff3)
  444. {
  445. result.success = false;
  446. result.message = "放弃转链";
  447. result.reason = "排除淘口令";
  448. result.link_type = LinkTypeEnum.other_aff;
  449. result.shortLinkurl = null;
  450. return result;
  451. //return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  452. }
  453. //todo 临时应急 0614
  454. //return (false, "纯口令 没链接", link_type, null);
  455. result.success = false;
  456. result.message = "放弃转链";
  457. result.reason = "纯口令 没链接";
  458. result.link_type = LinkTypeEnum.other_aff;
  459. result.shortLinkurl = null;
  460. return result;
  461. }
  462. private (bool, string, LinkTypeEnum, string) InternalTextProcessing222(string content)
  463. {
  464. bool success;
  465. string result;
  466. LinkTypeEnum link_type = LinkTypeEnum.unknown;
  467. string url = GetLink(content);
  468. if (!string.IsNullOrEmpty(url))
  469. {
  470. //排除已知的淘客链接
  471. if (IsAffLink(url)) return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  472. //直接提取
  473. (success, result) = IsTaobaoUrl(url);
  474. if (success) return (true, result, LinkTypeEnum.goods, result);
  475. //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
  476. //从http获取跳转的url
  477. (success, result) = GetDesiredUrl(url);
  478. if (success)
  479. {
  480. //排除已知的淘客链接
  481. if (IsAffLink(result)) return (false, "排除淘客链接", LinkTypeEnum.other_aff, url);
  482. (success, result) = IsTaobaoUrl(result);
  483. if (success) return (true, result, LinkTypeEnum.goods, result);
  484. link_type = GetLinkType(result);
  485. }
  486. else
  487. {
  488. link_type = GetLinkType(url);
  489. }
  490. return (false, result, link_type, url);
  491. }
  492. //判断淘客的¥羊角符号
  493. if (ContainsSpecialFormat(content)) return (false, "排除淘口令", LinkTypeEnum.other_aff, null);
  494. //todo 临时应急 0614
  495. //return (false, "纯口令 没链接", link_type, null);
  496. return (false, "纯口令 没链接", LinkTypeEnum.other_aff, null);
  497. }
  498. private LinkTypeEnum GetLinkType(string url)
  499. {
  500. if (string.IsNullOrEmpty(url)) return LinkTypeEnum.unknown;
  501. if (url.StartsWith("https://huodong.m.taobao.com/act/talent/live.html")) return LinkTypeEnum.live;
  502. if (url.StartsWith("https://web.m.taobao.com/app/tnode/web/index")) return LinkTypeEnum.video;
  503. string pattern = @"^https://shop\d+\.m\.taobao\.com";
  504. if (Regex.IsMatch(url, pattern))
  505. {
  506. return LinkTypeEnum.profile;
  507. }
  508. return LinkTypeEnum.unknown;
  509. }
  510. private async Task<TkDataDTO> alimamaParseAsync(string content, TkDataDTO result, CancellationToken cancellationToken = default)
  511. {
  512. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  513. Random random = new();
  514. double randomNumber = random.NextDouble();
  515. string randomString = randomNumber.ToString()[2..];
  516. string cna = _cookies.GetContentPart("cna=", ";");
  517. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  518. var variableMap = new
  519. {
  520. url = content,
  521. union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  522. lensScene = "PUB",
  523. spmB = "_portal_v2_tool_links_page_home_index_htm"
  524. }.Convert2Json(true).UrlEncode();
  525. variableMap = variableMap.Replace(" ", "%20");
  526. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  527. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  528. Stopwatch stopwatch = Stopwatch.StartNew();
  529. stopwatch.Start();
  530. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  531. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  532. .AddHeaders("Cookie", _cookies);
  533. client.Proxy = _proxy;
  534. #if DEBUG
  535. client.Proxy = null;
  536. #endif
  537. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  538. client.Timeout = GetRequestTimeout(result.elapsedTime2);
  539. //var response = client.Request(url);
  540. var response = await client.RequestAsync(url, "GET", cancellationToken);
  541. stopwatch.Stop();
  542. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  543. var body = string.Empty;
  544. try
  545. {
  546. if (!response.Successed)
  547. {
  548. result.channel = TkChannelEnum.tb;
  549. result.link_type = LinkTypeEnum.unknown;
  550. result.success = false;
  551. result.message = "fail";
  552. if (response.ResponseException != null)
  553. {
  554. _ = new LoggerLibrary("api_error", "fail")
  555. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  556. .SaveAsync();
  557. throw response.ResponseException;
  558. }
  559. return result;
  560. }
  561. if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.Found)
  562. {
  563. _ = new LoggerLibrary("api_error", "fail")
  564. .Info($"302:{response.ResponseMessage.Headers.Location}")
  565. .SaveAsync();
  566. throw new Exception("302 Found, was canceled");
  567. }
  568. JsonElement root;
  569. try
  570. {
  571. body = response.Body();
  572. root = body.Convert2JsonElement();
  573. }
  574. catch (Exception ex)
  575. {
  576. throw new Exception(body);
  577. }
  578. bool success = root.Read<bool>("success", false);
  579. string message = root.Read("message", string.Empty);
  580. string info_message = root.PathRead("info.message", string.Empty);
  581. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  582. string taoToken = root.PathRead("data.taoToken", string.Empty);
  583. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  584. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  585. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  586. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  587. string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
  588. string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
  589. string itemId = root.PathRead("data.itemId", string.Empty);
  590. string itemName = root.PathRead("data.itemName", string.Empty);
  591. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  592. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  593. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  594. string pic = root.PathRead("data.pic", string.Empty);
  595. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  596. if (response.ResponseMessage != null)
  597. {
  598. switch (response.ResponseMessage.StatusCode)
  599. {
  600. case System.Net.HttpStatusCode.OK:
  601. {
  602. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  603. {
  604. taoToken = couponLinkTaoToken;
  605. shortLinkurl = couponShortLinkUrl;
  606. }
  607. string shortLink = GetLink(taoToken);
  608. content = ReplaceUrls(content, shortLink);
  609. }
  610. break;
  611. case System.Net.HttpStatusCode.Found:
  612. string location = response.ResponseMessage.Headers.Location.OriginalString;
  613. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  614. {
  615. success = false;
  616. message = "nologin";
  617. }
  618. break;
  619. default:
  620. {
  621. success = false;
  622. message = "other";
  623. }
  624. break;
  625. }
  626. }
  627. if (!success)
  628. {
  629. shortLinkurl = GetLink(content);
  630. switch (message)
  631. {
  632. case "该商品已经下架或未加入淘宝客":
  633. case "该链接不支持转化,请更换链接尝试":
  634. case "网络错误":
  635. break;
  636. default:
  637. var _headers = response.ResponseMessage.Headers;
  638. var headers = "";
  639. foreach (var h in _headers)
  640. {
  641. foreach (var Value in h.Value)
  642. {
  643. headers += $"{h.Key}: {Value}\n";
  644. }
  645. }
  646. _ = new LoggerLibrary("api_error", "fail")
  647. .Info(message, content)
  648. .Info(headers, body)
  649. .SaveAsync();
  650. break;
  651. }
  652. }
  653. string deeplink_url = GetDeeplink(shortLinkurl);
  654. result.channel = TkChannelEnum.tb;
  655. result.link_type = success ? LinkTypeEnum.goods : result.link_type;
  656. result.success = success;
  657. result.message = message;
  658. result.content = content;
  659. result.couponAmount = couponAmount;
  660. result.couponEffectiveEndTime = couponEffectiveEndTime;
  661. result.couponEffectiveStartTime = couponEffectiveStartTime;
  662. result.taoToken = taoToken;
  663. result.shortLinkurl = shortLinkurl;
  664. result.deeplink_url = deeplink_url;
  665. result.itemId = itemId;
  666. result.itemName = itemName;
  667. result.promotionPrice = promotionPrice;
  668. result.sellerNickName = sellerNickName;
  669. result.shopTitle = shopTitle;
  670. result.pic = pic;
  671. result.qrCodeUrl = qrCodeUrl;
  672. }
  673. catch
  674. {
  675. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  676. {
  677. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  678. {
  679. throw new APIException("抱歉!页面无法访问……");
  680. }
  681. }
  682. throw;
  683. }
  684. return result;
  685. }
  686. private TkDataDTO alimamaParse(string content, TkDataDTO result)
  687. {
  688. string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";
  689. Random random = new();
  690. double randomNumber = random.NextDouble();
  691. string randomString = randomNumber.ToString()[2..];
  692. string cna = _cookies.GetContentPart("cna=", ";");
  693. string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];
  694. var variableMap = new
  695. {
  696. url = content,
  697. union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
  698. lensScene = "PUB",
  699. spmB = "_portal_v2_tool_links_page_home_index_htm"
  700. }.Convert2Json(true).UrlEncode();
  701. variableMap = variableMap.Replace(" ", "%20");
  702. string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  703. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  704. Stopwatch stopwatch = Stopwatch.StartNew();
  705. stopwatch.Start();
  706. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  707. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  708. .AddHeaders("Cookie", _cookies);
  709. client.Proxy = _proxy;
  710. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  711. #if DEBUG
  712. client.Proxy = null;
  713. url = "https://47.246.177.226/openapi/param2/1/gateway.unionpub/xt.entry.json?" +
  714. $"t={t}&_tb_token_={_tb_token}&floorId={_floorId}&refpid={_refpid}&variableMap={variableMap}";
  715. client.Headers["Host"] = "pub.alimama.com";
  716. #endif
  717. client.Timeout = GetRequestTimeout(result.elapsedTime2);
  718. //var response = client.Request(url);
  719. var response = client.Request(url);
  720. stopwatch.Stop();
  721. result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;
  722. var body = string.Empty;
  723. try
  724. {
  725. if (!response.Successed)
  726. {
  727. result.channel = TkChannelEnum.tb;
  728. result.accountId = _accountId;
  729. result.accountName = _accountName;
  730. result.link_type = LinkTypeEnum.unknown;
  731. result.success = false;
  732. result.message = "fail";
  733. if (response.ResponseException != null)
  734. {
  735. _ = new LoggerLibrary("api_error", "fail")
  736. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  737. .SaveAsync();
  738. throw response.ResponseException;
  739. }
  740. return result;
  741. }
  742. body = response.Body();
  743. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  744. JsonElement root = body.Convert2JsonElement();
  745. bool success = root.Read<bool>("success", false);
  746. string message = root.Read("message", string.Empty);
  747. string info_message = root.PathRead("info.message", string.Empty);
  748. if (!string.IsNullOrEmpty(info_message)) message = info_message;
  749. string taoToken = root.PathRead("data.taoToken", string.Empty);
  750. string shortLinkurl = root.PathRead("data.shortLinkurl", string.Empty);
  751. string couponLinkTaoToken = root.PathRead("data.couponLinkTaoToken", string.Empty);
  752. string couponShortLinkUrl = root.PathRead("data.couponShortLinkUrl", string.Empty);
  753. decimal couponAmount = root.PathRead<decimal>("data.couponAmount", 0);
  754. string couponEffectiveEndTime = root.PathRead("data.couponEffectiveEndTime", string.Empty);
  755. string couponEffectiveStartTime = root.PathRead("data.couponEffectiveStartTime", string.Empty);
  756. string itemId = root.PathRead("data.itemId", string.Empty);
  757. string itemName = root.PathRead("data.itemName", string.Empty);
  758. decimal promotionPrice = root.PathRead<decimal>("data.promotionPrice", 0);
  759. string sellerNickName = root.PathRead("data.sellerNickName", string.Empty);
  760. string shopTitle = root.PathRead("data.shopTitle", string.Empty);
  761. string pic = root.PathRead("data.pic", string.Empty);
  762. string qrCodeUrl = root.PathRead("data.qrCodeUrl", string.Empty);
  763. if (response.ResponseMessage != null)
  764. {
  765. switch (response.ResponseMessage.StatusCode)
  766. {
  767. case System.Net.HttpStatusCode.OK:
  768. {
  769. if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
  770. {
  771. taoToken = couponLinkTaoToken;
  772. shortLinkurl = couponShortLinkUrl;
  773. }
  774. string shortLink = GetLink(taoToken);
  775. content = ReplaceUrls(content, shortLink);
  776. }
  777. break;
  778. case System.Net.HttpStatusCode.Found:
  779. string location = response.ResponseMessage.Headers.Location.OriginalString;
  780. if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
  781. {
  782. success = false;
  783. message = "nologin";
  784. }
  785. break;
  786. default:
  787. {
  788. success = false;
  789. message = "other";
  790. }
  791. break;
  792. }
  793. }
  794. if (!success)
  795. {
  796. shortLinkurl = GetLink(content);
  797. switch (message)
  798. {
  799. case "该商品已经下架或未加入淘宝客":
  800. case "该链接不支持转化,请更换链接尝试":
  801. case "网络错误":
  802. break;
  803. default:
  804. var _headers = response.ResponseMessage.Headers;
  805. var headers = "";
  806. foreach (var h in _headers)
  807. {
  808. foreach (var Value in h.Value)
  809. {
  810. headers += $"{h.Key}: {Value}\n";
  811. }
  812. }
  813. _ = new LoggerLibrary("api_error", "fail")
  814. .Info(message, content)
  815. .Info(headers, body)
  816. .SaveAsync();
  817. break;
  818. }
  819. }
  820. if (success)
  821. {
  822. if (string.IsNullOrEmpty(itemName))
  823. {
  824. itemName = GetTitle(taoToken);
  825. }
  826. }
  827. else
  828. {
  829. itemName = GetTitle(content);
  830. }
  831. string deeplink_url = GetDeeplink(shortLinkurl);
  832. result.channel = TkChannelEnum.tb;
  833. result.accountId = _accountId;
  834. result.accountName = _accountName;
  835. if (success)
  836. {
  837. switch (result.link_type)
  838. {
  839. case LinkTypeEnum.profile:
  840. case LinkTypeEnum.goods:
  841. break;
  842. default:
  843. result.link_type = LinkTypeEnum.goods;
  844. break;
  845. }
  846. }
  847. result.success = success;
  848. result.message = message;
  849. result.content = content;
  850. result.couponAmount = couponAmount;
  851. result.couponEffectiveEndTime = couponEffectiveEndTime;
  852. result.couponEffectiveStartTime = couponEffectiveStartTime;
  853. result.taoToken = taoToken;
  854. result.shortLinkurl = shortLinkurl;
  855. result.deeplink_url = deeplink_url;
  856. result.itemId = itemId;
  857. result.itemName = itemName;
  858. result.promotionPrice = promotionPrice;
  859. result.sellerNickName = sellerNickName;
  860. result.shopTitle = shopTitle;
  861. result.pic = pic;
  862. result.qrCodeUrl = qrCodeUrl;
  863. }
  864. catch
  865. {
  866. if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
  867. {
  868. if (body.Contains("<p>抱歉!页面无法访问……</p>"))
  869. {
  870. throw new APIException("抱歉!页面无法访问……");
  871. }
  872. }
  873. throw;
  874. }
  875. return result;
  876. }
  877. }
  878. }