$END_LICENSE */
#include "video.h"
-#include "networkaccess.h"
-#include <QtNetwork>
-#include "videodefinition.h"
+#include "datautils.h"
+#include "http.h"
+#include "httputils.h"
#include "jsfunctions.h"
-#include "temporary.h"
+#include "playlistitemdelegate.h"
+#include "videodefinition.h"
-namespace The {
-NetworkAccess* http();
-}
+#include "ytjsvideo.h"
+#include "ytvideo.h"
+
+#include "variantpromise.h"
-namespace {
-static const QString jsNameChars = "a-zA-Z0-9\\$_";
+Video::Video()
+ : duration(0), viewCount(-1), license(LicenseYouTube), definitionCode(0), ytVideo(nullptr),
+ ytjsVideo(nullptr) {}
+
+Video::~Video() {
+ qDebug() << "Deleting" << id;
}
-Video::Video() : m_duration(0),
- m_viewCount(-1),
- definitionCode(0),
- elIndex(0),
- ageGate(false),
- m_license(LicenseYouTube),
- loadingStreamUrl(false),
- loadingThumbnail(false)
-{ }
+Video *Video::clone() {
+ Video *clone = new Video();
+ clone->title = title;
+ clone->description = description;
+ clone->channelTitle = channelTitle;
+ clone->channelId = channelId;
+ clone->webpage = webpage;
+ clone->streamUrl = streamUrl;
+ clone->thumbs = thumbs;
+ clone->thumbsNeedSorting = thumbsNeedSorting;
+ clone->duration = duration;
+ clone->formattedDuration = formattedDuration;
+ clone->published = published;
+ clone->formattedPublished = formattedPublished;
+ clone->viewCount = viewCount;
+ clone->formattedViewCount = formattedViewCount;
+ clone->id = id;
+ clone->definitionCode = definitionCode;
+ return clone;
+}
-Video* Video::clone() {
- Video* cloneVideo = new Video();
- cloneVideo->m_title = m_title;
- cloneVideo->m_description = m_description;
- cloneVideo->m_author = m_author;
- cloneVideo->m_userId = m_userId;
- cloneVideo->m_webpage = m_webpage;
- cloneVideo->m_streamUrl = m_streamUrl;
- cloneVideo->m_thumbnail = m_thumbnail;
- cloneVideo->m_thumbnailUrl = m_thumbnailUrl;
- cloneVideo->m_mediumThumbnailUrl = m_mediumThumbnailUrl;
- cloneVideo->m_duration = m_duration;
- cloneVideo->m_published = m_published;
- cloneVideo->m_viewCount = m_viewCount;
- cloneVideo->videoId = videoId;
- cloneVideo->videoToken = videoToken;
- cloneVideo->definitionCode = definitionCode;
- return cloneVideo;
+const QString &Video::getWebpage() {
+ if (webpage.isEmpty() && !id.isEmpty())
+ webpage.append("https://www.youtube.com/watch?v=").append(id);
+ return webpage;
}
-void Video::setWebpage(QUrl webpage) {
- m_webpage = webpage;
+void Video::setWebpage(const QString &value) {
+ webpage = value;
// Get Video ID
- // youtube-dl line 428
- // QRegExp re("^((?:http://)?(?:\\w+\\.)?youtube\\.com/(?:(?:v/)|(?:(?:watch(?:\\.php)?)?\\?(?:.+&)?v=)))?([0-9A-Za-z_-]+)(?(1).+)?$");
- QRegExp re("^https?://www\\.youtube\\.com/watch\\?v=([0-9A-Za-z_-]+).*");
- bool match = re.exactMatch(m_webpage.toString());
- if (!match) {
- qWarning() << QString("Cannot get video id for %1").arg(m_webpage.toString());
- // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
- // loadingStreamUrl = false;
- return;
+ if (id.isEmpty()) {
+ QRegExp re(JsFunctions::instance()->videoIdRE());
+ if (re.indexIn(webpage) == -1) {
+ qWarning() << QString("Cannot get video id for %1").arg(webpage);
+ // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
+ // loadingStreamUrl = false;
+ return;
+ }
+ id = re.cap(1);
}
- videoId = re.cap(1);
}
-void Video::loadThumbnail() {
- if (m_thumbnailUrl.isEmpty() || loadingThumbnail) return;
- loadingThumbnail = true;
- QObject *reply = The::http()->get(m_thumbnailUrl);
- connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
+void Video::setDuration(int value) {
+ duration = value;
+ formattedDuration = DataUtils::formatDuration(duration);
}
-void Video::setThumbnail(QByteArray bytes) {
- loadingThumbnail = false;
- m_thumbnail.loadFromData(bytes);
- if (m_thumbnail.width() > 160)
- m_thumbnail = m_thumbnail.scaledToWidth(160, Qt::SmoothTransformation);
- emit gotThumbnail();
+void Video::setViewCount(int value) {
+ viewCount = value;
+ formattedViewCount = DataUtils::formatCount(viewCount);
}
-void Video::loadMediumThumbnail() {
- if (m_mediumThumbnailUrl.isEmpty()) return;
- QObject *reply = The::http()->get(m_mediumThumbnailUrl);
- connect(reply, SIGNAL(data(QByteArray)), SIGNAL(gotMediumThumbnail(QByteArray)));
+void Video::setPublished(const QDateTime &value) {
+ published = value;
+ formattedPublished = DataUtils::formatDateTime(published);
}
-void Video::loadStreamUrl() {
- if (loadingStreamUrl) {
- qDebug() << "Already loading stream URL for" << this->title();
- return;
+void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) {
+ qDebug() << "Streams loaded";
+ this->streamUrl = streamUrl;
+ emit gotStreamUrl(streamUrl, audioUrl);
+ if (ytVideo) {
+ definitionCode = ytVideo->getDefinitionCode();
+ ytVideo->deleteLater();
+ ytVideo = nullptr;
}
- loadingStreamUrl = true;
- elIndex = 0;
- ageGate = false;
-
- getVideoInfo();
-}
-
-void Video::getVideoInfo() {
- static const QStringList elTypes = QStringList() << "&el=embedded" << "&el=detailpage" << "&el=vevo" << "";
-
- QUrl url;
-
- if (elIndex == elTypes.size()) {
- // qDebug() << "Trying special embedded el param";
- url = QUrl("http://www.youtube.com/get_video_info");
-
-#if QT_VERSION >= 0x050000
- {
- QUrl &u = url;
- QUrlQuery url;
-#endif
- url.addQueryItem("video_id", videoId);
- url.addQueryItem("el", "embedded");
- url.addQueryItem("gl", "US");
- url.addQueryItem("hl", "en");
- url.addQueryItem("eurl", "https://youtube.googleapis.com/v/" + videoId);
- url.addQueryItem("asv", "3");
- url.addQueryItem("sts", "1588");
-#if QT_VERSION >= 0x050000
- u.setQuery(url);
- }
-#endif
-
- } else if (elIndex > elTypes.size() - 1) {
- qWarning() << "Cannot get video info";
- loadingStreamUrl = false;
- emit errorStreamUrl("Cannot get video info");
- return;
- } else {
- // qDebug() << "Trying el param:" << elTypes.at(elIndex) << elIndex;
- url = QUrl(QString(
- "http://www.youtube.com/get_video_info?video_id=%1%2&ps=default&eurl=&gl=US&hl=en"
- ).arg(videoId, elTypes.at(elIndex)));
+ if (ytjsVideo) {
+ definitionCode = ytjsVideo->getDefinitionCode();
+ ytjsVideo->deleteLater();
+ ytjsVideo = nullptr;
}
-
- QObject *reply = The::http()->get(url);
- connect(reply, SIGNAL(data(QByteArray)), SLOT(gotVideoInfo(QByteArray)));
- connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
-
- // see you in gotVideoInfo...
}
-void Video::gotVideoInfo(QByteArray data) {
- QString videoInfo = QString::fromUtf8(data);
- // qDebug() << "videoInfo" << videoInfo;
-
- // get video token
- QRegExp re = QRegExp("^.*&token=([^&]+).*$");
- bool match = re.exactMatch(videoInfo);
- // handle regexp failure
- if (!match) {
- // qDebug() << "Cannot get token. Trying next el param";
- // Don't panic! We're gonna try another magic "el" param
- elIndex++;
- getVideoInfo();
+void Video::loadStreamUrlJS() {
+ if (ytjsVideo) {
+ qDebug() << "Already loading" << id;
return;
}
-
- QString videoToken = re.cap(1);
- while (videoToken.contains('%'))
- videoToken = QByteArray::fromPercentEncoding(videoToken.toLatin1());
- // qDebug() << "videoToken" << videoToken;
- this->videoToken = videoToken;
-
- // get fmt_url_map
- re = QRegExp("^.*&url_encoded_fmt_stream_map=([^&]+).*$");
- match = re.exactMatch(videoInfo);
- // handle regexp failure
- if (!match) {
- // qDebug() << "Cannot get urlMap. Trying next el param";
- // Don't panic! We're gonna try another magic "el" param
- elIndex++;
- getVideoInfo();
+ ytjsVideo = new YTJSVideo(id, this);
+ connect(ytjsVideo, &YTJSVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
+ connect(ytjsVideo, &YTJSVideo::errorStreamUrl, this, [this](const QString &msg) {
+ qDebug() << msg;
+ ytjsVideo->deleteLater();
+ ytjsVideo = nullptr;
+ // loadStreamUrlYT();
+ emit errorStreamUrl(msg);
+ });
+ ytjsVideo->loadStreamUrl();
+}
+
+void Video::loadStreamUrlYT() {
+ if (ytVideo) {
+ qDebug() << "Already loading" << id;
return;
}
-
- // qDebug() << "Got token and urlMap" << elIndex;
-
- QString fmtUrlMap = re.cap(1);
- fmtUrlMap = QByteArray::fromPercentEncoding(fmtUrlMap.toUtf8());
- parseFmtUrlMap(fmtUrlMap);
+ ytVideo = new YTVideo(id, this);
+ connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
+ connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) {
+ qDebug() << msg;
+ emit errorStreamUrl(msg);
+ ytVideo->deleteLater();
+ ytVideo = nullptr;
+ });
+ ytVideo->loadStreamUrl();
}
-void Video::parseFmtUrlMap(const QString &fmtUrlMap, bool fromWebPage) {
- QSettings settings;
- QString definitionName = settings.value("definition", "360p").toString();
- int definitionCode = VideoDefinition::getDefinitionCode(definitionName);
-
- // qDebug() << "fmtUrlMap" << fmtUrlMap;
- QStringList formatUrls = fmtUrlMap.split(',', QString::SkipEmptyParts);
- QHash<int, QString> urlMap;
- foreach(QString formatUrl, formatUrls) {
- // qDebug() << "formatUrl" << formatUrl;
- QStringList urlParams = formatUrl.split('&', QString::SkipEmptyParts);
- // qDebug() << "urlParams" << urlParams;
-
- int format = -1;
- QString url;
- QString sig;
- foreach(QString urlParam, urlParams) {
- // qWarning() << urlParam;
- if (urlParam.startsWith("itag=")) {
- int separator = urlParam.indexOf("=");
- format = urlParam.mid(separator + 1).toInt();
- } else if (urlParam.startsWith("url=")) {
- int separator = urlParam.indexOf("=");
- url = urlParam.mid(separator + 1);
- url = QByteArray::fromPercentEncoding(url.toUtf8());
- } else if (urlParam.startsWith("sig=")) {
- int separator = urlParam.indexOf("=");
- sig = urlParam.mid(separator + 1);
- sig = QByteArray::fromPercentEncoding(sig.toUtf8());
- } else if (urlParam.startsWith("s=")) {
- if (fromWebPage || ageGate) {
- int separator = urlParam.indexOf("=");
- sig = urlParam.mid(separator + 1);
- sig = QByteArray::fromPercentEncoding(sig.toUtf8());
- if (ageGate)
- sig = JsFunctions::instance()->decryptAgeSignature(sig);
- else {
- sig = decryptSignature(sig);
- if (sig.isEmpty())
- sig = JsFunctions::instance()->decryptSignature(sig);
- }
- } else {
- // qDebug() << "Loading webpage";
- QUrl url("http://www.youtube.com/watch");
-
-#if QT_VERSION >= 0x050000
- {
- QUrl &u = url;
- QUrlQuery url;
-#endif
- url.addQueryItem("v", videoId);
- url.addQueryItem("gl", "US");
- url.addQueryItem("hl", "en");
- url.addQueryItem("has_verified", "1");
-#if QT_VERSION >= 0x050000
- u.setQuery(url);
- }
-#endif
- QObject *reply = The::http()->get(url);
- connect(reply, SIGNAL(data(QByteArray)), SLOT(scrapeWebPage(QByteArray)));
- connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
- // see you in scrapWebPage(QByteArray)
- return;
- }
- }
- }
- if (format == -1 || url.isNull()) continue;
-
- url += "&signature=" + sig;
-
- if (!url.contains("ratebypass"))
- url += "&ratebypass=yes";
-
- // qWarning() << url;
+void Video::loadStreamUrl() {
+ loadStreamUrlJS();
+}
- if (format == definitionCode) {
- qDebug() << "Found format" << definitionCode;
- QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
- m_streamUrl = videoUrl;
- this->definitionCode = definitionCode;
- emit gotStreamUrl(videoUrl);
- loadingStreamUrl = false;
- return;
- }
+bool Video::isLoadingStreamUrl() const {
+ return ytVideo != nullptr || ytjsVideo != nullptr;
+}
- urlMap.insert(format, url);
+void Video::abortLoadStreamUrl() {
+ if (ytVideo) {
+ ytVideo->disconnect(this);
+ ytVideo->deleteLater();
+ ytVideo = nullptr;
}
-
- QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
- int currentIndex = definitionCodes.indexOf(definitionCode);
- int previousIndex = 0;
- while (currentIndex >= 0) {
- previousIndex = currentIndex - 1;
- if (previousIndex < 0) previousIndex = 0;
- int definitionCode = definitionCodes.at(previousIndex);
- if (urlMap.contains(definitionCode)) {
- qDebug() << "Found format" << definitionCode;
- QString url = urlMap.value(definitionCode);
- QUrl videoUrl = QUrl::fromEncoded(url.toUtf8(), QUrl::StrictMode);
- m_streamUrl = videoUrl;
- this->definitionCode = definitionCode;
- emit gotStreamUrl(videoUrl);
- loadingStreamUrl = false;
- return;
- }
- currentIndex--;
+ if (ytjsVideo) {
+ ytjsVideo->disconnect(this);
+ ytjsVideo->deleteLater();
+ ytjsVideo = nullptr;
}
-
- emit errorStreamUrl(tr("Cannot get video stream for %1").arg(m_webpage.toString()));
}
-void Video::foundVideoUrl(QString videoToken, int definitionCode) {
- // qDebug() << "foundVideoUrl" << videoToken << definitionCode;
-
- QUrl videoUrl = QUrl(QString(
- "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
- ).arg(videoId, videoToken, QString::number(definitionCode)));
-
- m_streamUrl = videoUrl;
- loadingStreamUrl = false;
- emit gotStreamUrl(videoUrl);
+void Video::addThumb(int width, int height, QString url) {
+ thumbs << YTThumb(width, height, url);
+ thumbsNeedSorting = true;
}
-void Video::errorVideoInfo(QNetworkReply *reply) {
- loadingStreamUrl = false;
- emit errorStreamUrl(tr("Network error: %1 for %2").arg(reply->errorString(), reply->url().toString()));
-}
-
-void Video::scrapeWebPage(QByteArray data) {
- QString html = QString::fromUtf8(data);
- // qWarning() << html;
-
- if (html.contains("player-age-gate-content\"")) {
- // qDebug() << "Found ageGate";
- ageGate = true;
- elIndex = 4;
- getVideoInfo();
- return;
+VariantPromise &Video::loadThumb(QSize size, qreal pixelRatio) {
+ if (thumbsNeedSorting) {
+ std::sort(thumbs.begin(), thumbs.end(),
+ [](auto a, auto b) { return a.getWidth() < b.getWidth(); });
+ thumbsNeedSorting = false;
}
- QRegExp re(".*\"url_encoded_fmt_stream_map\":\\s+\"([^\"]+)\".*");
- bool match = re.exactMatch(html);
- // on regexp failure, stop and report error
- if (!match) {
- qWarning() << "Error parsing video page";
- // emit errorStreamUrl("Error parsing video page");
- // loadingStreamUrl = false;
- elIndex++;
- getVideoInfo();
- return;
+ auto promise = new VariantPromise(this);
+ if (thumbs.isEmpty()) {
+ QTimer::singleShot(0, promise, [promise] { promise->reject("Empty thumbs"); });
+ return *promise;
}
- fmtUrlMap = re.cap(1);
- fmtUrlMap.replace("\\u0026", "&");
- // parseFmtUrlMap(fmtUrlMap, true);
-#ifdef APP_DASH
- QSettings settings;
- QString definitionName = settings.value("definition", "360p").toString();
- if (definitionName == QLatin1String("1080p") {
- QRegExp dashManifestRe("\"dashmpd\":\\s*\"([^\"]+)\"");
- if (dashManifestRe.indexIn(html) != -1) {
- dashManifestUrl = dashManifestRe.cap(1);
- dashManifestUrl.remove('\\');
- qDebug() << "dashManifestUrl" << dashManifestUrl;
- }
- }
-#endif
-
- QRegExp jsPlayerRe("\"assets\":.+\"js\":\\s*\"([^\"]+)\"");
- if (jsPlayerRe.indexIn(html) != -1) {
- QString jsPlayerUrl = jsPlayerRe.cap(1);
- jsPlayerUrl.remove('\\');
- jsPlayerUrl = "http:" + jsPlayerUrl;
- // qDebug() << "jsPlayerUrl" << jsPlayerUrl;
- /*
- QRegExp jsPlayerIdRe("-(.+)\\.js");
- jsPlayerIdRe.indexIn(jsPlayerUrl);
- QString jsPlayerId = jsPlayerRe.cap(1);
- */
- QObject *reply = The::http()->get(jsPlayerUrl);
- connect(reply, SIGNAL(data(QByteArray)), SLOT(parseJsPlayer(QByteArray)));
- connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
- }
-}
+ auto reallyLoad = [this, promise, size, pixelRatio](auto &&self,
+ YTThumb *previous = nullptr) -> void {
+ YTThumb *selected = nullptr;
-void Video::gotHeadHeaders(QNetworkReply* reply) {
- int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
- // qDebug() << "gotHeaders" << statusCode;
- if (statusCode == 200) {
- foundVideoUrl(videoToken, definitionCode);
- } else {
-
- // try next (lower quality) definition
- /*
- QStringList definitionNames = VideoDefinition::getDefinitionNames();
- int currentIndex = definitionNames.indexOf(currentDefinition);
- int previousIndex = 0;
- if (currentIndex > 0) {
- previousIndex = currentIndex - 1;
- }
- if (previousIndex > 0) {
- QString nextDefinitionName = definitionNames.at(previousIndex);
- findVideoUrl(nextDefinitionName);
- } else {
- foundVideoUrl(videoToken, 18);
- }*/
-
-
- QList<int> definitionCodes = VideoDefinition::getDefinitionCodes();
- int currentIndex = definitionCodes.indexOf(definitionCode);
- int previousIndex = 0;
- if (currentIndex > 0) {
- previousIndex = currentIndex - 1;
- int definitionCode = definitionCodes.at(previousIndex);
- if (definitionCode == 18) {
- // This is assumed always available
- foundVideoUrl(videoToken, 18);
- } else {
- findVideoUrl(definitionCode);
+ static bool fallback = false;
+ if (fallback) {
+ qDebug() << "Doing fallback loop";
+ bool skip = previous != nullptr;
+ for (int i = thumbs.size() - 1; i >= 0; --i) {
+ auto &thumb = thumbs.at(i);
+ if (!skip) {
+ selected = (YTThumb *)&thumb;
+ qDebug() << "selected" << selected->getUrl();
+ break;
+ }
+ if (&thumb == previous) skip = false;
}
-
} else {
- foundVideoUrl(videoToken, 18);
+ bool skip = previous != nullptr;
+ for (auto &thumb : qAsConst(thumbs)) {
+ if (!skip && thumb.getWidth() * pixelRatio >= size.width() &&
+ thumb.getHeight() * pixelRatio >= size.height()) {
+ selected = (YTThumb *)&thumb;
+ qDebug() << "selected" << selected->getUrl();
+ break;
+ }
+ if (&thumb == previous) skip = false;
+ }
}
-
- }
-}
-
-void Video::parseJsPlayer(QByteArray bytes) {
- QString js = QString::fromUtf8(bytes);
- // qWarning() << "jsPlayer" << js;
- QRegExp funcNameRe("signature=([" + jsNameChars + "]+)");
- if (funcNameRe.indexIn(js) == -1) {
- qWarning() << "Cannot capture signature function name";
- } else {
- sigFuncName = funcNameRe.cap(1);
- captureFunction(sigFuncName, js);
- // qWarning() << sigFunctions;
- }
-
-#ifdef APP_DASH
- if (!dashManifestUrl.isEmpty()) {
- QRegExp sigRe("/s/([\\w\\.]+)");
- if (sigRe.indexIn(dashManifestUrl) != -1) {
- qDebug() << "Decrypting signature for dash manifest";
- QString sig = sigRe.cap(1);
- sig = decryptSignature(sig);
- dashManifestUrl.replace(sigRe, "/signature/" + sig);
- qDebug() << dashManifestUrl;
-
- m_streamUrl = dashManifestUrl;
- this->definitionCode = 37;
- emit gotStreamUrl(m_streamUrl);
- loadingStreamUrl = false;
-
- /*
- QObject *reply = The::http()->get(QUrl::fromEncoded(dashManifestUrl.toUtf8()));
- connect(reply, SIGNAL(data(QByteArray)), SLOT(parseDashManifest(QByteArray)));
- connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
- */
-
+ if (!selected && !fallback) {
+ qDebug() << "Falling back";
+ fallback = true;
+ self(self);
return;
}
- }
-#endif
-
- parseFmtUrlMap(fmtUrlMap, true);
-}
-
-void Video::parseDashManifest(QByteArray bytes) {
- QFile file(Temporary::filename());
- if (!file.open(QIODevice::WriteOnly))
- qWarning() << file.errorString() << file.fileName();
- QDataStream stream(&file);
- stream.writeRawData(bytes.constData(), bytes.size());
-
- m_streamUrl = "file://" + file.fileName();
- this->definitionCode = 37;
- emit gotStreamUrl(m_streamUrl);
- loadingStreamUrl = false;
-}
-
-void Video::captureFunction(const QString &name, const QString &js) {
- QRegExp funcRe("function\\s+" + QRegExp::escape(name) + "\\s*\\([" + jsNameChars + ",\\s]*\\)\\s*\\{[^\\}]+\\}");
- if (funcRe.indexIn(js) == -1) {
- qWarning() << "Cannot capture function" << name;
- return;
- }
- QString func = funcRe.cap(0);
- sigFunctions.insert(name, func);
-
- // capture inner functions
- QRegExp invokedFuncRe("[\\s=;\\(]([" + jsNameChars + "]+)\\s*\\([" + jsNameChars + ",\\s]+\\)");
- int pos = name.length() + 9;
- while ((pos = invokedFuncRe.indexIn(func, pos)) != -1) {
- QString funcName = invokedFuncRe.cap(1);
- if (!sigFunctions.contains(funcName))
- captureFunction(funcName, js);
- pos += invokedFuncRe.matchedLength();
- }
-
- // capture referenced objects
- QRegExp objRe("[\\s=;\\(]([" + jsNameChars + "]+)\\.[" + jsNameChars + "]+");
- pos = name.length() + 9;
- while ((pos = objRe.indexIn(func, pos)) != -1) {
- QString objName = objRe.cap(1);
- if (!sigObjects.contains(objName))
- captureObject(objName, js);
- pos += objRe.matchedLength();
- }
-}
-
-void Video::captureObject(const QString &name, const QString &js) {
- QRegExp re("var\\s+" + QRegExp::escape(name) + "\\s*=\\s*\\{.+\\}\\s*;");
- re.setMinimal(true);
- if (re.indexIn(js) == -1) {
- qWarning() << "Cannot capture object" << name;
- return;
- }
- QString obj = re.cap(0);
- sigObjects.insert(name, obj);
-}
-
-QString Video::decryptSignature(const QString &s) {
- if (sigFuncName.isEmpty()) return QString();
- QScriptEngine engine;
- foreach (QString f, sigObjects.values()) {
- QScriptValue value = engine.evaluate(f);
- if (value.isError())
- qWarning() << "Error in" << f << value.toString();
- }
- foreach (QString f, sigFunctions.values()) {
- QScriptValue value = engine.evaluate(f);
- if (value.isError())
- qWarning() << "Error in" << f << value.toString();
- }
- QString js = sigFuncName + "('" + s + "');";
- QScriptValue value = engine.evaluate(js);
- if (value.isUndefined()) {
- qWarning() << "Undefined result for" << js;
- return QString();
- }
- if (value.isError()) {
- qWarning() << "Error in" << js << value.toString();
- return QString();
- }
- return value.toString();
-}
-
-void Video::findVideoUrl(int definitionCode) {
- this->definitionCode = definitionCode;
-
- QUrl videoUrl = QUrl(QString(
- "http://www.youtube.com/get_video?video_id=%1&t=%2&eurl=&el=&ps=&asv=&fmt=%3"
- ).arg(videoId, videoToken, QString::number(definitionCode)));
-
- QObject *reply = The::http()->head(videoUrl);
- connect(reply, SIGNAL(finished(QNetworkReply*)), SLOT(gotHeadHeaders(QNetworkReply*)));
- // connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorVideoInfo(QNetworkReply*)));
-
- // see you in gotHeadHeaders()
-}
-
-QString Video::formattedDuration() const {
- QString format = m_duration > 3600 ? "h:mm:ss" : "m:ss";
- return QTime().addSecs(m_duration).toString(format);
+ if (selected) {
+ qDebug() << "Loading" << selected->getUrl();
+ selected->load(promise)
+ .then([promise](auto variant) { promise->resolve(variant); })
+ .onFailed([self, selected] { self(self, selected); });
+ } else
+ promise->reject("No thumb");
+ };
+ reallyLoad(reallyLoad);
+
+ return *promise;
}