parse_2.cs 37 KB

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