parse_2.cs 39 KB

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