From 8624d5c95e6611d3bbdec03cc2a878b2a14812d3 Mon Sep 17 00:00:00 2001 From: Skykey Date: Sat, 18 Dec 2021 20:11:53 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=94=B9=E8=BF=9B=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E6=8F=90=E9=86=92=EF=BC=9B2.=20=E4=BF=AE=E5=A4=8D=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=E6=8F=90=E9=86=92BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ASoulNotification.cpp | 2 +- src/BilibiliMessage.cpp | 138 +++++++++++++++++++++++++------------- src/BilibiliMessage.h | 13 ++-- src/Essential.h | 1 - version.json | 4 ++ 5 files changed, 103 insertions(+), 55 deletions(-) diff --git a/src/ASoulNotification.cpp b/src/ASoulNotification.cpp index 48a3737..265cec7 100644 --- a/src/ASoulNotification.cpp +++ b/src/ASoulNotification.cpp @@ -20,7 +20,7 @@ using namespace WinToastLib; -const QString VERSION = "1.5.0"; +const QString VERSION = "2.0.0"; static int errCount = 0; diff --git a/src/BilibiliMessage.cpp b/src/BilibiliMessage.cpp index e16df86..2e1f842 100644 --- a/src/BilibiliMessage.cpp +++ b/src/BilibiliMessage.cpp @@ -1,13 +1,22 @@ #include "BilibiliMessage.h" -BiliBiliMessage::BiliBiliMessage(std::shared_ptr& logger, QStringList uidList, QObject* parent) : +BiliBiliMessage::BiliBiliMessage(std::shared_ptr& logger, QStringList &uidList, QObject* parent) : m_uidList(uidList), m_logger(logger), QObject(parent) { qDebug() << "BilibiliMessage Starting..."; m_logger->info("哔哩哔哩查询模块初始化。"); - currentLive = -1; + + // 初始化Time Out + if(uidList.size()!=6) // 仅当用户自定义查询用户后(数量改变)更改time out + m_timeOut = 60 / uidList.size() >= 5 ? (60 / uidList.size() >= 20 ? 20 : 60 / uidList.size()) : 5; // 最短查询间隔为5s,最长为20s + qDebug() << "Time out:" << m_timeOut; + + // 初始化livestatusmap + for (size_t i = 0; i < uidList.size(); ++i) + m_liveStatusMap[uidList[i].toInt()] = 0; + qDebug() << "BilibiliMessage Started."; m_logger->info("哔哩哔哩查询模块初始化成功。"); } @@ -24,51 +33,65 @@ void BiliBiliMessage::startQuery() m_logger->info("开始查询动态。"); for (int i = 0; i < m_uidList.size(); ++i) { - //QString url = QString(BMURLPREFIX) + ASOULUID[i]; QString url = QString(BMURLPREFIX) + m_uidList[i]; - BilibiliMessageCard card = messageQuery(url); - if (!card.is_null) + BiliBiliMessageRes res = messageQuery(url); // 查询一组动态 + if (!res.empty()) { - if (!oldMessageCardMap.contains(card.uid)) + // 判断当前是否为首次查询 + if(!m_oldMessageCardMap.contains(res[0].uid)) { - oldMessageCardMap.insert(card.uid, card); + // 首次查询,填充oldMessageCardMap + QSet messageSet; + for(size_t i=0;ierror("查询成员动态时发生错误,返回了空的消息卡片!"); + QString errorString = "查询成员动态时发生错误,返回了空的消息卡片组!"; + m_logger->error("查询成员动态时发生错误,返回了空的消息卡片组!"); emit errorOccurred(errorString); } // 延时 - QThread::sleep(5); + QThread::sleep(m_timeOut); } m_logger->info("开始查询直播。"); for (int i = 0; i < m_uidList.size(); ++i) { - //QString url = QString(BLURLPREFIX) + ASOULUID[i]; QString url = QString(BLURLPREFIX) + m_uidList[i]; BilibiliLiveCard card = liveQuery(url); if (!card.is_null) { - if (card.status == 1) + if(card.status!=m_liveStatusMap[card.mid]) { - if (currentLive != card.mid) - { - currentLive = card.mid; + // 与历史状态不同 + if(card.status==1) // 正在直播 emit newBilibiliLive(card.mid, card.title, card.url); - } + + m_liveStatusMap[card.mid] = card.status; // 更新liveStatusMap } } else @@ -78,9 +101,9 @@ void BiliBiliMessage::startQuery() emit errorOccurred(errorString); } // 延时 - QThread::sleep(5); + QThread::sleep(m_timeOut); } - QThread::sleep(5); + QThread::sleep(m_timeOut); //errCnt += 2; //if (errCnt < 6) //{ @@ -99,10 +122,10 @@ void BiliBiliMessage::startQuery() } } -BilibiliMessageCard BiliBiliMessage::messageQuery(const QString& url) +BiliBiliMessageRes BiliBiliMessage::messageQuery(const QString& url) { - BilibiliMessageCard ret; - ret.is_null = true; + BiliBiliMessageRes res; + //ret.is_null = true; Json doc = getJson(url); if (doc.is_null()) @@ -111,42 +134,63 @@ BilibiliMessageCard BiliBiliMessage::messageQuery(const QString& url) QString errorString = "查询成员动态时发生错误,获取的JSON值为空!"; m_logger->error("查询成员动态时发生错误,获取的JSON值为空!"); //emit errorOccurred(errorString); - return ret; + return res; } /* []操作符理应有异常机制,对异常机制进行处理 * JSON_THROW(std::out_of_range("key not found")); */ - int uid; - int type; - QString dynamic_id_str; - QString nickname; - try + + if(doc.contains("data")&&doc["data"].contains("cards")) { - uid = doc["data"]["cards"][0]["desc"]["uid"].get(); - type = doc["data"]["cards"][0]["desc"]["type"].get(); - dynamic_id_str = QString::fromStdString(doc["data"]["cards"][0]["desc"]["dynamic_id_str"].get()); - nickname = QString::fromStdString(doc["data"]["cards"][0]["desc"]["user_profile"]["info"]["uname"].get()); + Json cardArr = doc["data"]["cards"]; + int uid = cardArr[0]["desc"]["uid"].get(); + QString nickname = QString::fromStdString(cardArr[0]["desc"]["user_profile"]["info"]["uname"].get()); + int type; + QString dynamic_id_str; + + qDebug() << tr("Start query %1 %2's message card.").arg(QString::number(uid)).arg(nickname); + m_logger->info("开始查询用户UID:{},昵称:{}的一组动态:",uid,nickname.toStdString()); + for(size_t i=0;i(); + dynamic_id_str = QString::fromStdString(currentCardJson["desc"]["dynamic_id_str"].get()); + + currentMessageCard.uid = uid; + currentMessageCard.type = type; + currentMessageCard.dynamic_id_str = dynamic_id_str; + currentMessageCard.nickname = nickname; + + qDebug() << "Message Query: " << currentMessageCard.uid << currentMessageCard.nickname << currentMessageCard.type << currentMessageCard.dynamic_id_str; + m_logger->info("动态消息卡片:UID:{},昵称:{},类型:{},动态ID:{}", currentMessageCard.uid, currentMessageCard.nickname.toStdString(), currentMessageCard.type, currentMessageCard.dynamic_id_str.toStdString()); + + res.push_back(currentMessageCard); + } + catch (...) + { + qDebug() << "Message Query Error. Reason: The json can not be parsed"; + QString errorString = "查询成员动态时发生错误,未能解析获取的JSON!"; + m_logger->error("查询成员动态时发生错误,未能解析获取的JSON!"); + //emit errorOccurred(errorString); + return res; + } + } } - catch (...) + else { qDebug() << "Message Query Error. Reason: The json can not be parsed"; QString errorString = "查询成员动态时发生错误,未能解析获取的JSON!"; m_logger->error("查询成员动态时发生错误,未能解析获取的JSON!"); //emit errorOccurred(errorString); - return ret; + return res; } - ret.uid = uid; - ret.type = type; - ret.dynamic_id_str = dynamic_id_str; - ret.nickname = nickname; - ret.is_null = false; - - qDebug() << "Message Query: " << ret.uid << ret.nickname << ret.type << ret.dynamic_id_str; - m_logger->info("动态消息卡片:UID:{},昵称:{},类型:{},动态ID:{}", ret.uid, ret.nickname.toStdString(), ret.type, ret.dynamic_id_str.toStdString()); - - return ret; + return res; } BilibiliLiveCard BiliBiliMessage::liveQuery(const QString& url) diff --git a/src/BilibiliMessage.h b/src/BilibiliMessage.h index f5bd465..4b5573b 100644 --- a/src/BilibiliMessage.h +++ b/src/BilibiliMessage.h @@ -16,12 +16,13 @@ #include using Json = nlohmann::json; +using BiliBiliMessageRes=QVector; class BiliBiliMessage: public QObject { Q_OBJECT public: - BiliBiliMessage(std::shared_ptr& logger, QStringList uidList, QObject* parent = nullptr); + BiliBiliMessage(std::shared_ptr& logger, QStringList &uidList, QObject* parent = nullptr); void startQuery(); signals: @@ -30,16 +31,16 @@ class BiliBiliMessage: public QObject void errorOccurred(const QString errorString); private: - BilibiliMessageCard messageQuery(const QString& url); + BiliBiliMessageRes messageQuery(const QString& url); // ÿβѯһ鶯̬ BilibiliLiveCard liveQuery(const QString& url); Json getJson(const QString& url); private: - QMap oldMessageCardMap; - int currentLive; + QMap> m_oldMessageCardMap; // ÿûӦһɶ̬ + QMap m_liveStatusMap; // ÿûӦһֱ״̬ std::shared_ptr& m_logger; - QStringList m_uidList; - //int errCnt = 0; + QStringList &m_uidList; + int m_timeOut=5; }; diff --git a/src/Essential.h b/src/Essential.h index 7b00100..ef4cc44 100644 --- a/src/Essential.h +++ b/src/Essential.h @@ -13,7 +13,6 @@ typedef struct BilibiliMessageCard QString dynamic_id_str; int uid; int type; - bool is_null; }BilibiliMessageCard; typedef struct BilibiliLiveCard diff --git a/version.json b/version.json index b31182b..6febb40 100644 --- a/version.json +++ b/version.json @@ -1,5 +1,9 @@ { "versions": [ + { + "version": "v2.0.0", + "url": "https://github.com/skykeyjoker/A-Soul-Notification/releases/tag/v2.0.0" + }, { "version": "v1.5.0", "url": "https://github.com/skykeyjoker/A-Soul-Notification/releases/tag/v1.5.0"