videodownloader/network/vimeodownload.cpp

118 lines
4.0 KiB
C++
Raw Normal View History

2015-10-14 23:08:57 +02:00
#include "./vimeodownload.h"
2020-09-04 00:57:42 +02:00
#include <qtutilities/misc/compat.h>
2015-10-14 23:08:57 +02:00
#include <c++utilities/chrono/timespan.h>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
2019-06-10 22:50:15 +02:00
using namespace CppUtilities;
2015-10-14 22:58:55 +02:00
namespace Network {
2015-10-14 23:08:57 +02:00
/*!
* \class VimeoDownload
* \brief Download implementation for Vimeo videos.
*/
/*!
* \brief Constructs a new VimeoDownload for the specified \a url.
*/
2017-05-01 03:22:50 +02:00
VimeoDownload::VimeoDownload(const QUrl &url, QObject *parent)
: HttpDownloadWithInfoRequst(url, parent)
{
}
2015-10-14 23:08:57 +02:00
/*!
* \brief Constructs a new VimeoDownload for the specified video \a id.
*/
2017-05-01 03:22:50 +02:00
VimeoDownload::VimeoDownload(const QString &id, QObject *parent)
: HttpDownloadWithInfoRequst(QUrl(QStringLiteral("https://vimeo.com/%1").arg(id)), parent)
{
}
2015-10-14 23:08:57 +02:00
Download *VimeoDownload::infoRequestDownload(bool &success, QString &reasonForFail)
{
2020-09-04 00:57:42 +02:00
const auto pathParts = QtUtilities::splitRef(initialUrl().path(QUrl::FullyDecoded), QChar('/'), Qt::SkipEmptyParts);
2017-05-01 03:22:50 +02:00
if (pathParts.size() < 2) {
2015-10-14 23:08:57 +02:00
const auto &id = pathParts.back();
bool isInt;
2020-09-04 00:57:42 +02:00
static_cast<void>(id.toULongLong(&isInt));
2017-05-01 03:22:50 +02:00
if (isInt) {
setId(id
#if QT_VERSION >= 0x050400
2017-05-01 03:22:50 +02:00
.toString()
#endif
2019-07-20 20:20:58 +02:00
);
2015-10-14 23:08:57 +02:00
success = true;
return new HttpDownload(QUrl(QStringLiteral("https://player.vimeo.com/video/%1/config").arg(this->id())));
2015-10-14 23:08:57 +02:00
}
}
success = false;
reasonForFail = tr("The video ID couldn't be identified.");
return nullptr;
}
QString VimeoDownload::suitableFilename() const
2015-10-14 22:58:55 +02:00
{
2015-10-14 23:08:57 +02:00
auto filename = Download::suitableFilename();
2017-05-01 03:22:50 +02:00
if (!filename.endsWith(QLatin1String(".mp4"))) {
2015-10-14 23:08:57 +02:00
filename.append(QStringLiteral(".mp4"));
}
return filename;
}
2015-10-14 22:58:55 +02:00
2015-10-14 23:08:57 +02:00
QString VimeoDownload::typeName() const
{
return tr("Vimeo");
}
void VimeoDownload::evalVideoInformation(Download *, QBuffer *videoInfoBuffer)
{
QJsonParseError error;
const QJsonDocument doc = QJsonDocument::fromJson(videoInfoBuffer->readAll(), &error);
2017-05-01 03:22:50 +02:00
if (error.error == QJsonParseError::NoError) {
const auto h264Object = doc.object()
.value(QStringLiteral("request"))
.toObject()
.value(QStringLiteral("files"))
.toObject()
.value(QStringLiteral("h264"))
.toObject();
2015-10-14 23:08:57 +02:00
const auto videoObject = doc.object().value(QStringLiteral("video")).toObject();
const auto title = videoObject.value(QStringLiteral("title")).toString();
2017-05-01 03:22:50 +02:00
if (!title.isEmpty()) {
2015-10-14 23:08:57 +02:00
setTitle(title);
}
const auto uploader = videoObject.value(QStringLiteral("owner")).toObject().value(QStringLiteral("name")).toString();
2017-05-01 03:22:50 +02:00
if (!uploader.isEmpty()) {
2015-10-14 23:08:57 +02:00
setUploader(uploader);
}
2017-05-01 03:22:50 +02:00
if (const auto duration = videoObject.value(QStringLiteral("duration")).toInt()) {
2015-10-14 23:08:57 +02:00
setDuration(TimeSpan::fromSeconds(duration));
}
2017-05-01 03:22:50 +02:00
for (const auto &value : h264Object) {
2015-10-14 23:08:57 +02:00
const auto optionObject = value.toObject();
const auto url = optionObject.value(QStringLiteral("url")).toString();
2017-05-01 03:22:50 +02:00
if (!url.isEmpty()) {
2015-10-14 23:08:57 +02:00
const auto width = optionObject.value(QStringLiteral("width")).toInt();
const auto height = optionObject.value(QStringLiteral("height")).toInt();
const auto bitrate = optionObject.value(QStringLiteral("bitrate")).toInt();
addDownloadUrl(QStringLiteral("%1 x %2, %3 kbit/s").arg(width).arg(height).arg(bitrate), url);
}
}
2017-05-01 03:22:50 +02:00
if (availableOptionCount() > 0) {
2015-10-14 23:08:57 +02:00
reportInitiated(true);
} else {
2017-05-01 03:22:50 +02:00
reportInitiated(
false, tr("No video URLs found. The video config could be parsed, but it seems like Vimeo changed something in their API."));
2015-10-14 23:08:57 +02:00
}
} else {
reportInitiated(false, tr("Couldn't parse video configuration (invalid JSON)."));
}
2015-10-14 22:58:55 +02:00
}
} // namespace Network