Skip to content

armgit / Spun

Public Imported from GitHub
0

A CD-shaped music player for Linux, with local playback and Cider integration.

Set YouTube support up from the app instead of a terminal

yappologisticd4448ecbbb1c
diff --git a/README.md b/README.md
index b07837c..729cf5f 100644
--- a/README.md
+++ b/README.md
@@ -158,18 +158,21 @@ history can be cleared from its menu. All four 2D/3D players, Mini, media keys,
seeking, lyrics (when available) and the TX-6 audio route use the same native
transport. Starting a song does not change the Local or Cider queue.
-YouTube support is optional. Install Python 3 with venv/pip support and Node.js,
-then run this from the source checkout:
+YouTube support is optional and installs itself from the app. Open the YouTube
+panel and choose **Set up YouTube support**; it reports progress and says what
+is missing if Python 3 (with venv/pip) or Node.js is not present. The same setup
+can still be run from a terminal:
```sh
./scripts/setup-youtube.sh
```
-The helper runtime is isolated in `runtime/youtube`; it adds no browser engine
-and starts no background server. Local and Cider playback do not need it. After
-setup, use **Retry** if the YouTube panel was already open. The Nix package does
-not currently bundle the optional Python runtime; use the environment overrides
-below with a separately managed runtime.
+A Python interpreter that already carries `ytmusicapi` and `yt-dlp` is used as
+is, with no download. Otherwise the helper runtime is isolated in
+`runtime/youtube`; it adds no browser engine and starts no background server.
+Local and Cider playback do not need it. The Nix package does not currently
+bundle the optional Python runtime; use the environment overrides below with a
+separately managed runtime.
Audio is temporarily buffered before playback, with a 64 MiB limit per song and
at most one next-song buffer. Buffers are removed when replaced or on normal exit;
diff --git a/qml/YoutubePanel.qml b/qml/YoutubePanel.qml
index 19eb5c2..7366517 100644
--- a/qml/YoutubePanel.qml
+++ b/qml/YoutubePanel.qml
@@ -8,6 +8,8 @@ Rectangle {
color: app.surface
radius: SpunStyle.panelRadius
readonly property bool playlistPage: youtube.page.startsWith("playlist:")
+ // Offer setup only while the runtime is genuinely missing and installable.
+ readonly property bool setupOffered: !youtube.ready && youtube.installable && (youtube.installing || youtube.error.length>0)
property var selected: ({})
property int selectedIndex: -1
property string renameId: ""
@@ -133,7 +135,10 @@ Rectangle {
x: 24; y: 230; width: parent.width-48; spacing: 16
visible: !youtube.busy && (youtube.error.length>0 || youtube.items.length===0)
SpunText {width:parent.width;text:youtube.error || (youtube.page==="search"?"Find a song, album, artist or playlist. No account needed.":"Nothing here yet.");wrapMode:Text.WordWrap;color:youtube.error.length?theme.colors.error:panel.app.mutedInk;font.pixelSize:SpunStyle.body}
- SpunButton {objectName:"youtubeRetry";text:"Retry";visible:youtube.error.length>0;onClicked:{if(!youtube.ready)youtube.check();else if(search.text.trim().length)youtube.search(search.text,panel.searchFilter);else youtube.show("home")}}
+ // Setup runs from here so a first run never needs a terminal.
+ SpunText {objectName:"youtubeSetupNote";width:parent.width;visible:panel.setupOffered;text:youtube.installing?(youtube.installStatus||"Setting up YouTube support..."):"This downloads a small Python helper. No account is needed.";wrapMode:Text.WordWrap;color:panel.app.mutedInk;font.pixelSize:SpunStyle.caption}
+ SpunButton {objectName:"youtubeSetup";text:youtube.installing?"Setting up...":"Set up YouTube support";tonal:true;visible:panel.setupOffered;enabled:!youtube.installing;onClicked:youtube.install()}
+ SpunButton {objectName:"youtubeRetry";text:"Retry";visible:youtube.error.length>0 && !youtube.installing;onClicked:{if(!youtube.ready)youtube.check();else if(search.text.trim().length)youtube.search(search.text,panel.searchFilter);else youtube.show("home")}}
SpunButton {objectName:"youtubeDiscover";text:"Discover music";visible:!youtube.error.length && youtube.page==="search";onClicked:youtube.show("home")}
}
diff --git a/src/youtube.cpp b/src/youtube.cpp
index ec895b8..66e863b 100644
--- a/src/youtube.cpp
+++ b/src/youtube.cpp
@@ -155,6 +155,12 @@ Youtube::~Youtube() {
persist();
for (const auto &key : m_jobs.keys())
cancel(key);
+ // A half-finished setup must not outlive the app.
+ if (m_install) {
+ m_install->disconnect(this);
+ m_install->kill();
+ m_install->waitForFinished(2000);
+ }
m_player.stop();
}
void Youtube::fail(const QString &message) {
@@ -212,8 +218,7 @@ void Youtube::request(const QString &channel, const QVariantMap &args,
return;
m_jobs.remove(channel);
done({{"ok", false},
- {"error", "YouTube support is not installed. Run "
- "scripts/setup-youtube.sh in the Spun folder."}});
+ {"error", "YouTube support is not installed yet."}});
p->deleteLater();
});
connect(p, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this,
@@ -231,9 +236,7 @@ void Youtube::request(const QString &channel, const QVariantMap &args,
p->deleteLater();
done(data);
});
- QString python = qEnvironmentVariable("SPUN_YOUTUBE_PYTHON");
- if (python.isEmpty())
- python = QStringLiteral(SPUN_SOURCE_DIR) + "/runtime/youtube/bin/python";
+ const QString python = helperPython();
QString helper = qEnvironmentVariable("SPUN_YOUTUBE_HELPER",
QStringLiteral(SPUN_SOURCE_DIR) +
"/helper/youtube.py");
@@ -259,6 +262,93 @@ void Youtube::setEnabled(bool enabled) {
}
emit changed();
}
+QString Youtube::helperPython() {
+ const auto configured = qEnvironmentVariable("SPUN_YOUTUBE_PYTHON");
+ if (!configured.isEmpty())
+ return configured;
+ const auto bundled =
+ QStringLiteral(SPUN_SOURCE_DIR) + "/runtime/youtube/bin/python";
+ if (QFileInfo::exists(bundled))
+ return bundled;
+ // A system interpreter may already carry the packages; check() decides.
+ const auto system = QStandardPaths::findExecutable("python3");
+ return system.isEmpty() ? bundled : system;
+}
+QString Youtube::setupScript() {
+ return QStringLiteral(SPUN_SOURCE_DIR) + "/scripts/setup-youtube.sh";
+}
+bool Youtube::installable() const {
+ return qEnvironmentVariable("SPUN_YOUTUBE_PYTHON").isEmpty() &&
+ QFileInfo::exists(setupScript());
+}
+void Youtube::finishInstall(bool ok, const QString &message) {
+ m_installing = false;
+ m_installStatus.clear();
+ if (ok) {
+ m_checked = false;
+ check();
+ return;
+ }
+ m_error = message;
+ emit changed();
+ emit feedback(message, true);
+}
+// Setup is offered in the panel so a first run never needs a terminal.
+void Youtube::install() {
+ if (m_installing || m_install)
+ return;
+ if (!installable()) {
+ finishInstall(false, "YouTube support cannot be set up from this build.");
+ return;
+ }
+ if (QStandardPaths::findExecutable("node").isEmpty()) {
+ finishInstall(false, "Install Node.js, then set up YouTube support again.");
+ return;
+ }
+ if (QStandardPaths::findExecutable("python3").isEmpty()) {
+ finishInstall(false, "Install Python 3, then set up YouTube support again.");
+ return;
+ }
+ m_installing = true;
+ m_error.clear();
+ m_installStatus = "Preparing YouTube support...";
+ emit changed();
+ auto *p = new QProcess(this);
+ m_install = p;
+ p->setProcessChannelMode(QProcess::MergedChannels);
+ auto tail = std::make_shared<QString>();
+ connect(p, &QProcess::readyRead, this, [this, p, tail] {
+ const auto chunk = QString::fromUtf8(p->readAll());
+ for (const auto &line : chunk.split('\n', Qt::SkipEmptyParts))
+ *tail = line.trimmed();
+ if (tail->size() > 200)
+ *tail = tail->left(200);
+ // Surface progress without echoing pip's full transcript into the panel.
+ m_installStatus = tail->isEmpty() ? m_installStatus : *tail;
+ emit changed();
+ });
+ connect(p, &QProcess::errorOccurred, this, [this, p](QProcess::ProcessError) {
+ if (m_install != p)
+ return;
+ m_install.clear();
+ p->deleteLater();
+ finishInstall(false, "Could not start the YouTube setup script.");
+ });
+ connect(p, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this,
+ [this, p, tail](int code, QProcess::ExitStatus status) {
+ if (m_install != p)
+ return;
+ m_install.clear();
+ p->deleteLater();
+ const bool ok = status == QProcess::NormalExit && code == 0;
+ finishInstall(ok, ok ? QString()
+ : tail->isEmpty()
+ ? "YouTube setup failed. Check your "
+ "connection and try again."
+ : "YouTube setup failed: " + *tail);
+ });
+ p->start("/usr/bin/env", {"bash", setupScript()});
+}
void Youtube::check() {
m_checked = true;
m_busy = true;
@@ -266,10 +356,12 @@ void Youtube::check() {
request("browse", {{"op", "check"}}, [this](const auto &r) {
m_busy = false;
m_ready = r.value("ok").toBool();
- m_error = m_ready
- ? QString()
- : "YouTube support needs setup. Run scripts/setup-youtube.sh "
- "in the Spun folder, then Retry.";
+ m_error = m_ready ? QString()
+ : installable()
+ ? "YouTube support is not installed yet."
+ : "YouTube support is not installed. Point "
+ "SPUN_YOUTUBE_PYTHON at a runtime that has "
+ "ytmusicapi and yt-dlp.";
emit changed();
});
}
diff --git a/src/youtube.h b/src/youtube.h
index 367ccf6..454c6ef 100644
--- a/src/youtube.h
+++ b/src/youtube.h
@@ -17,6 +17,9 @@ class Youtube : public QObject {
Q_PROPERTY(bool ready READ ready NOTIFY changed)
Q_PROPERTY(bool busy READ busy NOTIFY changed)
Q_PROPERTY(QString error READ error NOTIFY changed)
+ Q_PROPERTY(bool installing READ installing NOTIFY changed)
+ Q_PROPERTY(bool installable READ installable NOTIFY changed)
+ Q_PROPERTY(QString installStatus READ installStatus NOTIFY changed)
Q_PROPERTY(QString heading READ heading NOTIFY changed)
Q_PROPERTY(QString page READ page NOTIFY changed)
Q_PROPERTY(QVariantList items READ items NOTIFY changed)
@@ -38,6 +41,10 @@ public:
bool ready() const { return m_ready; }
bool busy() const { return m_busy; }
QString error() const { return m_error; }
+ bool installing() const { return m_installing; }
+ // Setup is only offered when this build can run it from a source checkout.
+ bool installable() const;
+ QString installStatus() const { return m_installStatus; }
QString heading() const { return m_heading; }
QString page() const { return m_page; }
QVariantList items() const { return m_items; }
@@ -45,6 +52,7 @@ public:
QVariantMap current() const;
bool canBack() const { return !m_back.isEmpty(); }
Q_INVOKABLE void check();
+ Q_INVOKABLE void install();
Q_INVOKABLE void search(const QString &query,
const QString &filter = "songs");
Q_INVOKABLE void show(const QString &page);
@@ -96,6 +104,11 @@ private:
QList<Track> tracks(const QVariantList &items);
QVariantList queueItems() const;
void fail(const QString &message);
+ // The helper runs on the bundled runtime when present, otherwise on a
+ // system interpreter that already carries the packages.
+ static QString helperPython();
+ static QString setupScript();
+ void finishInstall(bool ok, const QString &message);
Player m_player;
QString m_directory, m_path, m_heading = "YouTube Music", m_page = "search",
m_error;
@@ -104,6 +117,9 @@ private:
QVariantList m_items, m_favorites, m_history, m_playlists, m_back;
QHash<QString, QVariantMap> m_catalog;
QHash<QString, QPointer<QProcess>> m_jobs;
+ QPointer<QProcess> m_install;
+ bool m_installing = false;
+ QString m_installStatus;
QNetworkAccessManager m_network;
QPointer<QNetworkReply> m_artReply;
std::shared_ptr<QTemporaryDir> m_audio, m_prepared;
diff --git a/src/youtubetest.cpp b/src/youtubetest.cpp
index f09adc6..3cd50d5 100644
--- a/src/youtubetest.cpp
+++ b/src/youtubetest.cpp
@@ -40,6 +40,30 @@ int exerciseYoutube(Player &local, Youtube &yt, QQuickWindow *window,
std::cout << (ok ? "PASS " : "FAIL ") << label << std::endl;
return ok;
};
+ // A first run must offer setup in the panel rather than name a shell script.
+ {
+ qunsetenv("SPUN_YOUTUBE_PYTHON");
+ Youtube fresh(temp + "/youtube-setup");
+ check(fresh.installable(),
+ "a source checkout can set YouTube support up in the app");
+ fresh.check();
+ check(until([&] { return !fresh.busy(); }, 30000),
+ "a missing runtime resolves instead of hanging");
+ if (fresh.ready())
+ // A system interpreter already carrying the packages needs no setup.
+ check(fresh.error().isEmpty(), "an existing system runtime is used as is");
+ else
+ check(!fresh.error().isEmpty() &&
+ !fresh.error().contains("setup-youtube.sh"),
+ "setup guidance does not send the user to a terminal");
+ // Without Node.js the script cannot succeed, so say so before running it.
+ const auto path = qgetenv("PATH");
+ qputenv("PATH", "/nonexistent");
+ fresh.install();
+ check(!fresh.installing() && fresh.error().contains("Node.js"),
+ "missing Node.js is reported instead of a failed script");
+ qputenv("PATH", path);
+ }
qputenv("SPUN_YOUTUBE_PYTHON", "/usr/bin/python3");
qputenv("SPUN_YOUTUBE_HELPER", SPUN_SOURCE_DIR "/tests/youtube_fixture.py");
qputenv("SPUN_YOUTUBE_FIXTURE_AUDIO", SPUN_DEMO_FILE);