fix(youtube-dl): Typo & Compatibility with older compilers

This commit is contained in:
Curve 2021-08-11 19:46:08 +02:00
parent 17dd691791
commit cdd8d9eabf
No known key found for this signature in database
GPG Key ID: 460F6C466BD35813
2 changed files with 25 additions and 23 deletions

View File

@ -1,4 +1,5 @@
#include "youtube-dl.hpp"
#include "process.hpp"
#include <core/global/globals.hpp>
#include <fancy.hpp>
#include <helper/misc/misc.hpp>
@ -24,17 +25,17 @@ namespace Soundux::Objects
Fancy::fancy.logTime().warning() << "youtube-dl or ffmpeg is not available!" << std::endl;
}
}
std::optional<nlohmann::json> YoutubeDl::getInfo(const std::string &url) const
nlohmann::json YoutubeDl::getInfo(const std::string &url) const
{
if (!isAvailable)
{
return std::nullopt;
return nullptr;
}
if (!std::regex_match(url, urlRegex))
{
Fancy::fancy.logTime().warning() << "Bad url " >> url << std::endl;
return std::nullopt;
return nullptr;
}
auto [result, success] = Helpers::getResultCompact("youtube-dl -i -j \"" + url + "\"");
@ -45,7 +46,7 @@ namespace Soundux::Objects
{
Fancy::fancy.logTime().warning() << "Failed to parse youtube-dl information" << std::endl;
Globals::gGui->onError(Enums::ErrorCode::YtdlInvalidJson);
return std::nullopt;
return nullptr;
}
nlohmann::json j;
@ -67,7 +68,7 @@ namespace Soundux::Objects
Fancy::fancy.logTime().warning() << "Failed to get info from youtube-dl" << std::endl;
Globals::gGui->onError(Enums::ErrorCode::YtdlInformationUnknown);
return std::nullopt;
return nullptr;
}
bool YoutubeDl::download(const std::string &url)
{
@ -99,21 +100,22 @@ namespace Soundux::Objects
currentDownload.reset();
}
currentDownload.emplace("youtube-dl --extract-audio --audio-format mp3 --no-mtime \"" + url + "\" -o \"" +
currentTab->path + "/%(title)s.%(ext)s" + "\"",
"", [](const char *rawData, std::size_t dataLen) {
std::string data(rawData, dataLen);
static const std::regex progressRegex(R"(([0-9.,]+)%.*(ETA (.+)))");
currentDownload = std::make_unique<TinyProcessLib::Process>(
"youtube-dl --extract-audio --audio-format mp3 --no-mtime \"" + url + "\" -o \"" + currentTab->path +
"/%(title)s.%(ext)s" + "\"",
"", [](const char *rawData, std::size_t dataLen) {
std::string data(rawData, dataLen);
static const std::regex progressRegex(R"(([0-9.,]+)%.*(ETA (.+)))");
std::smatch match;
if (std::regex_search(data, match, progressRegex))
{
if (match[1].matched && match[3].matched)
{
Globals::gGui->onDownloadProgressed(std::stof(match[1]), match[3]);
}
}
});
std::smatch match;
if (std::regex_search(data, match, progressRegex))
{
if (match[1].matched && match[3].matched)
{
Globals::gGui->onDownloadProgressed(std::stof(match[1]), match[3]);
}
}
});
Fancy::fancy.logTime().success() << "Started download of " >> url << std::endl;
auto rtn = currentDownload->get_exit_status() == 0;

View File

@ -2,10 +2,10 @@
#include <json.hpp>
#include <optional>
#pragma push_macro("UNICOCDE")
#pragma push_macro("UNICODE")
#undef UNICODE
#include <process.hpp>
#pragma pop_macro("UNICOCDE")
#pragma pop_macro("UNICODE")
#include <regex>
#include <string>
@ -18,14 +18,14 @@ namespace Soundux
{
bool isAvailable = false;
static const std::regex urlRegex;
std::optional<TinyProcessLib::Process> currentDownload;
std::unique_ptr<TinyProcessLib::Process> currentDownload;
public:
void setup();
void killDownload();
bool available() const;
bool download(const std::string &);
std::optional<nlohmann::json> getInfo(const std::string &) const;
nlohmann::json getInfo(const std::string &) const;
};
} // namespace Objects
} // namespace Soundux