/// <summary> /// 由于每天每个账号至多订阅数百个用户,所以只需要开一个线程 /// </summary> /// <param name="Pipeline"></param> /// <returns></returns> public string DoOneJob(IPipeline Pipeline) { int SuccCount = 0, ErrCount = 0; DateTime nextWorkTime = Utilities.Epoch; while (!StopFlag) { if (DateTime.Now > nextWorkTime) { string authorID = GetNextJob(); if (authorID != null) { try { SendMsg("待关注用户: " + authorID); var status = AccountDBManager.FollowUser(authorID); if (status == AccountDBManager.FollowStatus.Succ) { SendMsg("关注用户成功"); CntData.Tick(); SuccCount++; } else { if (status == AccountDBManager.FollowStatus.Exception) { SendMsg("出现异常,休息1小时后再尝试"); nextWorkTime = DateTime.Now.AddHours(1); } else { SendMsg("账号关注频率受限,1分钟后再试"); Thread.Sleep(1000 * 60); } } } catch (Exception ex) { nextWorkTime = DateTime.Now.AddHours(1); Logger.Error(ex.ToString()); ErrCount++; } } else { SendMsg("所有任务已完成"); } } Thread.Sleep(IntervalMS); } StopFlag = false; return(SuccCount == 0 && ErrCount == 0 ? "Nothing to do" : string.Format("OneJob Done. Succ {0} Err {1}", SuccCount, ErrCount)); }
private LoginAccountEntity GetNextJob() { lock (jobLock) { SendMsg("正在获取下一任务"); var acc = AccountDBManager.GetNextSubscribeJob(); if (acc != null) { SendMsg("获取到" + acc.UserName + "的任务"); } else { SendMsg("没有获取到任务,休息一会"); } return(acc); } }
/// <summary> /// 获取特定的僵尸以执行任务,如订阅、关注等 /// </summary> /// <param name="username">僵尸用户名</param> /// <returns>特定僵尸Client</returns> private static Client GetSpecificClient(string username) { lock (clientCacheLock) { if (clientCache.ContainsKey(username)) { return(clientCache[username]); } string password = AccountDBManager.GetPassword(username); RequireAccPermission(3); OAuth o = new OAuth(DefaultSettings.AppKey, DefaultSettings.AppSecret, String.Empty); o.ClientLogin(username, password); Client client = new Client(o); clientCache.Add(username, client); return(client); } }
private async void INSTANCE_AccountChanged(AccountDBManager handler, AccountChangedEventArgs args) { await CLIENT_SEMA.WaitAsync(); for (int i = 0; i < CLIENTS.Count; i++) { if (Equals(CLIENTS[i].getXMPPAccount().getBareJid(), args.ACCOUNT.getBareJid())) { // Disconnect first: await CLIENTS[i].disconnectAsync(); if (args.REMOVED) { unsubscribeFromEvents(CLIENTS[i]); CLIENTS.RemoveAt(i); } else { CLIENTS[i].setAccount(args.ACCOUNT); if (!CLIENTS[i].getXMPPAccount().disabled) { await CLIENTS[i].connectAsync(); } } CLIENT_SEMA.Release(); return; } } // Account got added: if (!args.REMOVED) { XMPPClient client = loadAccount(args.ACCOUNT); if (!client.getXMPPAccount().disabled) { await client.connectAsync(); } CLIENTS.Add(client); } CLIENT_SEMA.Release(); }
//--------------------------------------------------------Events:---------------------------------------------------------------------\\ #region --Events-- private async void INSTANCE_AccountChanged(AccountDBManager handler, AccountChangedEventArgs args) { await CLIENT_SEMA.WaitAsync(); for (int i = 0; i < CLIENTS.Count; i++) { if (Equals(CLIENTS[i].GetBareJid(), args.ACCOUNT.getBareJid())) { // Disconnect first: CLIENTS[i].DisconnectAsync().Wait(); if (args.REMOVED) { CLIENTS[i].ClientConnected -= OnClientConnected; CLIENTS.RemoveAt(i); } else { CLIENTS[i].SetAccount(args.ACCOUNT); if (!CLIENTS[i].IsDisabled()) { await CLIENTS[i].ConnectAsync(); } } CLIENT_SEMA.Release(); return; } } // Account got added: if (!args.REMOVED) { ClientConnectionHandler client = new ClientConnectionHandler(args.ACCOUNT); if (!client.IsDisabled()) { client.ConnectAsync().Wait(); } CLIENTS.Add(client); } CLIENT_SEMA.Release(); }
private string GetNextJob() { return(AccountDBManager.GetNextFollowJob()); }
public static void BackToOrigin() { AccountDBManager.SetAllSinceIDToNull(); }
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"></param> public static void UnFollowAuthor(string AuthorID) { AccountDBManager.UnFollowUser(AuthorID); }
private void INSTANCE_AccountChanged(AccountDBManager handler, AccountChangedEventArgs args) { loadAccounts(); }