| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- using Dataoke;
- using Google.Protobuf.WellKnownTypes;
- using System.Xml.Linq;
- namespace molilian.core
- {
- public partial class JdPoolCore
- {
- public enum JdAction
- {
- all,
- parse,
- coupon,
- promotionQuery
- }
- private static string _end_point;
- private static Dictionary<string, decimal> _incomeAmt = new();
- private static Dictionary<string, int> _calls = new();
- static JdPoolCore()
- {
- _end_point = Environment.GetEnvironmentVariable("EndPoint");
- }
- private static readonly object _lockObj = new();
- private static IEnumerable<JdPoolDTO> _cached;
- public static JdPoolDTO? GetOne()
- {
- var list = List();
- if (!list.Any()) return null;
- return list.OrderBy(l => Guid.NewGuid()).FirstOrDefault();
- }
- public static JdPoolDTO? GetOne(JdAction action)
- {
- var list = List();
- if (!list.Any()) return null;
- return list.Where(e => IsNotExceedDailyIncomeLimit(e, action)).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
- }
- public static JdPoolDTO? GetOne(int id, JdAction action)
- {
- var list = List();
- if (!list.Any()) return null;
- return list.Where(e => e.id == id && IsNotExceedDailyIncomeLimit(e, action)).FirstOrDefault();
- }
- //internal static void SaveIncomeAmt(int accountId, decimal income_amt)
- //{
- // string key = $"{accountId}:{DateTime.Now:yyyyMMdd}";
- // if (_incomeAmt.ContainsKey(key))
- // {
- // _incomeAmt[key] = income_amt;
- // }
- // else
- // {
- // _incomeAmt.Add(key, income_amt);
- // }
- // string cache_key = $"cache:jd_pool:{key}:income_amt";
- // var result = EndPointCore.ProcessEndPointNodes<bool>(node =>
- // {
- // if (!node.is_public_api) return true;
- // if (string.IsNullOrEmpty(node.redis_server)) return true;
- // var redis = RedisClientManager.GetRedisClient(node.redis_server);
- // return redis.Set(cache_key, income_amt, 3 * 86400);
- // });
- //}
- //public static decimal GetIncomeAmt(int accountId)
- //{
- // try
- // {
- // string key = $"{accountId}:{DateTime.Now:yyyyMMdd}";
- // if (_incomeAmt.ContainsKey(key)) return _incomeAmt[key];
- // string cache_key = $"cache:jd_pool:{key}:income_amt";
- // return RedisHelper.Get<decimal>(cache_key);
- // }
- // catch (Exception ex) { return 0; }
- //}
- internal static void CallsIncrBy(int accountId)
- {
- CallsIncrBy(accountId, DateTime.Now.ToString("yyyyMMdd"));
- CallsIncrBy(accountId, DateTime.Now.ToString("yyyyMMddHH"));
- }
- internal static void CallsIncrBy(int accountId, string flag)
- {
- string key = $"{accountId}:{flag}";
- if (_calls.ContainsKey(key))
- {
- _calls[key] = _calls[key] + 1;
- }
- else
- {
- _calls[key] = 1;
- }
- string cache_key = $"cache:jd_pool:{key}:calls:{flag}";
- RedisHelper.IncrBy(cache_key);
- RedisHelper.Expire(cache_key, 3 * 86400);
- }
- public static int SaveCalls(int accountId, string flag)
- {
- string key = $"{accountId}:{flag}";
- var result = EndPointCore.ProcessEndPointNodes<int>(node =>
- {
- if (!node.is_public_api) return 0;
- if (string.IsNullOrEmpty(node.redis_server)) return 0;
- #if DEBUG
- switch (node.name)
- {
- case "bj":
- node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
- break;
- case "gz":
- node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
- break;
- case "coupon1":
- node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
- break;
- default: return 0;
- }
- #endif
- string cache_key = $"cache:jd_pool:{key}:calls:{flag}";
- var redis = RedisClientManager.GetRedisClient(node.redis_server);
- return redis.Get<int>(cache_key);
- });
- int num = result.Sum();
- _calls.TryAdd(key, num);
- return num;
- }
- public static int GetCalls(int accountId, string flag)
- {
- try
- {
- string key = $"{accountId}:{flag}";
- if (_calls.ContainsKey(key)) return _calls[key];
- string cache_key = $"cache:jd_pool:{key}:calls:{flag}";
- int num = RedisHelper.Get<int>(cache_key);
- _calls.TryAdd(key, num);
- return num;
- }
- catch (Exception ex) { return 0; }
- }
- private static bool IsNotExceedDailyIncomeLimit(JdPoolDTO item, JdAction action)
- {
- if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
- {
- if (item.end_point != _end_point) return false;
- }
- switch (action)
- {
- case JdAction.parse:
- if (!item.enable_parse) return false;
- break;
- case JdAction.coupon:
- if (!item.enable_coupon) return false;
- break;
- }
- // 使用初始化参数创建工作时间表
- if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
- if (item.daily_calls_limit > 0)
- {
- int daily_num = GetCalls(item.id, DateTime.Now.ToString("yyyyMMdd"));
- if (daily_num >= item.daily_calls_limit) return false;
- }
- if (item.hourly_calls_limit > 0)
- {
- int hourly_num = GetCalls(item.id, DateTime.Now.ToString("yyyyMMddHH"));
- if (hourly_num >= item.hourly_calls_limit) return false;
- }
- return true;
- }
- public static IEnumerable<JdPoolDTO> List(bool force = false)
- {
- if (!force && _cached != null) return _cached;
- string cache_key = $"cache:jd_pool";
- var list = RedisHelper.Get<IEnumerable<JdPoolDTO>>(cache_key);
- if (force || list == null)
- {
- lock (_lockObj)
- {
- list = new DBContext.Table("jd_pool")
- .Where("status=@status", new { status = 1 })
- .Select<JdPoolDTO>();
- if (list == null) return default;
- _calls = new Dictionary<string, int>();
- foreach (var item in list)
- {
- string key = $"{item.id}:{DateTime.Now:yyyyMMddHH}";
- _calls.TryAdd(key, item.current_hourly_calls);
- key = $"{item.id}:{DateTime.Now:yyyyMMddHH}";
- _calls.TryAdd(key, item.current_daily_calls);
- }
- RedisHelper.Set(cache_key, list, 30 * 86400);
- }
- }
- _cached = list;
- return list;
- }
- public static void Refresh()
- {
- _ = List(true);
- }
- public static int Update(JdPoolDTO account)
- {
- return new DBContext.Table("jd_pool")
- .Add("current_hourly_calls", account.current_hourly_calls)
- .Add("current_daily_calls", account.current_daily_calls)
- .Where("id=@id", new { account.id })
- .Update();
- }
- public static int UpdateCookies(string cookies, string user_agent)
- {
- if (string.IsNullOrEmpty(cookies)) return 0;
- cookies += ";";
- string pin = cookies.GetContentPart("pin=", ";");
- pin = pin.UrlDecode();
- string company = pin;
- int accountId = 0;
- if (string.IsNullOrEmpty(pin)) return 0;
- var exist = new DBContext.Table("jd_pool").Get<JdPoolDTO>("pin=@pin", new { pin });
- if (exist != null)
- {
- var status = exist.status;
- var work_mode = exist.work_mode;
- accountId = exist.id;
- if (work_mode == JdUnionWorkMode.Crawler) status = true;
- new DBContext.Table("jd_pool")
- .Add("pin", pin)
- .Add("cookies", cookies)
- .Add("user_agent", user_agent)
- .Add("status", status)
- .Add("last_time", DateTime.Now)
- .Add("login_time", DateTime.Now)
- .Where("id=@id", new { exist.id })
- .Update();
- if (status) _ = List(true);
- }
- else
- {
- accountId = new DBContext.Table("jd_pool")
- .Add("pin", pin)
- .Add("name", pin)
- .Add("company", pin)
- .Add("description", "由cookies上报创建此记录")
- .Add("cookies", cookies)
- .Add("user_agent", user_agent)
- .Add("create_time", DateTime.Now)
- .Add("last_time", DateTime.Now)
- .Add("login_time", DateTime.Now)
- .Add("status", 0)
- .Create();
- }
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【京东{accountId}:{company}】cookie 上线",
- tags = ["green_circle"]
- });
- EndPointCore.NotifyReload(true);
- //NotifyCore.AnPushNotify("上线", $"【淘宝联盟:{dnk}】cookie 上报更新");
- return accountId;
- }
- public static void Disabled(int accountId, string name, string content)
- {
- string cache_key = $"cache:jd_pool:{name}:disabled";
- long count = RedisHelper.IncrBy(cache_key);
- RedisHelper.Expire(cache_key, 10);
- if (count > 1) return;
- var update = new DBContext.Table("jd_pool").Add("status", 0);
- if (accountId > 0)
- {
- update.Where("id=@accountId", new { accountId }).Update();
- }
- else
- {
- update.Where("name=@name", new { name }).Update();
- }
- _ = List(true);
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【京东联盟{accountId}:{name}】cookie 掉线\n\n{content}",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- NotifyCore.AnPushNotify("掉线", $"【京东{accountId}:{name}】cookie 掉线");
- EndPointCore.NotifyReload(true);
- }
- internal static void AccountExhausted()
- {
- string cache_key = $"cache:tk_pool:account:exhausted";
- long count = RedisHelper.IncrBy(cache_key);
- if (count > 1) return;
- RedisHelper.Expire(cache_key, 3600);
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【京东联盟】没有匹配账号",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- NotifyCore.AnPushNotify("没账号", $"【京东联盟】没有匹配账号");
- }
- }
- }
|