3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
22 #include "datautils.h"
24 #include "httputils.h"
25 #include "jsfunctions.h"
26 #include "playlistitemdelegate.h"
27 #include "videodefinition.h"
31 : duration(0), viewCount(-1), license(LicenseYouTube), definitionCode(0),
32 loadingThumbnail(false), ytVideo(nullptr) {}
35 qDebug() << "Deleting" << id;
38 Video *Video::clone() {
39 Video *clone = new Video();
41 clone->description = description;
42 clone->channelTitle = channelTitle;
43 clone->channelId = channelId;
44 clone->webpage = webpage;
45 clone->streamUrl = streamUrl;
46 clone->thumbnail = thumbnail;
47 clone->thumbnailUrl = thumbnailUrl;
48 clone->mediumThumbnailUrl = mediumThumbnailUrl;
49 clone->duration = duration;
50 clone->formattedDuration = formattedDuration;
51 clone->published = published;
52 clone->formattedPublished = formattedPublished;
53 clone->viewCount = viewCount;
54 clone->formattedViewCount = formattedViewCount;
56 clone->definitionCode = definitionCode;
60 const QString &Video::getWebpage() {
61 if (webpage.isEmpty() && !id.isEmpty())
62 webpage.append("https://www.youtube.com/watch?v=").append(id);
66 void Video::setWebpage(const QString &value) {
71 QRegExp re(JsFunctions::instance()->videoIdRE());
72 if (re.indexIn(webpage) == -1) {
73 qWarning() << QString("Cannot get video id for %1").arg(webpage);
74 // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
75 // loadingStreamUrl = false;
82 void Video::loadThumbnail() {
83 if (thumbnailUrl.isEmpty() || loadingThumbnail) return;
84 loadingThumbnail = true;
85 QObject *reply = HttpUtils::yt().get(thumbnailUrl);
86 connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
89 void Video::setDuration(int value) {
91 formattedDuration = DataUtils::formatDuration(duration);
94 void Video::setViewCount(int value) {
96 formattedViewCount = DataUtils::formatCount(viewCount);
99 void Video::setPublished(const QDateTime &value) {
101 formattedPublished = DataUtils::formatDateTime(published);
104 void Video::setThumbnail(const QByteArray &bytes) {
105 qreal ratio = qApp->devicePixelRatio();
106 thumbnail.loadFromData(bytes);
107 thumbnail.setDevicePixelRatio(ratio);
108 const int thumbWidth = PlaylistItemDelegate::thumbWidth * ratio;
109 if (thumbnail.width() > thumbWidth)
110 thumbnail = thumbnail.scaledToWidth(thumbWidth, Qt::SmoothTransformation);
112 loadingThumbnail = false;
115 void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) {
116 qDebug() << "Streams loaded";
117 definitionCode = ytVideo->getDefinitionCode();
118 this->streamUrl = streamUrl;
119 emit gotStreamUrl(streamUrl, audioUrl);
120 ytVideo->deleteLater();
124 void Video::loadStreamUrl() {
126 qDebug() << "Already loading" << id;
129 ytVideo = new YTVideo(id, this);
130 connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
131 connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) {
132 emit errorStreamUrl(msg);
133 ytVideo->deleteLater();
136 ytVideo->loadStreamUrl();
139 void Video::abortLoadStreamUrl() {
141 ytVideo->disconnect(this);
142 ytVideo->deleteLater();