]> git.sur5r.net Git - minitube/blob - src/video.cpp
52432b8d0efcf950566c4eb27f941d2448240f22
[minitube] / src / video.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "video.h"
22 #include "datautils.h"
23 #include "http.h"
24 #include "httputils.h"
25 #include "jsfunctions.h"
26 #include "videodefinition.h"
27 #include "ytvideo.h"
28
29 Video::Video()
30     : duration(0), viewCount(-1), license(LicenseYouTube), definitionCode(0),
31       loadingThumbnail(false), ytVideo(0) {}
32
33 Video::~Video() {
34     qDebug() << "Deleting" << id;
35 }
36
37 Video *Video::clone() {
38     Video *clone = new Video();
39     clone->title = title;
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;
53     clone->id = id;
54     clone->definitionCode = definitionCode;
55     return clone;
56 }
57
58 const QString &Video::getWebpage() {
59     if (webpage.isEmpty() && !id.isEmpty())
60         webpage.append("https://www.youtube.com/watch?v=").append(id);
61     return webpage;
62 }
63
64 void Video::setWebpage(const QString &value) {
65     webpage = value;
66
67     // Get Video ID
68     if (id.isEmpty()) {
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;
74             return;
75         }
76         id = re.cap(1);
77     }
78 }
79
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)));
85 }
86
87 void Video::setDuration(int value) {
88     duration = value;
89     formattedDuration = DataUtils::formatDuration(duration);
90 }
91
92 void Video::setPublished(const QDateTime &value) {
93     published = value;
94     formattedPublished = DataUtils::formatDateTime(published);
95 }
96
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);
104     emit gotThumbnail();
105     loadingThumbnail = false;
106 }
107
108 void Video::streamUrlLoaded(const QUrl &streamUrl) {
109     definitionCode = ytVideo->getDefinitionCode();
110     this->streamUrl = streamUrl;
111     emit gotStreamUrl(this->streamUrl);
112     delete ytVideo;
113     ytVideo = 0;
114 }
115
116 void Video::loadStreamUrl() {
117     if (ytVideo) {
118         qDebug() << "Already loading" << id;
119         return;
120     }
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();
126 }