Skip to content

Commit

Permalink
PGC Index Filters: speed up http request
Browse files Browse the repository at this point in the history
  • Loading branch information
xfangfang committed Oct 13, 2024
1 parent 4ecdfec commit eda1134
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 99 deletions.
92 changes: 47 additions & 45 deletions wiliwili/include/api/bilibili/util/http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ using ErrorCallback = std::function<void(const std::string&, int code)>;
#endif
#define CALLBACK(data) \
if (callback) callback(data)
#define CPR_HTTP_BASE \
cpr::HttpVersion{cpr::HttpVersionCode::VERSION_2_0_TLS}, cpr::Timeout{bilibili::HTTP::TIMEOUT}, \
bilibili::HTTP::HEADERS, bilibili::HTTP::COOKIES, bilibili::HTTP::PROXIES, bilibili::HTTP::VERIFY

class HTTP {
public:
Expand Down Expand Up @@ -57,8 +60,7 @@ class HTTP {
}
callback(r);
},
cpr::Url{url}, parameters, payload, HTTP::HEADERS, HTTP::COOKIES, HTTP::PROXIES, HTTP::VERIFY,
cpr::HttpVersion{cpr::HttpVersionCode::VERSION_2_0_TLS}, cpr::Timeout{HTTP::TIMEOUT});
cpr::Url{url}, parameters, payload, CPR_HTTP_BASE);
}

static void __cpr_get(const std::string& url, const cpr::Parameters& parameters = {},
Expand All @@ -75,58 +77,58 @@ class HTTP {
}
callback(r);
},
cpr::Url{url}, parameters, HTTP::HEADERS, HTTP::COOKIES, HTTP::PROXIES, HTTP::VERIFY,
cpr::HttpVersion{cpr::HttpVersionCode::VERSION_2_0_TLS}, cpr::Timeout{HTTP::TIMEOUT});
cpr::Url{url}, parameters, CPR_HTTP_BASE);
}

template <typename ReturnType>
static int parseJson(const cpr::Response& r, const std::function<void(ReturnType)>& callback = nullptr,
const ErrorCallback& error = nullptr) {
try {
nlohmann::json res = nlohmann::json::parse(r.text);
int code = res.at("code").get<int>();
if (code == 0) {
if (res.contains("data") && (res.at("data").is_object() || res.at("data").is_array())) {
CALLBACK(res.at("data").get<ReturnType>());
return 0;
} else if (res.contains("result") && res.at("result").is_object()) {
CALLBACK(res.at("result").get<ReturnType>());
return 0;
} else {
printf("data: %s\n", r.text.c_str());
ERROR_MSG("Cannot find data", -1);
}
} else if (res.at("message").is_string()) {
ERROR_MSG(res.at("message").get<std::string>(), code);
} else {
ERROR_MSG("Param error", -1);
}
} catch (const std::exception& e) {
ERROR_MSG("Api error. \n" + std::string{e.what()}, 200);
printf("data: %s\n", r.text.c_str());
printf("ERROR: %s\n", e.what());
}
return 1;
}

static void signParameters(cpr::Parameters& parameters) {
parameters.Add({{"appkey", BILIBILI_APP_KEY},
{"build", BILIBILI_BUILD},
{"ts", std::to_string(wiliwili::getUnixTime() * 1000)}});
std::vector<std::string> kv;
pystring::split(parameters.GetContent(cpr::CurlHolder()), kv, "&");
std::sort(kv.begin(), kv.end());
parameters.Add({{"sign", websocketpp::md5::md5_hash_hex(pystring::join("&", kv) + BILIBILI_APP_SECRET)}});
}

template <typename ReturnType>
static void getResultAsync(const std::string& url, cpr::Parameters parameters = {},
const std::function<void(ReturnType)>& callback = nullptr,
const ErrorCallback& error = nullptr, bool needSign = false) {
if (needSign) {
parameters.Add({{"appkey", BILIBILI_APP_KEY},
{"build", BILIBILI_BUILD},
{"ts", std::to_string(wiliwili::getUnixTime() * 1000)}});
std::vector<std::string> kv;
pystring::split(parameters.GetContent(cpr::CurlHolder()), kv, "&");
std::sort(kv.begin(), kv.end());
parameters.Add({{"sign", websocketpp::md5::md5_hash_hex(pystring::join("&", kv) + BILIBILI_APP_SECRET)}});
signParameters(parameters);
}
__cpr_get(
url, parameters,
[callback, error](const cpr::Response& r) {
try {
nlohmann::json res = nlohmann::json::parse(r.text);
int code = res.at("code").get<int>();
if (code == 0) {
if (res.contains("data") && (res.at("data").is_object() || res.at("data").is_array())) {
CALLBACK(res.at("data").get<ReturnType>());
} else if (res.contains("result") && res.at("result").is_object()) {
CALLBACK(res.at("result").get<ReturnType>());
} else {
printf("data: %s\n", r.text.c_str());
ERROR_MSG("Cannot find data", -1);
}
return;
}

if (res.at("message").is_string()) {
ERROR_MSG(res.at("message").get<std::string>(), code);
} else {
ERROR_MSG("Param error", -1);
}
} catch (const std::exception& e) {
if (r.error) {
ERROR_MSG(r.error.message, -1);
} else if (r.status_code == 200) {
ERROR_MSG("Api error. \n" + std::string{e.what()}, 200);
} else {
ERROR_MSG("Network error. \nStatus code: " + std::to_string(r.status_code), r.status_code);
}
printf("data: %s\n", r.text.c_str());
printf("ERROR: %s\n", e.what());
}
},
url, parameters, [callback, error](const cpr::Response& r) { parseJson<ReturnType>(r, callback, error); },
error);
}

Expand Down
13 changes: 11 additions & 2 deletions wiliwili/source/activity/pgc_index_activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ class IndexItem : public brls::Box {
public:
IndexItem() {
content = new brls::Label();
if (brls::Application::ORIGINAL_WINDOW_HEIGHT < 720) {
content->setFontSize(16);
this->setHeight(28);
} else {
this->setHeight(36);
}
this->setHideHighlightBackground(true);
this->setHeight(36);
this->setHighlightCornerRadius(6);
this->setMargins(2, 6, 2, 6);
this->setCornerRadius(4);
Expand Down Expand Up @@ -79,7 +84,11 @@ class IndexRow : public brls::Box {
public:
IndexRow(bilibili::PGCIndexFilter data) : data(data) {
YGNodeStyleSetFlexWrap(this->ygNode, YGWrap::YGWrapWrap);
this->setMargins(6, 0, 6, 0);
if (brls::Application::ORIGINAL_WINDOW_HEIGHT < 720) {
this->setMargins(2, 0, 2, 0);
} else {
this->setMargins(6, 0, 6, 0);
}
this->key = data.field;
for (size_t i = 0; i < data.values.size(); i++) {
auto item = new IndexItem();
Expand Down
81 changes: 39 additions & 42 deletions wiliwili/source/api/home_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,49 +191,46 @@ void BilibiliClient::get_pgc_filter(const std::string& index_type,
/// 主页 追番/影视 获取全部分类
void BilibiliClient::get_pgc_all_filter(const std::function<void(PGCIndexFilters)>& callback,
const ErrorCallback& error) {
PGCIndexFilters res;
BilibiliClient::get_pgc_filter(
"1",
[callback, error, res](auto wrapper) mutable {
wrapper.index_name = "追番";
res["1"] = wrapper;
BilibiliClient::get_pgc_filter(
"2",
[callback, error, res](auto wrapper) mutable {
wrapper.index_name = "电影";
res["2"] = wrapper;
BilibiliClient::get_pgc_filter(
"5",
[callback, error, res](auto wrapper) mutable {
wrapper.index_name = "电视剧";
res["5"] = wrapper;
BilibiliClient::get_pgc_filter(
"3",
[callback, error, res](auto wrapper) mutable {
wrapper.index_name = "纪录片";
res["3"] = wrapper;
BilibiliClient::get_pgc_filter(
"7",
[callback, error, res](auto wrapper) mutable {
wrapper.index_name = "综艺";
res["7"] = wrapper;
BilibiliClient::get_pgc_filter(
"102",
[callback, res](auto wrapper) mutable {
wrapper.index_name = "影视综合";
res["102"] = wrapper;
callback(res);
},
error);
},
error);
},
error);
},
error);
cpr::async([callback, error]() {
std::vector<std::string> index_types = {"1", "2", "5", "3", "7", "102"};
std::vector<std::string> index_names = {"追番", "电影", "电视剧", "纪录片", "综艺", "影视综合"};
cpr::MultiPerform multiperform;
for (auto& i : index_types) {
std::shared_ptr<cpr::Session> session = std::make_shared<cpr::Session>();
session->SetUrl(Api::PGCIndexFilter);
session->SetParameters({{"type", "2"}, {"index_type", i}});
session->SetHeader(HTTP::HEADERS);
session->SetVerifySsl(HTTP::VERIFY);
session->SetProxies(HTTP::PROXIES);
session->SetTimeout(HTTP::TIMEOUT);
session->SetCookies(HTTP::COOKIES);
session->SetHttpVersion(cpr::HttpVersion{cpr::HttpVersionCode::VERSION_2_0_TLS});
multiperform.AddSession(session);
}

std::vector<cpr::Response> responses = multiperform.Get();
PGCIndexFilters res;
for (size_t i = 0; i < responses.size(); i++) {
auto& r = responses[i];
if (r.error) {
ERROR_MSG(r.error.message, -1);
return;
} else if (r.status_code != 200) {
ERROR_MSG("Network error. [Status code: " + std::to_string(r.status_code) + " ]", r.status_code);
return;
}
int ret = HTTP::parseJson<PGCIndexFilterWrapper>(
r,
[&res, &index_types, &index_names, i, callback](auto wrapper) {
wrapper.index_name = index_names[i];
res[index_types[i]] = wrapper;
},
error);
},
error);
if (ret != 0) {
return;
}
}
callback(res);
});
}
} // namespace bilibili
3 changes: 1 addition & 2 deletions wiliwili/source/api/util/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
namespace bilibili {

cpr::Response HTTP::get(const std::string& url, const cpr::Parameters& parameters, int timeout) {
return cpr::Get(cpr::Url{url}, parameters, HTTP::HEADERS, HTTP::COOKIES, HTTP::PROXIES, HTTP::VERIFY,
cpr::Timeout{timeout});
return cpr::Get(cpr::Url{url}, parameters, CPR_HTTP_BASE);
}

}; // namespace bilibili
12 changes: 4 additions & 8 deletions wiliwili/source/api/video_detail_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ void BilibiliClient::get_webmask(const std::string& url, int64_t rangeStart, int
ERROR_MSG("Network error. [Status code: " + std::to_string(r.status_code) + " ]", r.status_code);
}
},
cpr::Range{start, end}, HTTP::VERIFY, HTTP::PROXIES, cpr::Url{url}, HTTP::HEADERS, HTTP::COOKIES,
cpr::Timeout{HTTP::TIMEOUT});
cpr::Range{start, end}, cpr::Url{url}, CPR_HTTP_BASE);
}

void BilibiliClient::get_video_pagelist(const std::string& bvid,
Expand Down Expand Up @@ -267,8 +266,7 @@ void BilibiliClient::get_danmaku(uint64_t cid, const std::function<void(std::str
printf("ERROR: %s\n", e.what());
}
},
HTTP::VERIFY, HTTP::PROXIES, cpr::Url{Api::VideoDanmaku}, HTTP::HEADERS,
cpr::Parameters({{"oid", std::to_string(cid)}}), HTTP::COOKIES, cpr::Timeout{HTTP::TIMEOUT});
cpr::Url{Api::VideoDanmaku}, cpr::Parameters({{"oid", std::to_string(cid)}}), CPR_HTTP_BASE);
}

void BilibiliClient::get_highlight_progress(uint64_t cid,
Expand All @@ -287,8 +285,7 @@ void BilibiliClient::get_highlight_progress(uint64_t cid,
ERROR_MSG(e.what(), -1);
}
},
HTTP::VERIFY, HTTP::PROXIES, cpr::Url{Api::VideoHighlight}, HTTP::HEADERS,
cpr::Parameters({{"cid", std::to_string(cid)}}), HTTP::COOKIES, cpr::Timeout{HTTP::TIMEOUT});
cpr::Url{Api::VideoHighlight}, cpr::Parameters({{"cid", std::to_string(cid)}}), CPR_HTTP_BASE);
}

void BilibiliClient::get_subtitle(const std::string& link, const std::function<void(SubtitleData)>& callback,
Expand All @@ -309,8 +306,7 @@ void BilibiliClient::get_subtitle(const std::string& link, const std::function<v
printf("ERROR: %s\n", e.what());
}
},
HTTP::VERIFY, HTTP::PROXIES, cpr::Url{url}, HTTP::HEADERS, cpr::Parameters({}), HTTP::COOKIES,
cpr::Timeout{HTTP::TIMEOUT});
cpr::Url{url}, cpr::Parameters({}), CPR_HTTP_BASE);
}

/// 视频页 上报历史记录
Expand Down

0 comments on commit eda1134

Please sign in to comment.