PddUnionPlus.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. string shortLinkurl = GetLink(request.Content);
  91. string deeplink_url = GetDeeplink(shortLinkurl);
  92. var link_type = LinkTypeEnum.unknown;
  93. if (!string.IsNullOrEmpty(shortLinkurl) && shortLinkurl.StartsWith("https://p.pinduoduo.com/"))
  94. {
  95. link_type = LinkTypeEnum.other_aff;
  96. }
  97. return new PddDataDTO()
  98. {
  99. message = string.Empty,
  100. link_type = link_type,
  101. channel = TkChannelEnum.pdd,
  102. accountName = string.Empty,
  103. success = false,
  104. content = request.Content,
  105. ip = request.Ip,
  106. oaid = request.Oaid,
  107. elapsedTime = 0,
  108. itemName = "点击打开拼多多APP",
  109. shortLinkurl = shortLinkurl,
  110. deeplink_url = string.Empty,
  111. create_time = DateTime.Now,
  112. end_point = _end_point,
  113. parse_type = request.Type,
  114. };
  115. }
  116. public async Task<PddDataDTO> PddParseAsync(string content, int commerceType, PddDataDTO result, CancellationToken cancellationToken = default)
  117. {
  118. string message = "OK";
  119. string url = GetLink(content);
  120. if (string.IsNullOrEmpty(url))
  121. {
  122. result.success = false;
  123. result.link_type = LinkTypeEnum.unknown;
  124. result.channel_type = ChannelTypeEnum.pdd;
  125. result.message = "放弃转链";
  126. result.reason = "无效链接";
  127. result.accountId = 0;
  128. result.accountName = string.Empty;
  129. result.content = content;
  130. return result;
  131. }
  132. string shortLinkurl = url;
  133. if (result.link_type == LinkTypeEnum.other_aff)
  134. {
  135. result.success = false;
  136. result.link_type = LinkTypeEnum.unknown;
  137. result.channel_type = ChannelTypeEnum.pdd;
  138. result.deeplink_url = GetDeeplink(url);
  139. result.message = "放弃转链";
  140. result.reason = "其他推广链接";
  141. result.accountId = 0;
  142. result.accountName = string.Empty;
  143. result.content = content;
  144. return result;
  145. }
  146. if (!TestParseCore.InWhitelist(result.ip, result.oaid) && FlowControlIgnoreRequest(_config, out string reason))
  147. {
  148. result.success = false;
  149. result.link_type = LinkTypeEnum.unknown;
  150. result.channel_type = ChannelTypeEnum.pdd;
  151. result.message = "放弃转链";
  152. result.reason = reason;
  153. result.accountId = 0;
  154. result.accountName = string.Empty;
  155. result.content = content;
  156. result.deeplink_url = GetDeeplink(result.shortLinkurl);
  157. return result;
  158. }
  159. var isTrackUrl = IsTrackUrl(url);
  160. //不是跟踪链接 ps=*** 就返回
  161. //2024-09-26 放开数字id转链
  162. //if (!isTrackUrl)
  163. //{
  164. // result.success = false;
  165. // result.link_type = LinkTypeEnum.unknown;
  166. // result.channel_type = ChannelTypeEnum.pdd;
  167. // result.message = "放弃转链";
  168. // result.reason = "数字id链接";
  169. // result.accountId = 0;
  170. // result.accountName = string.Empty;
  171. // result.content = content;
  172. // return result;
  173. //}
  174. //去追踪
  175. Stopwatch stopwatch = Stopwatch.StartNew();
  176. stopwatch.Start();
  177. var redirectedUrl = await GetRedirectedUrlAsync(url, cancellationToken);
  178. stopwatch.Stop();
  179. if (redirectedUrl != url)
  180. {
  181. result.rawContent2 = redirectedUrl;
  182. url = redirectedUrl;
  183. result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
  184. }
  185. switch (commerceType)
  186. {
  187. case 1:
  188. case 2:
  189. result.success = false;
  190. result.message = "放弃转链";
  191. result.reason = $"厂商放弃{commerceType}";
  192. return result;
  193. }
  194. Stopwatch stopwatch2 = Stopwatch.StartNew();
  195. stopwatch2.Start();
  196. result = _account.work_mode switch
  197. {
  198. PddUnionWorkMode.SiteApi => await UnionGeneratelink(result, url, cancellationToken),
  199. //case PddUnionWorkMode.AffApi:
  200. // result = await GetPromotionByAffAsync(result, url, cancellationToken);
  201. // break;
  202. _ => await transferUrl(result, url, cancellationToken),
  203. };
  204. if (!result.success)
  205. {
  206. result.shortLinkurl = shortLinkurl;
  207. result.deeplink_url = GetDeeplink(shortLinkurl);
  208. }
  209. stopwatch2.Stop();
  210. result.elapsedTime3 = (int)stopwatch2.ElapsedMilliseconds;
  211. return result;
  212. }
  213. }
  214. }