KsPoolCore.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using Google.Protobuf.WellKnownTypes;
  11. using System.Xml.Linq;
  12. namespace molilian.core
  13. {
  14. public partial class KsPoolCore
  15. {
  16. public enum KsAction
  17. {
  18. all,
  19. parse,
  20. coupon,
  21. promotionQuery
  22. }
  23. private static string _end_point;
  24. private static Dictionary<string, decimal> _incomeAmt = new();
  25. static KsPoolCore()
  26. {
  27. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  28. }
  29. private static readonly object _lockObj = new();
  30. private static IEnumerable<KsPoolDTO> _cached;
  31. public static KsPoolDTO? GetOne()
  32. {
  33. var list = List();
  34. if (!list.Any()) return null;
  35. return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  36. }
  37. public static KsPoolDTO? GetOne(KsAction action)
  38. {
  39. var list = List();
  40. if (!list.Any()) return null;
  41. return list.Where(e => IsNotExceedDailyIncomeLimit(e, action)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  42. }
  43. public static KsPoolDTO? GetOne(int id, KsAction action)
  44. {
  45. var list = List();
  46. if (!list.Any()) return null;
  47. return list.Where(e => e.id == id && IsNotExceedDailyIncomeLimit(e, action)).FirstOrDefault();
  48. }
  49. private static bool IsNotExceedDailyIncomeLimit(KsPoolDTO item, KsAction action)
  50. {
  51. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  52. {
  53. if (item.end_point != _end_point) return false;
  54. }
  55. switch (action)
  56. {
  57. case KsAction.parse:
  58. if (!item.enable_parse) return false;
  59. break;
  60. case KsAction.coupon:
  61. if (!item.enable_coupon) return false;
  62. break;
  63. }
  64. // 使用初始化参数创建工作时间表
  65. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  66. if (item.daily_calls_limit > 0)
  67. {
  68. int daily_num = RiskControlCore.GetCalls(TkChannelEnum.ks, item.id, DateTime.Now.ToString("yyyyMMdd"));
  69. if (daily_num >= item.daily_calls_limit) return false;
  70. }
  71. if (item.hourly_calls_limit > 0)
  72. {
  73. int hourly_num = RiskControlCore.GetCalls(TkChannelEnum.ks, item.id, DateTime.Now.ToString("yyyyMMddHH"));
  74. if (hourly_num >= item.hourly_calls_limit) return false;
  75. }
  76. return true;
  77. }
  78. public static IEnumerable<KsPoolDTO> List(bool force = false)
  79. {
  80. if (!force && _cached != null) return _cached;
  81. string cache_key = $"cache:ks_pool";
  82. var list = RedisHelper.Get<IEnumerable<KsPoolDTO>>(cache_key);
  83. if (force || list == null)
  84. {
  85. lock (_lockObj)
  86. {
  87. list = new DBContext.Table("ks_pool")
  88. .Where("status=@status", new { status = 1 })
  89. .Select<KsPoolDTO>();
  90. if (list == null) return default;
  91. foreach (var item in list)
  92. {
  93. RiskControlCore.SetCallsAsync(TkChannelEnum.ks, item.id, DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
  94. RiskControlCore.SetCallsAsync(TkChannelEnum.ks, item.id, DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
  95. }
  96. RedisHelper.Set(cache_key, list, 30 * 86400);
  97. }
  98. }
  99. _cached = list;
  100. return list;
  101. }
  102. public static void Refresh()
  103. {
  104. _ = List(true);
  105. }
  106. public static int Update(KsPoolDTO account)
  107. {
  108. return new DBContext.Table("ks_pool")
  109. .Add("current_hourly_calls", account.current_hourly_calls)
  110. .Add("current_daily_calls", account.current_daily_calls)
  111. .Add("today_clickNum", account.today_clickNum)
  112. .Add("today_cosFee", account.today_cosFee)
  113. .Add("today_cosPrice", account.today_cosPrice)
  114. .Add("today_finishCosFee", account.today_finishCosFee)
  115. .Add("today_finishCosPrice", account.today_finishCosPrice)
  116. .Add("today_finishOrderNum", account.today_finishOrderNum)
  117. .Add("today_orderNum", account.today_orderNum)
  118. .Where("id=@id", new { account.id })
  119. .Update();
  120. }
  121. public static int UpdateCookies(string cookies, string user_agent)
  122. {
  123. if (string.IsNullOrEmpty(cookies)) return 0;
  124. string pin = cookies.GetContentPart("pin=", ";");
  125. pin = pin.UrlDecode();
  126. string company = pin;
  127. int accountId = 0;
  128. if (string.IsNullOrEmpty(pin)) return 0;
  129. var exist = new DBContext.Table("ks_pool").Get<KsPoolDTO>("pin=@pin", new { pin });
  130. if (exist != null)
  131. {
  132. var status = exist.status;
  133. var work_mode = exist.work_mode;
  134. accountId = exist.id;
  135. if (work_mode == KsUnionWorkMode.Crawler) status = true;
  136. new DBContext.Table("ks_pool")
  137. .Add("pin", pin)
  138. .Add("cookies", cookies)
  139. .Add("user_agent", user_agent)
  140. .Add("status", status)
  141. .Add("last_time", DateTime.Now)
  142. .Add("login_time", DateTime.Now)
  143. .Where("id=@id", new { exist.id })
  144. .Update();
  145. if (status) _ = List(true);
  146. }
  147. else
  148. {
  149. accountId = new DBContext.Table("ks_pool")
  150. .Add("pin", pin)
  151. .Add("name", pin)
  152. .Add("company", pin)
  153. .Add("description", "由cookies上报创建此记录")
  154. .Add("cookies", cookies)
  155. .Add("user_agent", user_agent)
  156. .Add("create_time", DateTime.Now)
  157. .Add("last_time", DateTime.Now)
  158. .Add("login_time", DateTime.Now)
  159. .Add("status", 0)
  160. .Create();
  161. }
  162. NotifyCore.Notify(new NifyMessage
  163. {
  164. message = $"【快手{accountId}:{company}】cookie 上线",
  165. tags = ["green_circle"]
  166. });
  167. EndPointCore.NotifyReload(true);
  168. //NotifyCore.QYWeixinPushNotifyAsync("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  169. return accountId;
  170. }
  171. public static void Disabled(int accountId, string name, string content)
  172. {
  173. string cache_key = $"cache:ks_pool:{name}:disabled";
  174. long count = RedisHelper.IncrBy(cache_key);
  175. RedisHelper.Expire(cache_key, 10);
  176. if (count > 1) return;
  177. var update = new DBContext.Table("ks_pool").Add("status", 0);
  178. if (accountId > 0)
  179. {
  180. update.Where("id=@accountId", new { accountId }).Update();
  181. }
  182. else
  183. {
  184. update.Where("name=@name", new { name }).Update();
  185. }
  186. _ = List(true);
  187. NotifyCore.Notify(new NifyMessage
  188. {
  189. message = $"【快手联盟{accountId}:{name}】cookie 掉线\n\n{content}",
  190. priority = NifyMessagePriority.high,
  191. tags = ["red_circle"]
  192. });
  193. //NotifyCore.QYWeixinPushNotifyAsync("掉线", $"【快手{accountId}:{name}】cookie 掉线");
  194. EndPointCore.NotifyReload(true);
  195. }
  196. internal static void AccountExhausted()
  197. {
  198. string cache_key = $"cache:ks_pool:account:exhausted";
  199. long count = RedisHelper.IncrBy(cache_key);
  200. if (count > 1) return;
  201. RedisHelper.Expire(cache_key, 3600);
  202. NotifyCore.Notify(new NifyMessage
  203. {
  204. message = $"【快手联盟】没有匹配账号",
  205. priority = NifyMessagePriority.high,
  206. tags = ["red_circle"]
  207. });
  208. _ = NotifyCore.QYWeixinPushNotifyAsync("没账号", $"【快手联盟】没有匹配账号");
  209. }
  210. internal static void NotifyInterfaceError(string reason)
  211. {
  212. string cache_key = $"cache:ks_pool:account:InterfaceError";
  213. long count = RedisHelper.IncrBy(cache_key);
  214. if (count > 1) return;
  215. RedisHelper.Expire(cache_key, 3600);
  216. NotifyCore.Notify(new NifyMessage
  217. {
  218. message = $"【快手联盟】{reason}",
  219. priority = NifyMessagePriority.high,
  220. tags = ["red_circle"]
  221. });
  222. }
  223. }
  224. }