/// <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> /// <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="account">用来订阅的僵尸账号</param> /// <param name="result">返回结果</param> public static void FetchStatus(LoginAccountEntity account, List <NetDimension.Weibo.Entities.status.Entity> result) { int cur = 1; string sinceID = account.SubscribeSinceID; do { try { RequireAccPermission(1); var statuslist = GetSpecificClient(account.UserName).API.Entity.Statuses.HomeTimeline(sinceID, "", 50, cur++); if (statuslist == null || statuslist.Statuses == null) { break; } foreach (var status in statuslist.Statuses) { result.Add(status); try { //更新sinceID if (string.IsNullOrEmpty(account.SubscribeSinceID) || Int64.Parse(status.ID) > Int64.Parse(account.SubscribeSinceID)) { account.SubscribeSinceID = status.ID; } } catch (Exception) { throw new WeiboException("ID数字格式错误"); } } if (statuslist.NextCursor == "0" || statuslist.NextCursor == null) { break; } } catch (WeiboException ex) { if (ex.ErrorCode == "00000") { break; } else { throw ex; } } } while (true); }
public string DoOneJob(IPipeline Pipeline) { int SuccCount = 0, ErrCount = 0; DateTime nextWorkTime = Utilities.Epoch; while (!StopFlag) { if (DateTime.Now > nextWorkTime) { LoginAccountEntity account = GetNextJob(); if (account != null) { try { //刷新订阅的微博 List <NetDimension.Weibo.Entities.status.Entity> result = new List <NetDimension.Weibo.Entities.status.Entity>(); try { SendMsg(string.Format("正在刷新{0}关注的最新微博", account.UserName)); WeiboAPI.FetchStatus(account, result); } catch (IOException) { ErrCount++; nextWorkTime = WeiboAPI.rateLimitStatus.ResetTime; } catch (Exception ex) { SendMsg("获取新微博时发生错误,见日志"); ErrCount++; nextWorkTime = WeiboAPI.rateLimitStatus.ResetTime; SendMsg(ex.ToString()); Logger.Error(ex.ToString()); } SendMsg(string.Format("{0}关注的微博抓取到{1}条,开始插入", account.UserName, result.Count)); for (int i = 0; i < result.Count; ++i) { var item = ItemDBManager.ConvertToItem(result[i], Enums.AuthorSource.PublicLeader, CrawlID); ItemDBManager.InsertOrUpdateItem(item); CntData.Tick(); } SendMsg(string.Format("{0}的关注微博刷新任务完成", account.UserName)); SuccCount++; continue; } catch (Exception ex) { ErrCount++; nextWorkTime = WeiboAPI.rateLimitStatus.ResetTime; SendMsg(ex.ToString()); Logger.Error(ex.ToString()); } finally { AccountDBManager.PushbackSubscribeJob(account); } } } SendMsg(string.Format("休息{0}秒", IntervalMS / 1000)); Thread.Sleep(IntervalMS); } StopFlag = false; return(SuccCount == 0 && ErrCount == 0 ? "Nothing to do" : string.Format("OneJob Done. Succ {0} Err {1}", SuccCount, ErrCount)); }
/// <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); } } }