From bf4f953cf6f083d6bc876543f7ac8e77a123abbf Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <vital.had@gmail.com>
Date: Tue, 19 Nov 2024 17:04:56 +0800
Subject: [PATCH 08/11] Switch to yt-dlp

---
 src/qmplay2/YouTubeDL.cpp | 40 ++++++++++-----------------------------
 1 file changed, 10 insertions(+), 30 deletions(-)

diff --git src/qmplay2/YouTubeDL.cpp src/qmplay2/YouTubeDL.cpp
index de7b96be..98071fea 100644
--- src/qmplay2/YouTubeDL.cpp
+++ src/qmplay2/YouTubeDL.cpp
@@ -22,10 +22,9 @@
 #include <QMPlay2Core.hpp>
 #include <Version.hpp>
 #include <Json11.hpp>
-#ifdef Q_OS_WIN
-	#include <Functions.hpp>
-#endif
+#include <Functions.hpp>
 
+#include <QRegExp>
 #include <QReadWriteLock>
 #include <QFile>
 
@@ -50,9 +49,9 @@ static void exportCookiesFromJSON(const QString &jsonData, const QString &url)
 
 QString YouTubeDL::getFilePath()
 {
-	return QMPlay2Core.getSettingsDir() + "youtube-dl"
+	return QMPlay2Core.getSettingsDir() + "yt-dlp"
 #ifdef Q_OS_WIN
-	".exe"
+	"_x86.exe"
 #endif
 	;
 }
@@ -286,30 +285,11 @@ QStringList YouTubeDL::exec(const QString &url, const QStringList &args, QString
 					updateOutput += m_process.readAllStandardOutput() + m_process.readAllStandardError();
 					if (updateOutput.contains("ERROR:") || updateOutput.contains("package manager"))
 						error += "\n" + updateOutput;
-					else if (m_process.exitCode() == 0 && !updateOutput.contains("up-to-date"))
+					else if (m_process.exitCode() == 0 && !updateOutput.contains(QRegExp(R"(up\Wto\Wdate)")))
 					{
-#ifdef Q_OS_WIN
-						const QString updatedFile = ytDlPath + ".new";
-						QFile::remove(Functions::filePath(ytDlPath) + "youtube-dl-updater.bat");
-						if (QFile::exists(updatedFile))
-						{
-							Functions::s_wait(0.2); //Wait 200 ms to be sure that file is closed
-							QFile::remove(ytDlPath);
-							if (QFile::rename(updatedFile, ytDlPath))
-#endif
-							{
-								QMPlay2Core.setWorking(false);
-								emit QMPlay2Core.sendMessage(tr("\"youtube-dl\" has been successfully updated!"), g_name);
-								g_lock.unlock(); // Unlock for write
-								return exec(url, args, silentErr, false);
-							}
-#ifdef Q_OS_WIN
-						}
-						else
-						{
-							error += "\nUpdated youtube-dl file: \"" + updatedFile + "\" not found!";
-						}
-#endif
+						QMPlay2Core.setWorking(false);
+						emit QMPlay2Core.sendMessage(tr("\"youtube-dl\" has been successfully updated!"), g_name);
+						return {};
 					}
 				}
 				else if (updating && m_aborted)
@@ -339,9 +319,9 @@ QStringList YouTubeDL::exec(const QString &url, const QStringList &args, QString
 	}
 	else if (canUpdate && !m_aborted && m_process.error() == QProcess::FailedToStart)
 	{
-		const QString downloadUrl = "https://yt-dl.org/downloads/latest/youtube-dl"
+		const QString downloadUrl = "https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp"
 #ifdef Q_OS_WIN
-		".exe"
+		"_x86.exe"
 #endif
 		;
 
