/// <summary> /// 获取下一个用僵尸刷订阅微博的任务 /// </summary> /// <returns>满足任务触发条件的僵尸账户</returns> public static LoginAccountEntity GetNextSubscribeJob() { LoginAccountEntity acc = null; string where; try { where = string.Format("IsDisabled=FALSE AND SubscribeStatus>=0 AND Status={0} AND FollowCount>0 AND (SubscribeNextTime IS NULL OR SubscribeNextTime<'{1}')", (sbyte)Enums.CrawlStatus.Normal, DateTime.Now); acc = LoginAccountBusiness.GetTopByWhere(where, null, Palas.Common.Lib.DAL.LoginAccountMySqlDAL.OrderColumn.Default); if (acc == null) { return(null); } acc.Status = (sbyte)Enums.CrawlStatus.Crawling; return(acc); } catch (Exception ex) { Logger.Error(ex.ToString()); return(null); } finally { if (acc != null) { where = string.Format("AccountID='{0}'", acc.AccountID); var param = new LoginAccountMySqlDAL.LoginAccountParameter[1]; param[0] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.Status, (sbyte)Enums.CrawlStatus.Crawling); LoginAccountBusiness.UpdateWhere(param, where, null); } } }
/// <summary> /// 取消对某个用户的关注 /// </summary> /// <param name="username">关注某个用户的僵尸账号</param> /// <param name="targetAuthorID">需要被取消的用户ID</param> /// <returns>是否取消成功</returns> public static bool UnFollowUser(string targetAuthorID) { string username = AuthorDBManager.GetUsernameFollowing(targetAuthorID); if (username == null) { return(false); } string where = string.Format("UserName='******'", username); LoginAccountEntity acc = null; try { acc = LoginAccountBusiness.GetTopByWhere(where, null, LoginAccountMySqlDAL.OrderColumn.Default); if (acc == null) { return(false); } try { WeiboAPI.UnFollowUser(username, targetAuthorID); } catch (WeiboException ex) { Logger.Error(ex.ToString()); acc.FailCount++; if (acc.FailCount > DefaultSettings.MaxFailureCount) { acc.IsDisabled = true; } return(false); } acc.FollowCount--; AuthorDBManager.UnSubscribeAuthor(targetAuthorID); return(true); } catch (Exception ex) { Logger.Error(ex.ToString()); return(false); } finally { if (acc != null) { var param = new LoginAccountMySqlDAL.LoginAccountParameter[3]; 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.FailCount, acc.FailCount); param[2] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.IsDisabled, acc.IsDisabled); LoginAccountBusiness.UpdateWhere(param, where, null); } } }
/// <summary> /// 初始化所有任务 /// </summary> public static void SetAllSinceIDToNull() { string where = "SubscribeStatus>=0"; var accList = LoginAccountBusiness.GetAllByWhere(where, null, LoginAccountMySqlDAL.OrderColumn.Default); foreach (var acc in accList) { where = string.Format("AccountID='{0}'", acc.AccountID); var param = new LoginAccountMySqlDAL.LoginAccountParameter[3]; param[0] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.SubscribeSinceID, null); param[1] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.SubscribeNextTime, Utilities.Epoch); param[2] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.Status, Enums.CrawlStatus.Normal); LoginAccountBusiness.UpdateWhere(param, where, null); } }
/// <summary> /// 僵尸刷订阅微博的任务完成后,处理后事 /// </summary> /// <param name="a">僵尸账户</param> public static void PushbackSubscribeJob(LoginAccountEntity acc) { try { string where = string.Format("AccountID='{0}'", acc.AccountID); var param = new LoginAccountMySqlDAL.LoginAccountParameter[4]; param[0] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.Status, (sbyte)Enums.CrawlStatus.Normal); param[1] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.SubscribeLastTime, DateTime.Now); param[2] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.SubscribeNextTime, DateTime.Now.AddMinutes(acc.SubscribeIntervalMins)); param[3] = new LoginAccountMySqlDAL.LoginAccountParameter(Palas.Common.Lib.DTO.LoginAccountDTO.Column.SubscribeSinceID, acc.SubscribeSinceID); LoginAccountBusiness.UpdateWhere(param, where, null); } catch (Exception ex) { Logger.Error(ex.ToString()); } }
/// <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); } } }