Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/modules/Extensions/YouTube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#include <QMenu>
#include <QUrl>

Comment thread
RJVB marked this conversation as resolved.
Outdated
#include <QDateTime>
#include <QDeadlineTimer>
#include <unistd.h>
Comment thread
RJVB marked this conversation as resolved.
Outdated

Q_LOGGING_CATEGORY(youtube, "Extensions/YouTube")

#define YOUTUBE_URL "https://www.youtube.com"
Expand Down Expand Up @@ -1271,6 +1275,8 @@ QStringList YouTube::getYouTubeVideo(const QString &param, const QString &url, I

QHash<int, QPair<QStringList, QStringList>> itagsData;

double latest_available_at = 0.0;

for (auto &&formatVal : formats)
{
const auto format = formatVal.toObject();
Expand Down Expand Up @@ -1298,6 +1304,16 @@ QStringList YouTube::getYouTubeVideo(const QString &param, const QString &url, I
urlLanguages[url] = format[QStringLiteral("language")].toString();
urlNotes[url] = note;
}
if (format.contains("available_at"))
{
auto available_at = format["available_at"].toDouble() * 1000.0;
Comment thread
RJVB marked this conversation as resolved.
Outdated
qCDebug(youtube) << "url for format" << format["format_id"].toString() << "will be available at"
Comment thread
RJVB marked this conversation as resolved.
Outdated
<< QDateTime::fromMSecsSinceEpoch(available_at) << "so in" << (available_at - QDateTime::currentMSecsSinceEpoch()) / 1000.0 << "s";
if (available_at > latest_available_at)
{
latest_available_at = available_at;
}
}
}
}

Expand Down Expand Up @@ -1436,6 +1452,28 @@ QStringList YouTube::getYouTubeVideo(const QString &param, const QString &url, I

result += o["description"].toString();

if (latest_available_at > 0)
Comment thread
RJVB marked this conversation as resolved.
{
const auto dt = int( (latest_available_at - QDateTime::currentMSecsSinceEpoch()) / 1000.0 + 0.5);
Comment thread
RJVB marked this conversation as resolved.
Outdated
if (dt > 0) {
Comment thread
RJVB marked this conversation as resolved.
Outdated
QMPlay2Core.logInfo(QString::asprintf("Waiting for %ds as required by YouTube", dt));
Comment thread
RJVB marked this conversation as resolved.
Outdated
// do an active wait; a less active clone of QTest::qWait():
auto remaining = dt * 1000;
QDeadlineTimer timer(remaining, Qt::PreciseTimer);
do
{
QCoreApplication::processEvents(QEventLoop::AllEvents, remaining);
QCoreApplication::sendPostedEvents(Q_NULLPTR, QEvent::DeferredDelete);
Comment thread
RJVB marked this conversation as resolved.
Outdated
remaining = timer.remainingTime();
if (remaining <= 0)
{
break;
}
usleep(qMin(100, remaining) * 1000);
Comment thread
RJVB marked this conversation as resolved.
Outdated
remaining = timer.remainingTime();
} while (remaining > 0);
}
}
return result;
}

Expand Down