]> git.sur5r.net Git - minitube/blob - src/ytchannel.cpp
New upstream version 3.6
[minitube] / src / ytchannel.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 "ytchannel.h"
22 #include "database.h"
23 #include "http.h"
24 #include "httputils.h"
25 #include <QtSql>
26
27 #include "yt3.h"
28
29 #include "iconutils.h"
30
31 #include "ivchannel.h"
32 #include "videoapi.h"
33 #include "ytjschannel.h"
34
35 YTChannel::YTChannel(const QString &channelId, QObject *parent)
36     : QObject(parent), id(0), channelId(channelId), loadingThumbnail(false), notifyCount(0),
37       checked(0), watched(0), loaded(0), loading(false) {}
38
39 QHash<QString, YTChannel *> YTChannel::cache;
40
41 YTChannel *YTChannel::forId(const QString &channelId) {
42     if (channelId.isEmpty()) return 0;
43
44     auto i = cache.constFind(channelId);
45     if (i != cache.constEnd()) return i.value();
46
47     QSqlDatabase db = Database::instance().getConnection();
48     QSqlQuery query(db);
49     query.prepare("select id,name,description,thumb_url,notify_count,watched,checked,loaded "
50                   "from subscriptions where user_id=?");
51     query.bindValue(0, channelId);
52     bool success = query.exec();
53     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
54
55     YTChannel *channel = 0;
56     if (query.next()) {
57         // Change userId to ChannelId
58
59         channel = new YTChannel(channelId);
60         channel->id = query.value(0).toInt();
61         channel->displayName = query.value(1).toString();
62         channel->description = query.value(2).toString();
63         channel->thumbnailUrl = query.value(3).toString();
64         channel->notifyCount = query.value(4).toInt();
65         channel->watched = query.value(5).toUInt();
66         channel->checked = query.value(6).toUInt();
67         channel->loaded = query.value(7).toUInt();
68         channel->thumbnail = QPixmap(channel->getThumbnailLocation());
69         channel->thumbnail.setDevicePixelRatio(2.0);
70         channel->maybeLoadfromAPI();
71         cache.insert(channelId, channel);
72     }
73
74     return channel;
75 }
76
77 void YTChannel::maybeLoadfromAPI() {
78     if (loading) return;
79     if (channelId.isEmpty()) return;
80
81     uint now = QDateTime::currentDateTime().toTime_t();
82     static const int refreshInterval = 60 * 60 * 24 * 10;
83     if (loaded > now - refreshInterval) return;
84
85     loading = true;
86
87     if (VideoAPI::impl() == VideoAPI::YT3) {
88         QUrl url = YT3::instance().method("channels");
89         QUrlQuery q(url);
90         q.addQueryItem("id", channelId);
91         q.addQueryItem("part", "snippet");
92         url.setQuery(q);
93
94         QObject *reply = HttpUtils::yt().get(url);
95         connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResponse(QByteArray)));
96         connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
97     } else if (VideoAPI::impl() == VideoAPI::IV) {
98         auto ivChannel = new IVChannel(channelId);
99         connect(ivChannel, &IVChannel::error, this, &YTChannel::requestError);
100         connect(ivChannel, &IVChannel::loaded, this, [this, ivChannel] {
101             displayName = ivChannel->getDisplayName();
102             description = ivChannel->getDescription();
103             thumbnailUrl = ivChannel->getThumbnailUrl();
104             ivChannel->deleteLater();
105             emit infoLoaded();
106             storeInfo();
107             loading = false;
108         });
109     } else if (VideoAPI::impl() == VideoAPI::JS) {
110         auto ivChannel = new YTJSChannel(channelId);
111         connect(ivChannel, &YTJSChannel::error, this, &YTChannel::requestError);
112         connect(ivChannel, &YTJSChannel::loaded, this, [this, ivChannel] {
113             displayName = ivChannel->getDisplayName();
114             description = ivChannel->getDescription();
115             thumbnailUrl = ivChannel->getThumbnailUrl();
116             ivChannel->deleteLater();
117             emit infoLoaded();
118             storeInfo();
119             loading = false;
120         });
121     }
122 }
123
124 void YTChannel::parseResponse(const QByteArray &bytes) {
125     QJsonDocument doc = QJsonDocument::fromJson(bytes);
126     QJsonObject obj = doc.object();
127     const QJsonArray items = obj["items"].toArray();
128     for (const QJsonValue &v : items) {
129         QJsonObject item = v.toObject();
130         QJsonObject snippet = item["snippet"].toObject();
131         displayName = snippet["title"].toString();
132         description = snippet["description"].toString();
133         QJsonObject thumbnails = snippet["thumbnails"].toObject();
134         thumbnailUrl = thumbnails["medium"].toObject()["url"].toString();
135         qDebug() << displayName << description << thumbnailUrl;
136     }
137
138     emit infoLoaded();
139     storeInfo();
140     loading = false;
141 }
142
143 void YTChannel::loadThumbnail() {
144     if (loadingThumbnail) return;
145     if (thumbnailUrl.isEmpty()) return;
146     loadingThumbnail = true;
147
148     QUrl url(thumbnailUrl);
149     QObject *reply = HttpUtils::yt().get(url);
150     connect(reply, SIGNAL(data(QByteArray)), SLOT(storeThumbnail(QByteArray)));
151     connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
152 }
153
154 const QString &YTChannel::getThumbnailDir() {
155     static const QString thumbDir =
156             QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/channels/";
157     return thumbDir;
158 }
159
160 QString YTChannel::getThumbnailLocation() {
161     return getThumbnailDir() + channelId;
162 }
163
164 QString YTChannel::latestVideoId() {
165     QSqlDatabase db = Database::instance().getConnection();
166     QSqlQuery query(db);
167     query.prepare("select video_id from subscriptions_videos where user_id=? order by published "
168                   "desc limit 1");
169     query.bindValue(0, channelId);
170     bool success = query.exec();
171     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
172     if (!query.next()) return QString();
173     return query.value(0).toString();
174 }
175
176 void YTChannel::unsubscribe() {
177     YTChannel::unsubscribe(channelId);
178 }
179
180 void YTChannel::storeThumbnail(const QByteArray &bytes) {
181     thumbnail.loadFromData(bytes);
182     qreal maxRatio = 2.0;
183     thumbnail.setDevicePixelRatio(maxRatio);
184     const int maxWidth = 88 * maxRatio;
185
186     QDir dir;
187     dir.mkpath(getThumbnailDir());
188
189     if (thumbnail.width() > maxWidth) {
190         thumbnail = thumbnail.scaledToWidth(maxWidth, Qt::SmoothTransformation);
191         thumbnail.setDevicePixelRatio(maxRatio);
192         thumbnail.save(getThumbnailLocation(), "JPG");
193     } else {
194         QFile file(getThumbnailLocation());
195         if (!file.open(QIODevice::WriteOnly))
196             qWarning() << "Error opening file for writing" << file.fileName();
197         QDataStream stream(&file);
198         stream.writeRawData(bytes.constData(), bytes.size());
199     }
200
201     emit thumbnailLoaded();
202     loadingThumbnail = false;
203 }
204
205 void YTChannel::requestError(const QString &message) {
206     emit error(message);
207     qWarning() << message;
208     loading = false;
209     loadingThumbnail = false;
210 }
211
212 void YTChannel::storeInfo() {
213     if (channelId.isEmpty()) return;
214     QSqlDatabase db = Database::instance().getConnection();
215     QSqlQuery query(db);
216     query.prepare("update subscriptions set "
217                   "user_name=?, name=?, description=?, thumb_url=?, loaded=? "
218                   "where user_id=?");
219     qDebug() << userName;
220     query.bindValue(0, userName);
221     query.bindValue(1, displayName);
222     query.bindValue(2, description);
223     query.bindValue(3, thumbnailUrl);
224     query.bindValue(4, QDateTime::currentDateTime().toTime_t());
225     query.bindValue(5, channelId);
226     bool success = query.exec();
227     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
228
229     loadThumbnail();
230 }
231
232 void YTChannel::subscribe(const QString &channelId) {
233     if (channelId.isEmpty()) return;
234
235     uint now = QDateTime::currentDateTime().toTime_t();
236
237     QSqlDatabase db = Database::instance().getConnection();
238     QSqlQuery query(db);
239     query.prepare("insert into subscriptions "
240                   "(user_id,added,watched,checked,views,notify_count)"
241                   " values (?,?,?,0,0,0)");
242     query.bindValue(0, channelId);
243     query.bindValue(1, now);
244     query.bindValue(2, now);
245     bool success = query.exec();
246     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
247
248     // This will call maybeLoadFromApi
249     YTChannel::forId(channelId);
250 }
251
252 void YTChannel::unsubscribe(const QString &channelId) {
253     if (channelId.isEmpty()) return;
254     QSqlDatabase db = Database::instance().getConnection();
255     QSqlQuery query(db);
256     query.prepare("delete from subscriptions where user_id=?");
257     query.bindValue(0, channelId);
258     bool success = query.exec();
259     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
260
261     query = QSqlQuery(db);
262     query.prepare("delete from subscriptions_videos where user_id=?");
263     query.bindValue(0, channelId);
264     success = query.exec();
265     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
266
267     YTChannel *user = cache.take(channelId);
268     if (user) user->deleteLater();
269 }
270
271 bool YTChannel::isSubscribed(const QString &channelId) {
272     if (!Database::exists()) return false;
273     if (channelId.isEmpty()) return false;
274     QSqlDatabase db = Database::instance().getConnection();
275     QSqlQuery query(db);
276     query.prepare("select count(*) from subscriptions where user_id=?");
277     query.bindValue(0, channelId);
278     bool success = query.exec();
279     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
280     if (query.next()) return query.value(0).toInt() > 0;
281     return false;
282 }
283
284 void YTChannel::updateChecked() {
285     if (channelId.isEmpty()) return;
286
287     uint now = QDateTime::currentDateTime().toTime_t();
288     checked = now;
289
290     QSqlDatabase db = Database::instance().getConnection();
291     QSqlQuery query(db);
292     query.prepare("update subscriptions set checked=? where user_id=?");
293     query.bindValue(0, now);
294     query.bindValue(1, channelId);
295     bool success = query.exec();
296     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
297 }
298
299 void YTChannel::updateWatched() {
300     if (channelId.isEmpty()) return;
301
302     uint now = QDateTime::currentDateTime().toTime_t();
303     watched = now;
304     notifyCount = 0;
305     emit notifyCountChanged();
306
307     QSqlDatabase db = Database::instance().getConnection();
308     QSqlQuery query(db);
309     query.prepare(
310             "update subscriptions set watched=?, notify_count=0, views=views+1 where user_id=?");
311     query.bindValue(0, now);
312     query.bindValue(1, channelId);
313     bool success = query.exec();
314     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
315 }
316
317 void YTChannel::storeNotifyCount(int count) {
318     if (notifyCount != count) emit notifyCountChanged();
319     notifyCount = count;
320
321     QSqlDatabase db = Database::instance().getConnection();
322     QSqlQuery query(db);
323     query.prepare("update subscriptions set notify_count=? where user_id=?");
324     query.bindValue(0, count);
325     query.bindValue(1, channelId);
326     bool success = query.exec();
327     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
328 }
329
330 bool YTChannel::updateNotifyCount() {
331     QSqlDatabase db = Database::instance().getConnection();
332     QSqlQuery query(db);
333     query.prepare("select count(*) from subscriptions_videos "
334                   "where channel_id=? and added>? and published>? and watched=0");
335     query.bindValue(0, id);
336     query.bindValue(1, watched);
337     query.bindValue(2, watched);
338     bool success = query.exec();
339     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
340     if (!query.next()) {
341         qWarning() << __PRETTY_FUNCTION__ << "Count failed";
342         return false;
343     }
344     int count = query.value(0).toInt();
345     storeNotifyCount(count);
346     return count != notifyCount;
347 }