parse_2.cs 39 KB

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