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 QObject *reply = HttpUtils::yt().get(thumbnailUrl);
88 connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
91 void Video::setDuration(int value) {
93 formattedDuration = DataUtils::formatDuration(duration);
96 void Video::setViewCount(int value) {
98 formattedViewCount = DataUtils::formatCount(viewCount);
101 void Video::setPublished(const QDateTime &value) {
103 formattedPublished = DataUtils::formatDateTime(published);
106 void Video::setThumbnail(const QByteArray &bytes) {
107 qreal ratio = qApp->devicePixelRatio();
108 thumbnail.loadFromData(bytes);
109 thumbnail.setDevicePixelRatio(ratio);
110 const int thumbWidth = PlaylistItemDelegate::thumbWidth * ratio;
111 if (thumbnail.width() > thumbWidth)
112 thumbnail = thumbnail.scaledToWidth(thumbWidth, Qt::SmoothTransformation);
114 loadingThumbnail = false;
117 void Video::streamUrlLoaded(const QString &streamUrl, const QString &audioUrl) {
118 qDebug() << "Streams loaded";
119 this->streamUrl = streamUrl;
120 emit gotStreamUrl(streamUrl, audioUrl);
122 definitionCode = ytVideo->getDefinitionCode();
123 ytVideo->deleteLater();
127 definitionCode = ytjsVideo->getDefinitionCode();
128 ytjsVideo->deleteLater();
133 void Video::loadStreamUrlJS() {
135 qDebug() << "Already loading" << id;
138 ytjsVideo = new YTJSVideo(id, this);
139 connect(ytjsVideo, &YTJSVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
140 connect(ytjsVideo, &YTJSVideo::errorStreamUrl, this, [this](const QString &msg) {
141 ytjsVideo->deleteLater();
145 ytjsVideo->loadStreamUrl();
148 void Video::loadStreamUrlYT() {
150 qDebug() << "Already loading" << id;
153 ytVideo = new YTVideo(id, this);
154 connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
155 connect(ytVideo, &YTVideo::errorStreamUrl, this, [this](const QString &msg) {
156 emit errorStreamUrl(msg);
157 ytVideo->deleteLater();
160 ytVideo->loadStreamUrl();
163 void Video::loadStreamUrl() {
167 void Video::abortLoadStreamUrl() {
169 ytVideo->disconnect(this);
170 ytVideo->deleteLater();