mirror of
https://github.com/HappyTanuki/BumbleCee.git
synced 2025-10-26 09:55:14 +00:00
비동기 다운로드 구현 완료
This commit is contained in:
@@ -1,25 +1,69 @@
|
||||
#include <AsyncDownloadManager.hpp>
|
||||
#include <sstream>
|
||||
|
||||
namespace bumbleBee {
|
||||
|
||||
void AsyncDownloadManager::enqueueAsyncDL(std::string query) {
|
||||
dlQueueMutex.lock();
|
||||
std::lock_guard<std::mutex> lock(dlQueueMutex);
|
||||
downloadQueue.push(query);
|
||||
dlQueueMutex.unlock();
|
||||
dlQueueCondition.notify_one();
|
||||
}
|
||||
|
||||
std::string getResultFromCommand(std::string cmd) {
|
||||
std::string result;
|
||||
FILE* stream;
|
||||
const int maxBuffer = 12; // 버퍼의 크기는 적당하게
|
||||
char buffer[maxBuffer];
|
||||
cmd.append(" 2>&1"); // 표준에러를 표준출력으로 redirect
|
||||
|
||||
stream = popen(cmd.c_str(), "r"); // 주어진 command를 shell로 실행하고 파이프 연결 (fd 반환)
|
||||
if (stream) {
|
||||
while (fgets(buffer, maxBuffer, stream) != NULL) result.append(buffer); // fgets: fd (stream)를 길이 (maxBuffer)만큼 읽어 버퍼 (buffer)에 저장
|
||||
pclose(stream); // 파이프 닫는 것 잊지 마시고요!
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void AsyncDownloadManager::downloadWorker() {
|
||||
std::unique_lock<std::mutex> dlQueueLock(dlQueueMutex);
|
||||
std::ostringstream tid;
|
||||
tid << std::this_thread::get_id();
|
||||
while (true) {
|
||||
//mutex lock
|
||||
dlQueueLock.lock();
|
||||
dlQueueCondition.wait(dlQueueLock, [&]{ return !downloadQueue.empty(); });
|
||||
|
||||
system(("./yt-dlp --no-clean-info-json --write-info-json --default-search ytsearch \
|
||||
--flat-playlist --skip-download --quiet --ignore-errors -f ba* " + downloadQueue.front()).c_str());
|
||||
std::unique_lock<std::mutex> dlQueueLock(dlQueueMutex);
|
||||
dlQueueCondition.wait(dlQueueLock, [&]{ return !downloadQueue.empty() || terminate; });
|
||||
std::string query = downloadQueue.front();
|
||||
downloadQueue.pop();
|
||||
dlQueueLock.unlock();
|
||||
auto cluster = bot.lock();
|
||||
assert(cluster);
|
||||
|
||||
cluster->log(dpp::ll_info, "Enqueuing " + query + " accepted.");
|
||||
|
||||
std::string idstring = getResultFromCommand("./yt-dlp --default-search ytsearch --flat-playlist --skip-download --quiet --ignore-errors --print id " + query);
|
||||
std::queue<std::string> ids;
|
||||
std::stringstream ss(idstring);
|
||||
|
||||
std::string _id;
|
||||
while (std::getline(ss, _id, '\n')) {
|
||||
ids.push(_id);
|
||||
}
|
||||
|
||||
if (ids.size() >= 2) {
|
||||
cluster->log(dpp::ll_info, "Playlist detected.");
|
||||
while (!ids.empty()) {
|
||||
cluster->log(dpp::ll_info, "Enqueuing " + ids.front());
|
||||
enqueue("https://youtu.be/" + ids.front());
|
||||
ids.pop();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
cluster->log(dpp::ll_info, "Thread id: " + tid.str() + ": " + ids.front() + " accepted.");
|
||||
|
||||
system(("./yt-dlp -o \"Temp/%(id)s\" --no-clean-info-json --write-info-json --default-search ytsearch \
|
||||
--flat-playlist --skip-download --quiet --ignore-errors -f ba* https://youtu.be/" + ids.front()).c_str());
|
||||
|
||||
cluster->log(dpp::ll_info, "Thread id: " + tid.str() + ": " + ids.front() + " downloaded.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace bumbleBee{
|
||||
BumbleBee::BumbleBee(nlohmann::json settings) {
|
||||
this->cluster = std::make_unique<dpp::cluster>(settings["token"]);
|
||||
this->cluster = std::make_shared<dpp::cluster>(settings["token"]);
|
||||
dbDriver = sql::mariadb::get_driver_instance();
|
||||
this->dbURL = std::make_shared<sql::SQLString>(settings["dbURL"]);
|
||||
sql::Properties pro({
|
||||
@@ -16,21 +16,16 @@ BumbleBee::BumbleBee(nlohmann::json settings) {
|
||||
cluster->on_ready([this](const dpp::ready_t &event){on_ready(event);});
|
||||
}
|
||||
|
||||
BumbleBee::~BumbleBee() {
|
||||
}
|
||||
|
||||
void BumbleBee::start() { this->cluster->start(dpp::st_wait); }
|
||||
|
||||
bool BumbleBee::addCommand(commands::ICommand cmd) {
|
||||
commands.push_back(cmd);
|
||||
return false;
|
||||
}
|
||||
|
||||
void BumbleBee::on_slashcommand(const dpp::slashcommand_t &event) {
|
||||
|
||||
for (auto command : commands)
|
||||
for (auto alias : command->nameAndAliases)
|
||||
if (event.command.get_command_name() == alias.name)
|
||||
(*command)(event);
|
||||
}
|
||||
|
||||
void BumbleBee::on_ready(const dpp::ready_t &event) {
|
||||
cluster->log(dpp::loglevel::ll_info, "Bot ready.");
|
||||
}
|
||||
|
||||
}
|
||||
10
src/main.cpp
10
src/main.cpp
@@ -1,5 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <BumbleBee.hpp>
|
||||
#include <AsyncDownloadManager.hpp>
|
||||
#include <thread>
|
||||
|
||||
int main() {
|
||||
nlohmann::json configdocument;
|
||||
@@ -8,5 +10,11 @@ int main() {
|
||||
|
||||
bumbleBee::BumbleBee bot(configdocument);
|
||||
|
||||
bot.start();
|
||||
bumbleBee::AsyncDownloadManager& manager = bumbleBee::AsyncDownloadManager::getInstance(5, bot.cluster, bot.queue);
|
||||
manager.enqueue("https://music.youtube.com/playlist?list=PL5NSTAfQ-wQBqZYMTqxADemyUW8mxJq2h&si=S1OwPaaif_litCqN");
|
||||
|
||||
std::thread th([](){sleep(100);});
|
||||
th.join();
|
||||
|
||||
//bot.start();
|
||||
}
|
||||
Reference in New Issue
Block a user