JdPoolCore.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 JdPoolCore
  15. {
  16. public enum JdAction
  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. private static Dictionary<string, int> _calls = new();
  26. static JdPoolCore()
  27. {
  28. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  29. }
  30. private static readonly object _lockObj = new();
  31. private static IEnumerable<JdPoolDTO> _cached;
  32. public static JdPoolDTO? GetOne()
  33. {
  34. var list = List();
  35. if (!list.Any()) return null;
  36. return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  37. }
  38. public static JdPoolDTO? GetOne(JdAction action)
  39. {
  40. var list = List();
  41. if (!list.Any()) return null;
  42. return list.Where(e => IsNotExceedDailyIncomeLimit(e, action)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  43. }
  44. public static JdPoolDTO? GetOne(int id, JdAction action)
  45. {
  46. var list = List();
  47. if (!list.Any()) return null;
  48. return list.Where(e => e.id == id && IsNotExceedDailyIncomeLimit(e, action)).FirstOrDefault();
  49. }
  50. internal static void CallsIncrBy(int accountId)
  51. {
  52. CallsIncrBy(accountId, DateTime.Now.ToString("yyyyMMdd"));
  53. CallsIncrBy(accountId, DateTime.Now.ToString("yyyyMMddHH"));
  54. }
  55. internal static void CallsIncrBy(int accountId, string flag)
  56. {
  57. string key = $"{accountId}:{flag}";
  58. if (_calls.ContainsKey(key))
  59. {
  60. _calls[key] = _calls[key] + 1;
  61. }
  62. else
  63. {
  64. _calls[key] = 1;
  65. }
  66. string cache_key = $"cache:jd_pool:{key}:calls:{flag}";
  67. RedisHelper.IncrBy(cache_key);
  68. RedisHelper.Expire(cache_key, 3 * 86400);
  69. }
  70. public static int SaveCalls(int accountId, string flag)
  71. {
  72. string key = $"{accountId}:{flag}";
  73. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  74. {
  75. if (!node.is_public_api) return 0;
  76. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  77. #if DEBUG
  78. switch (node.name)
  79. {
  80. case "bj":
  81. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  82. break;
  83. case "gz":
  84. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  85. break;
  86. case "coupon1":
  87. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  88. break;
  89. default: return 0;
  90. }
  91. #endif
  92. string cache_key = $"cache:jd_pool:{key}:calls:{flag}";
  93. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  94. return redis.Get<int>(cache_key);
  95. });
  96. int num = result.Sum();
  97. _calls.TryAdd(key, num);
  98. return num;
  99. }
  100. public static int GetCalls(int accountId, string flag)
  101. {
  102. try
  103. {
  104. string key = $"{accountId}:{flag}";
  105. if (_calls.ContainsKey(key)) return _calls[key];
  106. string cache_key = $"cache:jd_pool:{key}:calls:{flag}";
  107. int num = RedisHelper.Get<int>(cache_key);
  108. _calls.TryAdd(key, num);
  109. return num;
  110. }
  111. catch (Exception ex) { return 0; }
  112. }
  113. private static bool IsNotExceedDailyIncomeLimit(JdPoolDTO item, JdAction action)
  114. {
  115. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  116. {
  117. if (item.end_point != _end_point) return false;
  118. }
  119. switch (action)
  120. {
  121. case JdAction.parse:
  122. if (!item.enable_parse) return false;
  123. break;
  124. case JdAction.coupon:
  125. if (!item.enable_coupon) return false;
  126. break;
  127. }
  128. // 使用初始化参数创建工作时间表
  129. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  130. if (item.daily_calls_limit > 0)
  131. {
  132. int daily_num = GetCalls(item.id, DateTime.Now.ToString("yyyyMMdd"));
  133. if (daily_num >= item.daily_calls_limit) return false;
  134. }
  135. if (item.hourly_calls_limit > 0)
  136. {
  137. int hourly_num = GetCalls(item.id, DateTime.Now.ToString("yyyyMMddHH"));
  138. if (hourly_num >= item.hourly_calls_limit) return false;
  139. }
  140. return true;
  141. }
  142. public static IEnumerable<JdPoolDTO> List(bool force = false)
  143. {
  144. if (!force && _cached != null) return _cached;
  145. string cache_key = $"cache:jd_pool";
  146. var list = RedisHelper.Get<IEnumerable<JdPoolDTO>>(cache_key);
  147. if (force || list == null)
  148. {
  149. lock (_lockObj)
  150. {
  151. list = new DBContext.Table("jd_pool")
  152. .Where("status=@status", new { status = 1 })
  153. .Select<JdPoolDTO>();
  154. if (list == null) return default;
  155. _calls = new Dictionary<string, int>();
  156. foreach (var item in list)
  157. {
  158. string key = $"{item.id}:{DateTime.Now:yyyyMMddHH}";
  159. _calls.TryAdd(key, item.current_hourly_calls);
  160. key = $"{item.id}:{DateTime.Now:yyyyMMddHH}";
  161. _calls.TryAdd(key, item.current_daily_calls);
  162. }
  163. RedisHelper.Set(cache_key, list, 30 * 86400);
  164. }
  165. }
  166. _cached = list;
  167. return list;
  168. }
  169. public static void Refresh()
  170. {
  171. _ = List(true);
  172. }
  173. public static int Update(JdPoolDTO account)
  174. {
  175. return new DBContext.Table("jd_pool")
  176. .Add("current_hourly_calls", account.current_hourly_calls)
  177. .Add("current_daily_calls", account.current_daily_calls)
  178. .Add("today_clickNum", account.today_clickNum)
  179. .Add("today_cosFee", account.today_cosFee)
  180. .Add("today_cosPrice", account.today_cosPrice)
  181. .Add("today_finishCosFee", account.today_finishCosFee)
  182. .Add("today_finishCosPrice", account.today_finishCosPrice)
  183. .Add("today_finishOrderNum", account.today_finishOrderNum)
  184. .Add("today_orderNum", account.today_orderNum)
  185. .Where("id=@id", new { account.id })
  186. .Update();
  187. }
  188. public static int UpdateCookies(string cookies, string user_agent)
  189. {
  190. if (string.IsNullOrEmpty(cookies)) return 0;
  191. string pin = cookies.GetContentPart("pin=", ";");
  192. pin = pin.UrlDecode();
  193. string company = pin;
  194. int accountId = 0;
  195. if (string.IsNullOrEmpty(pin)) return 0;
  196. var exist = new DBContext.Table("jd_pool").Get<JdPoolDTO>("pin=@pin", new { pin });
  197. if (exist != null)
  198. {
  199. var status = exist.status;
  200. var work_mode = exist.work_mode;
  201. accountId = exist.id;
  202. if (work_mode == JdUnionWorkMode.Crawler) status = true;
  203. new DBContext.Table("jd_pool")
  204. .Add("pin", pin)
  205. .Add("cookies", cookies)
  206. .Add("user_agent", user_agent)
  207. .Add("status", status)
  208. .Add("last_time", DateTime.Now)
  209. .Add("login_time", DateTime.Now)
  210. .Where("id=@id", new { exist.id })
  211. .Update();
  212. if (status) _ = List(true);
  213. }
  214. else
  215. {
  216. accountId = new DBContext.Table("jd_pool")
  217. .Add("pin", pin)
  218. .Add("name", pin)
  219. .Add("company", pin)
  220. .Add("description", "由cookies上报创建此记录")
  221. .Add("cookies", cookies)
  222. .Add("user_agent", user_agent)
  223. .Add("create_time", DateTime.Now)
  224. .Add("last_time", DateTime.Now)
  225. .Add("login_time", DateTime.Now)
  226. .Add("status", 0)
  227. .Create();
  228. }
  229. NotifyCore.Notify(new NifyMessage
  230. {
  231. message = $"【京东{accountId}:{company}】cookie 上线",
  232. tags = ["green_circle"]
  233. });
  234. EndPointCore.NotifyReload(true);
  235. //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
  236. return accountId;
  237. }
  238. public static void Disabled(int accountId, string name, string content)
  239. {
  240. string cache_key = $"cache:jd_pool:{name}:disabled";
  241. long count = RedisHelper.IncrBy(cache_key);
  242. RedisHelper.Expire(cache_key, 10);
  243. if (count > 1) return;
  244. var update = new DBContext.Table("jd_pool").Add("status", 0);
  245. if (accountId > 0)
  246. {
  247. update.Where("id=@accountId", new { accountId }).Update();
  248. }
  249. else
  250. {
  251. update.Where("name=@name", new { name }).Update();
  252. }
  253. _ = List(true);
  254. NotifyCore.Notify(new NifyMessage
  255. {
  256. message = $"【京东联盟{accountId}:{name}】cookie 掉线\n\n{content}",
  257. priority = NifyMessagePriority.high,
  258. tags = ["red_circle"]
  259. });
  260. NotifyCore.AnPushNotify("掉线", $"【京东{accountId}:{name}】cookie 掉线");
  261. EndPointCore.NotifyReload(true);
  262. }
  263. internal static void AccountExhausted()
  264. {
  265. string cache_key = $"cache:tk_pool:account:exhausted";
  266. long count = RedisHelper.IncrBy(cache_key);
  267. if (count > 1) return;
  268. RedisHelper.Expire(cache_key, 3600);
  269. NotifyCore.Notify(new NifyMessage
  270. {
  271. message = $"【京东联盟】没有匹配账号",
  272. priority = NifyMessagePriority.high,
  273. tags = ["red_circle"]
  274. });
  275. NotifyCore.AnPushNotify("没账号", $"【京东联盟】没有匹配账号");
  276. }
  277. }
  278. }