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"
29 #include "ytjsvideo.h"
33 : duration(0), viewCount(-1), license(LicenseYouTube), definitionCode(0),
34 loadingThumbnail(false), ytVideo(nullptr), ytjsVideo(nullptr) {}
37 qDebug() << "Deleting" << id;
40 Video *Video::clone() {
41 Video *clone = new Video();
43 clone->description = description;
44 clone->channelTitle = channelTitle;
45 clone->channelId = channelId;
46 clone->webpage = webpage;
47 clone->streamUrl = streamUrl;
48 clone->thumbnail = thumbnail;
49 clone->thumbnailUrl = thumbnailUrl;
50 clone->mediumThumbnailUrl = mediumThumbnailUrl;
51 clone->duration = duration;
52 clone->formattedDuration = formattedDuration;
53 clone->published = published;
54 clone->formattedPublished = formattedPublished;
55 clone->viewCount = viewCount;
56 clone->formattedViewCount = formattedViewCount;
58 clone->definitionCode = definitionCode;
62 const QString &Video::getWebpage() {
63 if (webpage.isEmpty() && !id.isEmpty())
64 webpage.append("https://www.youtube.com/watch?v=").append(id);
68 void Video::setWebpage(const QString &value) {
73 QRegExp re(JsFunctions::instance()->videoIdRE());
74 if (re.indexIn(webpage) == -1) {
75 qWarning() << QString("Cannot get video id for %1").arg(webpage);
76 // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
77 // loadingStreamUrl = false;
84 void Video::loadThumbnail() {
85 if (thumbnailUrl.isEmpty() || loadingThumbnail) return;
86 loadingThumbnail = true;
87 auto reply = HttpUtils::yt().get(thumbnailUrl);
88 connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
89 connect(reply, &HttpReply::error, this, [this](auto &msg) {
91 loadingThumbnail = false;
95 void Video::setDuration(int value) {
97 formattedDuration = DataUtils::formatDuration(duration);
100 void Video::setViewCount(int value) {
102 formattedViewCount = DataUtils::formatCount(viewCount);
105 void Video::setPublished(const QDateTime &value) {
107 formattedPublished = DataUtils::formatDateTime(published);
110 void Video::setThumbnail(const QByteArray &bytes) {
111 qreal ratio = qApp->devicePixelRatio();
112 thumbnail.loadFromData(bytes);
113 thumbnail.setDevicePixelRatio(ratio);
114 const int thumbWidth = PlaylistItemDelegate::thumbWidth * ratio;
115 if (thumbnail.width() > thumbWidth)
116 thumbnail = thumbnail.scaledToWidth(thumbWidth, Qt::SmoothTransformation);
118 loadingThumbnail = false;
121 void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) {
122 qDebug() << "Streams loaded";
123 this->streamUrl = streamUrl;
124 emit gotStreamUrl(streamUrl, audioUrl);
126 definitionCode = ytVideo->getDefinitionCode();
127 ytVideo->deleteLater();
131 definitionCode = ytjsVideo->getDefinitionCode();
132 ytjsVideo->deleteLater();
137 void Video::loadStreamUrlJS() {
139 qDebug() << "Already loading" << id;
142 ytjsVideo = new YTJSVideo(id, this);
143 connect(ytjsVideo, &YTJSVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
144 connect(ytjsVideo, &YTJSVideo::errorStreamUrl, this, [this](const QString &msg) {
146 ytjsVideo->deleteLater();
150 ytjsVideo->loadStreamUrl();
153 void Video::loadStreamUrlYT() {
155 qDebug() << "Already loading" << id;
158 ytVideo = new YTVideo(id, this);
159 connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
160 connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) {
162 emit errorStreamUrl(msg);
163 ytVideo->deleteLater();
166 ytVideo->loadStreamUrl();
169 void Video::loadStreamUrl() {
173 void Video::abortLoadStreamUrl() {
175 ytVideo->disconnect(this);
176 ytVideo->deleteLater();