Compare commits

...
1 Commits
Author SHA1 Message Date
Jonah Brüchert 9440629b68 WIP: Add download feature 2022-11-25 01:17:08 +01:00
5 changed files with 65 additions and 3 deletions

No files matched your search

+19
View File
@@ -10,6 +10,7 @@
#include <QJsonArray>
#include <QFutureInterface>
#include <QCoreApplication>
#include <QTimer>
#include <pybind11/embed.h>
@@ -55,6 +56,17 @@ AsyncYTMusic::AsyncYTMusic(QObject *parent)
connect(this, &AsyncYTMusic::errorOccurred, this, [](const QString &err) {
std::cerr << qPrintable(err);
});
QTimer::singleShot(0, [this]() {
qDebug() << "Downloading album";
downloadPlaylist("https://music.youtube.com/watch?v=8rIjsa85UVk&list=RDAMVM8rIjsa85UVk", [](YtDlDownloadStatus status) {
if (!status.finished && status.current_track) {
std::cout << *status.current_track << std::endl;
} else if (status.finished) {
std::cout << "Download finished" << std::endl;
}
});
});
}
//
@@ -148,6 +160,13 @@ QFuture<Lyrics> AsyncYTMusic::fetchLyrics(const QString &browseId)
});
}
QFuture<void> AsyncYTMusic::downloadPlaylist(const std::string &url, std::function<void (YtDlDownloadStatus)> progressHook)
{
return invokeAndCatchOnThread([=, this]() {
m_ytm->download_playlist(url, progressHook);
});
}
YTMusicThread &YTMusicThread::instance()
{
static YTMusicThread thread;
+8 -2
View File
@@ -123,6 +123,8 @@ public:
QFuture<Lyrics> fetchLyrics(const QString &browseId);
QFuture<void> downloadPlaylist(const std::string &url, std::function<void(YtDlDownloadStatus)> progressHook);
Q_SIGNAL void errorOccurred(const QString &error);
protected:
@@ -136,8 +138,12 @@ private:
auto interface = std::make_shared<QFutureInterface<ReturnType>>();
QMetaObject::invokeMethod(this, [=, this]() {
try {
ReturnType val = fun();
interface->reportResult(val);
if constexpr (!std::is_same_v<std::invoke_result_t<Func>, void>) {
ReturnType val = fun();
interface->reportResult(val);
} else {
fun();
}
interface->reportFinished();
} catch (const std::exception &err) {
interface->reportFinished();
View File
Whitespace-only changes.
+27 -1
View File
@@ -497,7 +497,6 @@ watch::Playlist YTMusic::get_watch_playlist(const std::optional<std::string> &vi
};
}
Lyrics YTMusic::get_lyrics(const std::string &browse_id) const
{
auto lyrics = d->get_ytmusic().attr("get_lyrics")(browse_id);
@@ -507,3 +506,30 @@ Lyrics YTMusic::get_lyrics(const std::string &browse_id) const
lyrics["lyrics"].cast<std::string>()
};
}
void YTMusic::download_playlist(const std::string &playlist_url, std::function<void (YtDlDownloadStatus)> progress_hook) const
{
py::module::import("ytmx.ytmx").attr("YTMX")(std::vector {playlist_url}, py::cpp_function([=](const py::dict &ytdl_status) {
if (ytdl_status["status"].cast<std::string>() == "finished") {
int current_track_index = ytdl_status["info_dict"]["playlist_index"].cast<int>();
int playlist_length = ytdl_status["info_dict"]["playlist_count"].cast<int>();
auto current_track = ytdl_status["info_dict"]["title"].cast<std::string>();
progress_hook(YtDlDownloadStatus {
.finished = true,
.playlist_length = playlist_length,
.current_track_index = current_track_index,
});
} else if (ytdl_status["status"].cast<std::string>() == "downloading") {
int current_track_index = ytdl_status["info_dict"]["playlist_index"].cast<int>();
int playlist_length = ytdl_status["info_dict"]["playlist_count"].cast<int>();
auto current_track = ytdl_status["info_dict"]["title"].cast<std::string>();
progress_hook(YtDlDownloadStatus {
.playlist_length = playlist_length,
.current_track_index = current_track_index,
.current_track = current_track
});
}
}));
}
+11
View File
@@ -11,6 +11,7 @@
#include <variant>
#include <vector>
#include <memory>
#include <functional>
struct YTMusicPrivate;
@@ -277,6 +278,13 @@ struct Lyrics {
std::string lyrics;
};
struct YtDlDownloadStatus {
bool finished = false;
int playlist_length;
std::optional<int> current_track_index;
std::optional<std::string> current_track;
};
class YTMusic
{
public:
@@ -319,6 +327,9 @@ public:
Lyrics get_lyrics(const std::string &browse_id) const;
void download_playlist(const std::string &playlist_url,
std::function<void(YtDlDownloadStatus)> progress_hook) const;
// TODO wrap more methods
private: