EstimateRevenue.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using dodohold.core;
  2. using Sayaka.Common;
  3. using System.Drawing.Printing;
  4. using System.Text.Json;
  5. namespace molilian.core
  6. {
  7. public partial class PddUnionPlus
  8. {
  9. public async Task queryEstimateRevenueList(DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default)
  10. {
  11. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  12. string cookies = _account.cookies;
  13. bool hasNext = true;
  14. int pageNo = 1;
  15. int pageSize = 100;
  16. while (hasNext)
  17. {
  18. var args = new
  19. {
  20. startTime = startTime.ToString("yyyy-MM-dd"),
  21. endTime = endTime.ToString("yyyy-MM-dd"),
  22. orderPromotionType = 0,
  23. pageNumber = pageNo,
  24. pageSize
  25. };
  26. DateTime stime = DateTime.Now;
  27. string url = "https://jinbao.pinduoduo.com/network/api/order/stat/overall/list";
  28. var root = await this.RequestAsync(url, args.Convert2Json());
  29. TimeSpan ts2 = DateTime.Now - stime;
  30. string xx = ts2.TotalSeconds.ToString();
  31. var success = root.Read<bool>("success");
  32. if (!success) break;
  33. var list = root.ElementRead("result").ElementRead("list");
  34. string filter = "accountId=@accountId AND report_date=@report_date AND pid=@pid";
  35. var len = list.GetArrayLength();
  36. if (len == 0) break;
  37. foreach (var item in list.EnumerateArray())
  38. {
  39. var report = item.GetRawText().Convert2Object<PddEstimateRevenueDTO>();
  40. var exist = new DBContext.Table("pdd_estimate_revenue")
  41. .Fields("id")
  42. .Get<dynamic>(filter, new { report_date = report.calcDate, accountId = _account.id, report.pid });
  43. var update = new DBContext.Table("pdd_estimate_revenue")
  44. .Add("accountId", _account.id)
  45. .Add("accountName", _account.name)
  46. .Add("report_date", report.calcDate)
  47. .Add("pid", report.pid)
  48. .Add("pidName", report.pidName)
  49. .Add("orderNum", report.orderNum)
  50. .Add("orderAmount", report.orderAmount)
  51. .Add("feeAmount", report.feeAmount)
  52. .Add("avgPrice", report.avgPrice)
  53. .Add("mallRelFeeAmount", report.mallRelFeeAmount)
  54. .Add("lockFeeAmount", report.lockFeeAmount)
  55. .Add("last_time", DateTime.Now);
  56. if (exist == null)
  57. {
  58. update.Add("report_date", report.calcDate)
  59. .Add("create_time", DateTime.Now)
  60. .Create(DBContext.InsertType.REPLACE);
  61. }
  62. else
  63. {
  64. update.Where("id=@id", new { exist.id }).Update();
  65. }
  66. }
  67. pageNo++;
  68. }
  69. }
  70. public async Task<JsonElement> RequestAsync(string url, string data, CancellationToken cancellationToken = default)
  71. {
  72. string cookies = _account.cookies.Trim();
  73. string useragent = ProviderFakeUserAgent.RandomComputer;
  74. WebClientUtility client = new WebClientUtility();
  75. client.Proxy = _proxy;
  76. #if DEBUG
  77. client.Proxy = null;
  78. #endif
  79. client.SetContentType("application/json");
  80. client.AddHeaders("Referer", "https://jinbao.pinduoduo.com/data/data-analysis");
  81. client.AddHeaders("Origin", "https://jinbao.pinduoduo.com");
  82. client.UserAgent = useragent;
  83. client.AddHeaders("Cookie", cookies);
  84. client.Post(data);
  85. var response = await client.RequestAsync(url, "POST", cancellationToken);
  86. string body = response.Body();
  87. var root = body.Convert2JsonElement();
  88. //string message = string.Empty;
  89. // todo 这里我会做一些 日志 处理
  90. var success = root.Read<bool>("success");
  91. if (!success)
  92. {
  93. var errorCode = root.Read<int>("errorCode");
  94. var errorMsg = root.Read<string>("errorMsg");
  95. _ = new LoggerLibrary("PddUnion", "unsuccessful").Info(url, data).Info(body).SaveAsync();
  96. throw new APIException($"{errorCode}:{errorMsg}");
  97. }
  98. return root;
  99. }
  100. }
  101. }