Exemplo n.º 1
0
        /// <summary>
        /// 订阅某个用户
        /// </summary>
        /// <param name="AuthorID">待订阅的用户ID</param>
        /// <returns>订阅结果枚举</returns>
        public static FollowStatus FollowUser(string AuthorID)
        {
            string where;
            LoginAccountEntity acc = null;

            try
            {
                where = string.Format("IsDisabled=FALSE AND AuthorID IS NOT NULL AND SubscribeStatus>=0 AND FollowCountToday<{0} AND FollowCount<{1} AND SubscribeLastTime<'{2}'", DefaultSettings.FollowLimitPerDay, DefaultSettings.MaxFriendsCnt, DateTime.Now.Subtract(new TimeSpan(0, 6, 0)));
                acc   = LoginAccountBusiness.GetTopByWhere(where, null, LoginAccountMySqlDAL.OrderColumn.Default);
                if (acc == null)
                {
                    return(FollowStatus.Shortage);
                }

                try
                {
                    Console.WriteLine(string.Format("使用账号{0}进行关注", acc.UserName));
                    WeiboAPI.FollowUser(acc.UserName, AuthorID);
                }
                catch (WeiboException ex)
                {
                    switch (ex.ErrorCode)
                    {
                    case "20036":
                        Console.WriteLine("关注次数IP限制");
                        break;

                    case "20003":
                        AuthorDBManager.UnSubscribeAuthor(AuthorID);
                        Logger.Error(ex.ToString());
                        acc.FailCount++;
                        if (acc.FailCount > DefaultSettings.MaxFailureCount)
                        {
                            acc.IsDisabled = true;
                        }
                        Console.WriteLine("出现\"用户不存在\"异常,已取消关注该用户,请手工确保账号有效");
                        break;

                    default:
                        acc.FailCount++;
                        if (acc.FailCount > DefaultSettings.MaxFailureCount)
                        {
                            acc.IsDisabled = true;
                        }
                        acc.FollowCountToday = DefaultSettings.FollowLimitPerDay;
                        Logger.Error(ex.ToString());
                        break;
                    }
                    return(FollowStatus.Exception);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.ToString()); return(FollowStatus.Exception);
                }
                acc.FollowCount++;
                acc.FollowCountToday++;
                acc.SubscribeLastTime = DateTime.Now;

                AuthorDBManager.SubscribeAuthor(acc.UserName, AuthorID);
                return(FollowStatus.Succ);
            }
            catch (Exception ex) { Logger.Error(ex.ToString()); return(FollowStatus.Exception); }
            finally
            {
                if (acc != null)
                {
                    where = string.Format("AccountID='{0}'", acc.AccountID);
                    var param = new LoginAccountMySqlDAL.LoginAccountParameter[5];
                    param[0] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.FollowCount, acc.FollowCount);
                    param[1] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.FollowCountToday, acc.FollowCountToday);
                    param[2] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.FailCount, acc.FailCount);
                    param[3] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.IsDisabled, acc.IsDisabled);
                    param[4] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.SubscribeLastTime, acc.SubscribeLastTime);
                    LoginAccountBusiness.UpdateWhere(param, where, null);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 定期检查账号情况
        /// </summary>
        private static void NormalizeAccount()
        {
            string where;
            try
            {
                #region 初始化账号
                where = "IsDisabled=FALSE AND SubscribeStatus>=0 AND AuthorID IS NULL";
                var result = LoginAccountBusiness.GetAllByWhere(where, null, LoginAccountMySqlDAL.OrderColumn.Default);
                foreach (var account in result)
                {
                    try
                    {
                        account.AuthorID = WeiboAPI.GetAuthorID(account.UserName);
                        var author = WeiboAPI.GetAuthorInfo(account.AuthorID);
                        var list   = WeiboAPI.GetFriendsIDs(account.AuthorID, Enums.SampleMethod.All);
                        account.FollowCount = 0;
                        foreach (var id in list)
                        {
                            AuthorDBManager.SubscribeAuthor(account.UserName, id);
                            account.FollowCount++;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(string.Format("Err@ Account: {0}, message: {1}", account.UserName, ex.ToString()));
                        account.FailCount++;
                        if (account.FailCount > DefaultSettings.MaxFailureCount)
                        {
                            account.IsDisabled = true;
                        }
                        LoginAccountBusiness.UpdateByAccountID(account);
                        continue;
                    }
                    account.FailCount                = 0;
                    account.SubscribeIntervalMins    = 15;
                    account.SubscribeLastTime        = Utilities.Epoch;
                    account.SubscribeNextTime        = Utilities.Epoch;
                    account.SubscribeSinceID         = null;
                    account.FollowCountLastClearTime = DateTime.Now;
                    account.FollowCountToday         = 0;
                    LoginAccountBusiness.UpdateByAccountID(account);
                }
                #endregion

                #region 关注次数和失效次数清零(每天一次)
                DateTime refreshtime = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0, 0));
                where  = string.Format("IsDisabled=FALSE AND SubscribeStatus>=0 AND (FollowCountLastClearTime IS NULL OR FollowCountLastClearTime<'{0}')", refreshtime);
                result = LoginAccountBusiness.GetAllByWhere(where, null, LoginAccountMySqlDAL.OrderColumn.Default);
                foreach (var account in result)
                {
                    account.FollowCountLastClearTime = DateTime.Now;
                    account.FollowCountToday         = 0;
                    account.FailCount = 0;
                    LoginAccountBusiness.UpdateByAccountID(account);
                }
                #endregion
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
            }
        }