| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using dodohold.core;
- using Sayaka.Common;
- using System.Diagnostics;
- using System.Text.RegularExpressions;
- using System.Threading.Channels;
- using System.Web;
- namespace molilian.core
- {
- public partial class PddUnionPlus
- {
- public static string _url_pattern = @"https?://(?:[\w-]+\.)*(?:yangkeduo\.com|pinduoduo\.com)(?:/[^\s]*)?";
- public static string GetLink(string content)
- {
- if (string.IsNullOrEmpty(content)) return content;
- string result = content;
- //
- Regex regex = new(_url_pattern);
- MatchCollection matches = regex.Matches(content);
- foreach (Match m in matches.Cast<Match>())
- {
- string url = m.Value;
- return url;
- }
- return string.Empty;
- }
- public static string GetDeeplink(string url)
- {
- string result = $"pinduoduo://com.xunmeng.pinduoduo/{url}";
- return result;
- }
- private string ExtractGoodsIdUrl(string url)
- {
- var uri = new Uri(url);
- var queryParams = HttpUtility.ParseQueryString(uri.Query);
- string goodsId = queryParams["goods_id"];
- if (!string.IsNullOrEmpty(goodsId) && uri.AbsolutePath.StartsWith("/goods"))
- {
- // 保持域名和路径不变,仅保留 goods_id 参数
- var baseUrl = $"{uri.Scheme}://{uri.Host}{uri.AbsolutePath}";
- return $"{baseUrl}?goods_id={goodsId}";
- }
- return url; // 如果没有找到 goods_id 参数,则返回原始 URL
- }
- private bool IsTrackUrl(string sourceUrl)
- {
- string pattern = @"^https?://mobile\.yangkeduo\.com/goods(\d*)\.html\?ps=.*$";
- Regex regex = new Regex(pattern);
- return regex.IsMatch(sourceUrl);
- }
- /// <summary>
- /// bool:是否ps链接,string 处理后的链接
- /// </summary>
- /// <param name="sourceUrl"></param>
- /// <param name="cancellationToken"></param>
- /// <returns></returns>
- public async Task<string> GetRedirectedUrlAsync(string sourceUrl, CancellationToken cancellationToken = default)
- {
- var isTrackUrl = IsTrackUrl(sourceUrl);
- if (!isTrackUrl) return ExtractGoodsIdUrl(sourceUrl);
- string redirectedUrl = sourceUrl;
- try
- {
- string useragent = ProviderFakeUserAgent.RandomMobile;
- WebClientUtility client = new()
- {
- AllowAutoRedirect = false,
- UserAgent = useragent,
- };
- client.Proxy = _proxy;
- #if DEBUG
- client.Proxy = null;
- #endif
- var response = await client.RequestAsync(sourceUrl, "GET", cancellationToken);
- if (!response.Successed) return null;
- if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.RedirectKeepVerb ||
- response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
- {
- redirectedUrl = response.ResponseMessage.Headers.Location.ToString();
- redirectedUrl = ExtractGoodsIdUrl(redirectedUrl);
- return redirectedUrl;
- }
- }
- catch (Exception ex)
- {
- }
- return sourceUrl;
- }
- public static PddDataDTO GetFormattedObject(UnionParseRequest request)
- {
- string shortLinkurl = GetLink(request.Content);
- string deeplink_url = GetDeeplink(shortLinkurl);
- var link_type = LinkTypeEnum.unknown;
- if (!string.IsNullOrEmpty(shortLinkurl) && shortLinkurl.StartsWith("https://p.pinduoduo.com/"))
- {
- link_type = LinkTypeEnum.other_aff;
- }
- return new PddDataDTO()
- {
- message = string.Empty,
- link_type = link_type,
- channel = TkChannelEnum.pdd,
- accountName = string.Empty,
- success = false,
- content = request.Content,
- ip = request.Ip,
- oaid = request.Oaid,
- elapsedTime = 0,
- itemName = "点击打开拼多多APP",
- shortLinkurl = shortLinkurl,
- deeplink_url = string.Empty,
- create_time = DateTime.Now,
- end_point = _end_point,
- parse_type = request.Type,
- };
- }
- public async Task<PddDataDTO> PddParseAsync(string content, int commerceType, PddDataDTO result, CancellationToken cancellationToken = default)
- {
- string message = "OK";
- string url = GetLink(content);
- if (string.IsNullOrEmpty(url))
- {
- result.success = false;
- result.link_type = LinkTypeEnum.unknown;
- result.channel_type = ChannelTypeEnum.pdd;
- result.message = "放弃转链";
- result.reason = "无效链接";
- result.accountId = 0;
- result.accountName = string.Empty;
- result.content = content;
- return result;
- }
- string shortLinkurl = url;
- if (result.link_type == LinkTypeEnum.other_aff)
- {
- result.success = false;
- result.link_type = LinkTypeEnum.unknown;
- result.channel_type = ChannelTypeEnum.pdd;
- result.deeplink_url = GetDeeplink(url);
- result.message = "放弃转链";
- result.reason = "其他推广链接";
- result.accountId = 0;
- result.accountName = string.Empty;
- result.content = content;
- return result;
- }
- // 移到外围了
- //if (!TestParseCore.InWhitelist(result.ip, result.oaid) && FlowControlIgnoreRequest(_config, out string reason))
- //{
- // result.success = false;
- // result.link_type = LinkTypeEnum.unknown;
- // result.channel_type = ChannelTypeEnum.pdd;
- // result.message = "放弃转链";
- // result.reason = reason;
- // result.accountId = 0;
- // result.accountName = string.Empty;
- // result.content = content;
- // result.deeplink_url = GetDeeplink(result.shortLinkurl);
- // return result;
- //}
- var isTrackUrl = IsTrackUrl(url);
- //不是跟踪链接 ps=*** 就返回
- //2024-09-26 放开数字id转链
- //if (!isTrackUrl)
- //{
- // result.success = false;
- // result.link_type = LinkTypeEnum.unknown;
- // result.channel_type = ChannelTypeEnum.pdd;
- // result.message = "放弃转链";
- // result.reason = "数字id链接";
- // result.accountId = 0;
- // result.accountName = string.Empty;
- // result.content = content;
- // return result;
- //}
- //去追踪
- Stopwatch stopwatch = Stopwatch.StartNew();
- stopwatch.Start();
- var redirectedUrl = await GetRedirectedUrlAsync(url, cancellationToken);
- stopwatch.Stop();
- if (redirectedUrl != url)
- {
- result.rawContent2 = redirectedUrl;
- url = redirectedUrl;
- result.elapsedTime2 = (int)stopwatch.ElapsedMilliseconds;
- }
- switch (commerceType)
- {
- case 1:
- case 2:
- result.success = false;
- result.message = "放弃转链";
- result.reason = $"厂商放弃{commerceType}";
- return result;
- }
- Stopwatch stopwatch2 = Stopwatch.StartNew();
- stopwatch2.Start();
- result = _account.work_mode switch
- {
- PddUnionWorkMode.SiteApi => await UnionGeneratelink(result, url, cancellationToken),
- //case PddUnionWorkMode.AffApi:
- // result = await GetPromotionByAffAsync(result, url, cancellationToken);
- // break;
- _ => await transferUrl(result, url, cancellationToken),
- };
- if (!result.success)
- {
- result.shortLinkurl = shortLinkurl;
- result.deeplink_url = GetDeeplink(shortLinkurl);
- }
- stopwatch2.Stop();
- result.elapsedTime3 = (int)stopwatch2.ElapsedMilliseconds;
- return result;
- }
- }
- }
|