/// <summary>
        /// 解锁Host用户
        /// </summary>
        private static void HostUnLock()
        {
            lock (lockObj)
            {
                using (var db = new ResumeMatchDBEntities())
                {
                    db.UsingTransaction(() =>
                    {
                        string host;

                        while (hostQueue.TryDequeue(out host))
                        {
                            var hostUnLock = host;

                            var users = db.User.Where(w => w.Host == hostUnLock && w.IsLocked).ToList();

                            foreach (var user in users)
                            {
                                user.IsLocked = false;
                            }
                        }

                        foreach (var item in hostList)
                        {
                            if (string.IsNullOrWhiteSpace(item))
                            {
                                continue;
                            }

                            var users = db.User.Where(w => w.Host == item && w.IsLocked).ToList();

                            foreach (var user in users)
                            {
                                user.IsLocked = false;
                            }
                        }

                        db.SaveChanges();

                        hostList.Clear();
                    });
                }
            }
        }
        /// <summary>
        /// 解锁简历
        /// </summary>
        private static void ResumeUnLock()
        {
            lock (lockObj)
            {
                using (var db = new ResumeMatchDBEntities())
                {
                    db.UsingTransaction(() =>
                    {
                        KeyValuePair <string, List <ResumeComplete> > hostResume;

                        while (resumeQueue.TryDequeue(out hostResume))
                        {
                            var resumeIdArr = hostResume.Value.Select(s => s.Id).ToArray();

                            var resumes = db.ResumeComplete.Where(w => resumeIdArr.Any(a => a == w.Id) && w.IsLocked).ToList();

                            foreach (var resume in resumes)
                            {
                                resume.IsLocked = false;
                            }
                        }

                        foreach (var item in hostResumeList)
                        {
                            var resumeIdArr = item.Value.Select(s => s.Id).ToArray();

                            var resumes = db.ResumeComplete.Where(w => resumeIdArr.Any(a => a == w.Id) && w.IsLocked).ToList();

                            foreach (var resume in resumes)
                            {
                                resume.IsLocked = false;
                            }
                        }

                        db.SaveChanges();
                    });
                }
            }
        }
        private void Download()
        {
            lock (lockObj)
            {
                var resumes = new List <ResumeComplete>();

                using (var db = new ResumeMatchDBEntities())
                {
                    db.UsingTransaction(() =>
                    {
                        var dateTime = DateTime.UtcNow.AddHours(-1);

                        var today = DateTime.UtcNow.Date.AddHours(-8);

                        var downloadUserArr = db.User
                                              .Where(w => w.IsEnable && (string.IsNullOrEmpty(w.Host) || !string.IsNullOrEmpty(w.Host) == Global.IsEnanbleProxy) && (w.DownloadNumber > 0 || w.LastLoginTime < today || w.LastLoginTime == null))
                                              .GroupBy(g => new { g.Host, g.Platform })
                                              .Select(s => s.Key)
                                              .Distinct()
                                              .ToList();

                        var query = db.ResumeComplete
                                    .Where(w => w.Status == 2 && (!w.IsLocked || w.IsLocked && w.LockedTime < dateTime));

                        var platformList = downloadUserArr.GroupBy(g => g.Platform).Select(s => s.Key).ToList();

                        var hostList = downloadUserArr.GroupBy(g => g.Host) /*.Where(w=>!string.IsNullOrEmpty(w.Key))*/.Select(s => s.Key).ToList(); // 排除本地HOST

                        foreach (var item in platformList)
                        {
                            resumes.AddRange(query
                                             .Where(w => w.MatchPlatform == item)
                                             .OrderByDescending(o => o.Weights)
                                             .ThenByDescending(o => o.MatchTime)
                                             .Take(20));
                        }

                        resumes = resumes.Where(w => w.Weights == 1 && w.MatchPlatform == 4) // Todo:当前只优先下载泽林的简历
                                  .OrderByDescending(o => o.Weights)
                                  .ThenByDescending(o => o.MatchTime)
                                  .Take(20).ToList();

                        foreach (var resume in resumes)
                        {
                            resume.IsLocked = true;

                            resume.LockedTime = DateTime.UtcNow;
                        }

                        db.SaveChanges();

                        var count = resumes.Count / hostList.Count + 1;

                        for (var i = 0; i < hostList.Count; i++)
                        {
                            var temp = resumes.Skip(i * count).Take(count).ToList();

                            if (temp.Any())
                            {
                                resumeQueue.Enqueue(new KeyValuePair <string, List <ResumeComplete> >(hostList[i], temp));
                            }
                        }
                    });
                }
            }

            KeyValuePair <string, List <ResumeComplete> > hostResume;

            while (resumeQueue.TryDequeue(out hostResume))
            {
                var hostResumeTemp = hostResume;

                hostResumeList.Add(hostResumeTemp);

                if (!string.IsNullOrWhiteSpace(hostResumeTemp.Key))
                {
                    GetProxy("Download", hostResumeTemp.Key);
                }

                Work(hostResumeTemp.Key, hostResumeTemp.Value);

                ReleaseProxy("Download", hostResumeTemp.Key);

                if (isEnd)
                {
                    ResumeUnLock();
                }

                hostResumeList.Remove(hostResumeTemp);
            }
        }
        /// <summary>
        /// 匹配
        /// </summary>
        private void Match()
        {
            lock (lockObj)
            {
                if (!hostQueue.Any() && isFirst)
                {
                    hostQueue.Enqueue("210.83.225.31:15839");

                    isFirst = false;
                }

                var users = new List <User>();

                using (var db = new ResumeMatchDBEntities())
                {
                    db.UsingTransaction(() =>
                    {
                        var dateTime = DateTime.UtcNow.AddHours(-1);

                        var nowDate = DateTime.UtcNow.Date;

                        users = db.User
                                .Where(w => w.IsEnable && w.Status == 1 && (w.RequestDate.Value == null || w.RequestDate.Value < nowDate || w.RequestDate.Value == nowDate && w.RequestNumber < Global.TodayMaxRequestNumber) && (!w.IsLocked || w.IsLocked && w.LockedTime < dateTime) && (string.IsNullOrEmpty(w.Host) || !string.IsNullOrEmpty(w.Host) == Global.IsEnanbleProxy))
                                .OrderBy(o => o.RequestNumber)
                                .ThenByDescending(o => o.Host)
                                //.Take(Global.PlatformCount * Global.PlatformHostCount * 2)
                                .ToList();

                        foreach (var user in users)
                        {
                            user.IsLocked = true;

                            user.LockedTime = DateTime.UtcNow;
                        }

                        db.SaveChanges();
                    });
                }

                var hosts = users
                            .Where(w => w.IsEnable && w.Host != "210.83.225.31:15839")
                            .GroupBy(g => g.Host)
                            .Select(s => new { Host = s.Key, Count = s.Count() })
                            .OrderByDescending(o => o.Count)
                            .ToList();

                hosts.ForEach(f => { hostQueue.Enqueue(f.Host); });
            }

            string host;

            while (hostQueue.TryDequeue(out host))
            {
                var hostTemp = host;

                hostList.Add(hostTemp);

                if (!string.IsNullOrWhiteSpace(hostTemp))
                {
                    GetProxy("Match", hostTemp);
                }

                while (true)
                {
                    List <ResumeSearch> resumes;

                    while (true)
                    {
                        try
                        {
                            resumes = ResumeProducer.PullResumes();

                            break;
                        }
                        catch (Exception ex)
                        {
                            while (true)
                            {
                                if (ex.InnerException == null)
                                {
                                    break;
                                }

                                ex = ex.InnerException;
                            }

                            LogFactory.Error($"拉取待匹配的简历异常!异常信息:{ex.Message},堆栈信息:{ex.StackTrace}");
                        }
                    }

                    if (resumes != null && resumes.Count > 0)
                    {
                        Console.WriteLine(string.Join(",", resumes.Select(s => s.Name).ToArray()));

                        if (!Work(hostTemp, resumes))
                        {
                            break;
                        }
                    }
                }

                ReleaseProxy("Match", hostTemp);

                //hostList.Remove(hostTemp);

                LogFactory.Info("Host 消费记录:" + JsonConvert.SerializeObject(hostQueue));
            }

            HostUnLock();
        }
Beispiel #5
0
        /// <summary>
        /// 激活
        /// </summary>
        private static void Activation()
        {
            var users = new List <User>();

            lock (lockObj)
            {
                using (var db = new ResumeMatchDBEntities())
                {
                    db.UsingTransaction(() =>
                    {
                        var time = DateTime.UtcNow.AddHours(-1);

                        users = db.User.Where(w => w.Status == 0 && w.Platform == 3 && (!w.IsLocked || w.IsLocked && w.LockedTime < time)).ToList();

                        foreach (var user in users)
                        {
                            user.IsLocked = true;

                            user.LockedTime = DateTime.UtcNow;
                        }

                        db.SaveChanges();
                    });
                }
            }

            if (users.Count == 0)
            {
                return;
            }

            #region 获取未读邮件列表

            var seenUids = new List <string>();

            var messages = EmailFactory.FetchUnseenMessages("pop.exmail.qq.com", 995, true, Global.Email, Global.PassWord, seenUids);

            using (var db = new ResumeMatchDBEntities())
            {
                var userIdArr = users.Select(s => s.Id).ToArray();

                var userList = db.User.Where(w => userIdArr.Any(a => a == w.Id));

                foreach (var user in userList)
                {
                    user.IsLocked = false;

                    var message = messages.FirstOrDefault(f => f.message.Headers.To.FirstOrDefault()?.Address == user.Email);

                    if (message == null)
                    {
                        continue;
                    }

                    var content = Encoding.Default.GetString(message.message.RawMessage);

                    if (!Regex.IsMatch(content, "(?s)token=(.+?)</a>"))
                    {
                        LogFactory.Error($"未匹配到激活码!响应源:{content}", MessageSubjectEnum.JianLiKa);

                        continue;
                    }

                    var activationCode = Regex.Match(content, "(?s)token=(.+?)</a>").Result("$1").Substring(2);

                    var host = string.Empty;

                    //if (Global.IsEnanbleProxy)
                    //{
                    //    if (!string.IsNullOrWhiteSpace(user.Host))
                    //    {
                    //        host = user.Host;

                    //        GetProxy("JLK_Activation",user.Host);
                    //    }
                    //}

                    var dataResult = Activation(activationCode, user.Email, host);

                    //ReleaseProxy("JLK_Activation", host);

                    if (dataResult == null)
                    {
                        continue;
                    }

                    if (!dataResult.IsSuccess)
                    {
                        LogFactory.Error(dataResult.ErrorMsg, MessageSubjectEnum.JianLiKa);

                        continue;
                    }

                    user.Status = 1;

                    LogFactory.Info($"激活成功!邮箱:{user.Email}", MessageSubjectEnum.JianLiKa);

                    if (!EmailFactory.DeleteMessageByMessageId("pop.exmail.qq.com", 995, true, Global.Email, Global.PassWord, message.message.Headers.MessageId))
                    {
                        LogFactory.Error($"删除激活邮件失败,邮箱地址:{user.Email}", MessageSubjectEnum.JianLiKa);
                    }
                }

                db.TransactionSaveChanges();
            }

            #endregion
        }
        /// <summary>
        /// 激活
        /// </summary>
        private void Activation()
        {
            var users = new List <User>();

            lock (lockObj)
            {
                using (var db = new ResumeMatchDBEntities())
                {
                    db.UsingTransaction(() =>
                    {
                        var time = DateTime.UtcNow.AddHours(-1);

                        users = db.User.Where(w => w.Status == 0 && w.Platform == 4 /*&& (!w.IsLocked || w.IsLocked && w.LockedTime < time)*/).ToList();

                        foreach (var user in users)
                        {
                            user.IsLocked = true;

                            user.LockedTime = DateTime.UtcNow;
                        }

                        db.SaveChanges();
                    });
                }
            }

            if (users.Count == 0)
            {
                return;
            }

            #region 获取未读邮件列表

            var seenUids = new List <string>();

            var messages = EmailFactory.FetchUnseenMessages("pop.exmail.qq.com", 995, true, Global.Email, Global.PassWord, seenUids);

            using (var db = new ResumeMatchDBEntities())
            {
                var userIdArr = users.Select(s => s.Id).ToArray();

                var userList = db.User.Where(w => userIdArr.Any(a => a == w.Id));

                foreach (var user in userList)
                {
                    user.IsLocked = false;

                    var message = messages.FirstOrDefault(f => f.message.Headers.To.FirstOrDefault()?.Address == user.Email && f.message.Headers.From.Address == "*****@*****.**");

                    if (message == null)
                    {
                        continue;
                    }

                    var content = Encoding.UTF8.GetString(message.message.FindFirstHtmlVersion().Body);

                    if (!Regex.IsMatch(content, "(?s)完成验证.+?com/track/click/(.+?html)"))
                    {
                        LogFactory.Error($"未匹配到激活码!响应源:{content}", MessageSubjectEnum.ZhaoPinGou);

                        continue;
                    }

                    var url = "http://sctrack.info.zhaopingou.com/track/click/" + Regex.Match(content, "(?s)完成验证.+?com/track/click/(.+?html)\".style").Result("$1");

                    var host = string.Empty;

                    //if (Global.IsEnanbleProxy)
                    //{
                    //    if (!string.IsNullOrWhiteSpace(user.Host))
                    //    {
                    //        host = user.Host;

                    //        GetProxy("ZPG_Activation",user.Host);
                    //    }
                    //}
                    var dataResult = Activation(url, user.Email, host);

                    //ReleaseProxy("ZPG_Activation", host);

                    if (dataResult == null)
                    {
                        continue;
                    }

                    if (!dataResult.IsSuccess)
                    {
                        LogFactory.Error(dataResult.ErrorMsg, MessageSubjectEnum.ZhaoPinGou);

                        continue;
                    }

                    user.Status = 1;

                    LogFactory.Info($"激活成功!邮箱:{user.Email}", MessageSubjectEnum.ZhaoPinGou);

                    if (!EmailFactory.DeleteMessageByMessageId("pop.exmail.qq.com", 995, true, Global.Email, Global.PassWord, message.message.Headers.MessageId))
                    {
                        LogFactory.Error($"删除激活邮件失败,邮箱地址:{user.Email}", MessageSubjectEnum.ZhaoPinGou);
                    }
                }

                db.TransactionSaveChanges();
            }

            #endregion
        }
Beispiel #7
0
        /// <summary>
        /// 注册
        /// </summary>
        private void Register()
        {
            var proxyId = 0;

            var host = string.Empty;

            var isAddProxy = false;

            lock (lockObj)
            {
                using (var db = new ResumeMatchDBEntities())
                {
                    var data = db.UsingTransaction(() =>
                    {
                        var result = new DataResult();

                        if (!Global.IsEnanbleProxy)
                        {
                            return(result);
                        }

                        var ipArr = db.Proxy.AsNoTracking()
                                    .GroupBy(g => g.Host)
                                    .Select(s => new { Host = s.Key, Count = s.Count() })
                                    .Where(w => w.Count < Global.PlatformCount * Global.PlatformHostCount)
                                    .Select(s => s.Host).ToArray();

                        if (ipArr.Length > 0)
                        {
                            var proxy = db.Proxy.FirstOrDefault(f => ipArr.Any(a => a == f.Host) && f.Platform == 4 && !string.IsNullOrEmpty(f.Host) && f.Count < Global.PlatformHostCount);

                            if (proxy != null)
                            {
                                proxyId = proxy.Id;

                                host = proxy.Host;

                                proxy.Count++;

                                var user = db.User.Where(w => w.Host == host && w.Platform == 4).OrderBy(o => o.DownloadNumber).FirstOrDefault();

                                if (user == null)
                                {
                                    LogFactory.Error($"找不到用户,Host = {host}", MessageSubjectEnum.ZhaoPinGou);

                                    result.IsSuccess = false;

                                    return(result);
                                }

                                db.SaveChanges();

                                return(result);
                            }
                        }

                        var proxyEntity = new Proxy {
                            Count = 1, Platform = 4
                        };

                        db.Proxy.Add(proxyEntity);

                        db.SaveChanges();

                        isAddProxy = true;

                        proxyId = proxyEntity.Id;

                        return(result);
                    });

                    if (!data.IsSuccess)
                    {
                        return;
                    }
                }
            }

            if (Global.IsEnanbleProxy)
            {
                if (string.IsNullOrWhiteSpace(host))
                {
                    host = GetProxy("ZPG_Register", true);
                }
                else
                {
                    GetProxy("ZPG_Register", host);
                }
            }

            var dataResult = Register(host);

            ReleaseProxy("ZPG_Register", host);

            using (var db = new ResumeMatchDBEntities())
            {
                var proxy = db.Proxy.FirstOrDefault(f => f.Id == proxyId);

                if (dataResult == null || !dataResult.IsSuccess)
                {
                    if (proxy != null)
                    {
                        if (isAddProxy)
                        {
                            db.Proxy.Remove(proxy);
                        }
                        else
                        {
                            proxy.Count--;
                        }
                    }
                }
                else
                {
                    db.User.Add(new User
                    {
                        Email          = dataResult.Data.Email,
                        Password       = dataResult.Data.Password,
                        InviteCode     = dataResult.Data.InviteCode,
                        CreateTime     = DateTime.UtcNow,
                        IsEnable       = true,
                        DownloadNumber = 10,
                        Host           = host,
                        Platform       = 4,
                        Status         = 0,
                        IsLocked       = false,
                        LockedTime     = new DateTime(1900, 1, 1),
                        RequestNumber  = 0
                    });

                    if (proxy != null)
                    {
                        proxy.Host = host;
                    }
                }

                db.TransactionSaveChanges();
            }
        }