private void getNewChapter(object sender, System.EventArgs e) { lock (lockObj) { //获取更新的书籍信息 List <Book> bookList = ReadXml.GetBooksData(); //遍历每一本书籍 foreach (Book book in bookList) { //起点最新章节 Chapter newChapter = getCatalog(book.Id); //本地存储的最新章节 Chapter latestChapter = readLatestChapter(book.Code); //如果起点的最新章节发布时间要大于本地的最新章节发布时间,说明已更新 if (latestChapter == null || DateTime.Parse(newChapter.ChapterTime) > DateTime.Parse(latestChapter.ChapterTime)) { foreach (Group g in book.Group) { CQApi.SendGroupMessage(long.Parse(g.GroupNo), (g.IsAtAll ? CQApi.CQCode_AtAll().ToSendString() + "\n" : string.Empty) + (g.IsSendImage ? CQApi.CQCode_Image(g.ImageName).ToSendString() + "\n" : string.Empty) + "最新章节:\"" + newChapter.ChapterName + "\" \n发布时间:" + newChapter.ChapterTime + " \n本章字数:" + newChapter.WordNumber); } //把最新章节信息写入本地 writeLatestChapter(newChapter, book.Code); } //本次获取的最新章节名称 LogHelper.WriteMsgInLog("书名:" + book.Name + ",章节名称:" + newChapter.ChapterName, book.Code); } } }
private static Task GetDynamic(CQApi cqApi, long biliUser, List <long> groupId, SubscriptionDBHelper dbHelper) { string message; Dynamic biliDynamic; //获取动态文本 try { JObject cardData = DynamicAPIs.GetBiliDynamicJson((ulong)biliUser, out CardType cardType); switch (cardType) { //检查动态类型 case CardType.PlainText: PlainTextCard plainTextCard = new PlainTextCard(cardData) { ContentType = ContentType.CQCode }; message = plainTextCard.ToString(); biliDynamic = plainTextCard; break; case CardType.TextAndPic: TextAndPicCard textAndPicCard = new TextAndPicCard(cardData) { ContentType = ContentType.CQCode }; message = textAndPicCard.ToString(); biliDynamic = textAndPicCard; break; default: ConsoleLog.Debug("动态获取", $"ID:{biliUser}的动态获取成功,动态类型未知"); return(Task.CompletedTask); } } catch (Exception e) { ConsoleLog.Error("获取动态更新时发生错误", ConsoleLog.ErrorLogBuilder(e)); return(Task.CompletedTask); } //获取用户信息 UserInfo sender = biliDynamic.GetUserInfo(); ConsoleLog.Info("动态获取", $"{sender.UserName}的动态获取成功"); //检查是否是最新的 List <long> targetGroups = new List <long>(); foreach (long group in groupId) { //检查是否已经发送过消息 if (!dbHelper.IsLatest(group, sender.Uid, biliDynamic.UpdateTime)) { targetGroups.Add(group); } } //没有群需要发送消息 if (targetGroups.Count == 0) { ConsoleLog.Info("动态获取", $"{sender.UserName}的动态已是最新"); return(Task.CompletedTask); } //向未发生消息的群发送消息 string messageToSend = MsgBuilder(sender, message, biliDynamic); foreach (long targetGroup in targetGroups) { ConsoleLog.Info("动态获取", $"向群{targetGroup}发送动态信息"); cqApi.SendGroupMessage(targetGroup, messageToSend); dbHelper.Update(targetGroup, sender.Uid, biliDynamic.UpdateTime); } return(Task.CompletedTask); }