PddUnionPlus.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using dodohold.core;
  2. using Sayaka.Common;
  3. using System.Diagnostics;
  4. using System.Text.RegularExpressions;
  5. using System.Threading.Channels;
  6. using System.Web;
  7. namespace molilian.core
  8. {
  9. public partial class PddUnionPlus
  10. {
  11. public static string _url_pattern = @"https?://(?:[\w-]+\.)*(?:yangkeduo\.com|pinduoduo\.com)(?:/[^\s]*)?";
  12. public static string GetLink(string content)
  13. {
  14. if (string.IsNullOrEmpty(content)) return content;
  15. string result = content;
  16. //
  17. Regex regex = new(_url_pattern);
  18. MatchCollection matches = regex.Matches(content);
  19. foreach (Match m in matches.Cast<Match>())
  20. {
  21. string url = m.Value;
  22. return url;
  23. }
  24. return string.Empty;
  25. }
  26. public static string GetDeeplink(string url)
  27. {
  28. string result = $"pinduoduo://com.xunmeng.pinduoduo/{url}";
  29. return result;
  30. }
  31. private string ExtractGoodsIdUrl(string url)
  32. {
  33. var uri = new Uri(url);
  34. var queryParams = HttpUtility.ParseQueryString(uri.Query);
  35. string goodsId = queryParams["goods_id"];
  36. if (!string.IsNullOrEmpty(goodsId) && uri.AbsolutePath.StartsWith("/goods"))
  37. {
  38. // 保持域名和路径不变,仅保留 goods_id 参数
  39. var baseUrl = $"{uri.Scheme}://{uri.Host}{uri.AbsolutePath}";
  40. return $"{baseUrl}?goods_id={goodsId}";
  41. }
  42. return url; // 如果没有找到 goods_id 参数,则返回原始 URL
  43. }
  44. private bool IsTrackUrl(string sourceUrl)
  45. {
  46. string pattern = @"^https?://mobile\.yangkeduo\.com/goods(\d*)\.html\?ps=.*$";
  47. Regex regex = new Regex(pattern);
  48. return regex.IsMatch(sourceUrl);
  49. }
  50. /// <summary>
  51. /// bool:是否ps链接,string 处理后的链接
  52. /// </summary>
  53. /// <param name="sourceUrl"></param>
  54. /// <param name="cancellationToken"></param>
  55. /// <returns></returns>
  56. public async Task<string> GetRedirectedUrlAsync(string sourceUrl, CancellationToken cancellationToken = default)
  57. {
  58. var isTrackUrl = IsTrackUrl(sourceUrl);
  59. if (!isTrackUrl) return ExtractGoodsIdUrl(sourceUrl);
  60. string redirectedUrl = sourceUrl;
  61. try
  62. {
  63. string useragent = ProviderFakeUserAgent.RandomMobile;
  64. WebClientUtility client = new()
  65. {
  66. AllowAutoRedirect = false,
  67. UserAgent = useragent,
  68. };
  69. client.Proxy = _proxy;
  70. #if DEBUG
  71. client.Proxy = null;
  72. #endif
  73. var response = await client.RequestAsync(sourceUrl, "GET", cancellationToken);
  74. if (!response.Successed) return null;
  75. if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.RedirectKeepVerb ||
  76. response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
  77. {
  78. redirectedUrl = response.ResponseMessage.Headers.Location.ToString();
  79. redirectedUrl = ExtractGoodsIdUrl(redirectedUrl);
  80. return redirectedUrl;
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. }
  86. return sourceUrl;
  87. }
  88. public static PddDataDTO GetFormattedObject(UnionParseRequest request)
  89. {
  90. switch (request.RiskStrategy)
  91. {
  92. case "tbpush":
  93. if (AlimamaPlus.IsDigitsOnly(request.Content))
  94. {
  95. request.Content = $"https://mobile.yangkeduo.com/goods.html?goods_id={request.Content}";
  96. }
  97. break;
  98. }
  99. string shortLinkurl = GetLink(request.Content);
  100. string deeplink_url = GetDeeplink(shortLinkurl);
  101. var link_type = LinkTypeEnum.unknown;
  102. if (!string.IsNullOrEmpty(shortLinkurl) && shortLinkurl.StartsWith("https://p.pinduoduo.com/"))
  103. {
  104. link_type = LinkTypeEnum.other_aff;
  105. }
  106. return new PddDataDTO()
  107. {
  108. message = string.Empty,
  109. link_type = link_type,
  110. channel = TkChannelEnum.pdd,
  111. accountName = string.Empty,
  112. success = false,
  113. content = request.Content,
  114. ip = request.Ip,
  115. oaid = request.Oaid,
  116. elapsedTime = 0,
  117. itemName = "点击打开拼多多APP",
  118. shortLinkurl = shortLinkurl,
  119. deeplink_url = string.Empty,
  120. create_time = DateTime.Now,
  121. end_point = _end_point,
  122. parse_type = request.Type,
  123. riskStrategy = request.RiskStrategy,
  124. launchScene = request.LaunchScene,
  125. };
  126. }
  127. public async Task<PddDataDTO> PddParseAsync(string content, int commerceType, PddDataDTO result, CancellationToken cancellationToken = default)
  128. {
  129. string message = "OK";
  130. string url = GetLink(content);
  131. if (string.IsNullOrEmpty(url))
  132. {
  133. result.success = false;
  134. result.link_type = LinkTypeEnum.unknown;
  135. result.channel_type = ChannelTypeEnum.pdd;
  136. result.message = "放弃转链";
  137. result.reason = "无效链接";
  138. result.accountId = 0;
  139. result.accountName = string.Empty;
  140. result.content = content;
  141. return result;
  142. }
  143. string shortLinkurl = url;
  144. if (result.link_type == LinkTypeEnum.other_aff)
  145. {
  146. result.success = false;
  147. result.link_type = LinkTypeEnum.unknown;
  148. result.channel_type = ChannelTypeEnum.pdd;
  149. result.deeplink_url = GetDeeplink(url);
  150. result.message = "放弃转链";
  151. result.reason = "其他推广链接";
  152. result.accountId = 0;
  153. result.accountName = string.Empty;
  154. result.content = content;
  155. return result;
  156. }
  157. // 移到外围了
  158. //if (!TestParseCore.InWhitelist(result.ip, result.oaid) && FlowControlIgnoreRequest(_config, out string reason))
  159. //{
  160. // result.success = false;
  161. // result.link_type = LinkTypeEnum.unknown;
  162. // result.channel_type = ChannelTypeEnum.pdd;
  163. // result.message = "放弃转链";
  164. // result.reason = reason;
  165. // result.accountId = 0;
  166. // result.accountName = string.Empty;
  167. // result.content = content;
  168. // result.deeplink_url = GetDeeplink(result.shortLinkurl);
  169. // return result;
  170. //}
  171. var isTrackUrl = IsTrackUrl(url);
  172. //不是跟踪链接 ps=*** 就返回
  173. //2024-09-26 放开数字id转链
  174. //if (!isTrackUrl)
  175. //{
  176. // result.success = false;
  177. // result.link_type = LinkTypeEnum.unknown;
  178. // result.channel_type = ChannelTypeEnum.pdd;
  179. // result.message = "放弃转链";
  180. // result.reason = "数字id链接";
  181. // result.accountId = 0;
  182. // result.accountName = string.Empty;
  183. // result.content = content;
  184. // return result;
  185. //}
  186. //去追踪
  187. Stopwatch stopwatch = Stopwatch.StartNew();
  188. stopwatch.Start();
  189. var redirectedUrl = await GetRedirectedUrlAsync(url, cancellationToken);
  190. stopwatch.Stop();
  191. if (redirectedUrl != url)
  192. {
  193. result.rawContent2 = redirectedUrl;
  194. url = redirectedUrl;
  195. result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
  196. }
  197. switch (commerceType)
  198. {
  199. case 1:
  200. case 2:
  201. result.success = false;
  202. result.message = "放弃转链";
  203. result.reason = $"厂商放弃{commerceType}";
  204. return result;
  205. }
  206. Stopwatch stopwatch2 = Stopwatch.StartNew();
  207. stopwatch2.Start();
  208. result = _account.work_mode switch
  209. {
  210. PddUnionWorkMode.SiteApi => await UnionGeneratelink(result, url, cancellationToken),
  211. //case PddUnionWorkMode.AffApi:
  212. // result = await GetPromotionByAffAsync(result, url, cancellationToken);
  213. // break;
  214. _ => await transferUrl(result, url, cancellationToken),
  215. };
  216. if (!result.success)
  217. {
  218. result.shortLinkurl = shortLinkurl;
  219. result.deeplink_url = GetDeeplink(shortLinkurl);
  220. }
  221. stopwatch2.Stop();
  222. result.elapsedTime3 = (int)stopwatch2.ElapsedMilliseconds;
  223. return result;
  224. }
  225. }
  226. }