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 "videodefinition.h"
30 : duration(0), viewCount(-1), license(LicenseYouTube), definitionCode(0),
31 loadingThumbnail(false), ytVideo(0) {}
34 qDebug() << "Deleting" << id;
37 Video *Video::clone() {
38 Video *clone = new Video();
40 clone->description = description;
41 clone->channelTitle = channelTitle;
42 clone->channelId = channelId;
43 clone->webpage = webpage;
44 clone->streamUrl = streamUrl;
45 clone->thumbnail = thumbnail;
46 clone->thumbnailUrl = thumbnailUrl;
47 clone->mediumThumbnailUrl = mediumThumbnailUrl;
48 clone->duration = duration;
49 clone->formattedDuration = formattedDuration;
50 clone->published = published;
51 clone->formattedPublished = formattedPublished;
52 clone->viewCount = viewCount;
54 clone->definitionCode = definitionCode;
58 const QString &Video::getWebpage() {
59 if (webpage.isEmpty() && !id.isEmpty())
60 webpage.append("https://www.youtube.com/watch?v=").append(id);
64 void Video::setWebpage(const QString &value) {
69 QRegExp re(JsFunctions::instance()->videoIdRE());
70 if (re.indexIn(webpage) == -1) {
71 qWarning() << QString("Cannot get video id for %1").arg(webpage);
72 // emit errorStreamUrl(QString("Cannot get video id for %1").arg(m_webpage.toString()));
73 // loadingStreamUrl = false;
80 void Video::loadThumbnail() {
81 if (thumbnailUrl.isEmpty() || loadingThumbnail) return;
82 loadingThumbnail = true;
83 QObject *reply = HttpUtils::yt().get(thumbnailUrl);
84 connect(reply, SIGNAL(data(QByteArray)), SLOT(setThumbnail(QByteArray)));
87 void Video::setDuration(int value) {
89 formattedDuration = DataUtils::formatDuration(duration);
92 void Video::setPublished(const QDateTime &value) {
94 formattedPublished = DataUtils::formatDateTime(published);
97 void Video::setThumbnail(const QByteArray &bytes) {
98 qreal ratio = qApp->devicePixelRatio();
99 thumbnail.loadFromData(bytes);
100 thumbnail.setDevicePixelRatio(ratio);
101 const int thumbWidth = 160 * ratio;
102 if (thumbnail.width() > thumbWidth)
103 thumbnail = thumbnail.scaledToWidth(thumbWidth, Qt::SmoothTransformation);
105 loadingThumbnail = false;
108 void Video::streamUrlLoaded(const QUrl &streamUrl) {
109 definitionCode = ytVideo->getDefinitionCode();
110 this->streamUrl = streamUrl;
111 emit gotStreamUrl(this->streamUrl);
116 void Video::loadStreamUrl() {
118 qDebug() << "Already loading" << id;
121 ytVideo = new YTVideo(id, this);
122 connect(ytVideo, &YTVideo::gotStreamUrl, this, &Video::streamUrlLoaded);
123 connect(ytVideo, &YTVideo::errorStreamUrl, this, &Video::errorStreamUrl);
124 connect(ytVideo, &YTVideo::errorStreamUrl, ytVideo, &QObject::deleteLater);
125 ytVideo->loadStreamUrl();