File indexing completed on 2025-02-23 04:35:14

0001 // SPDX-FileCopyrightText: 2019 Linus Jahn <lnj@kaidan.im>
0002 // SPDX-License-Identifier: GPL-3.0-or-later
0003 
0004 #include "videobasicinfo.h"
0005 
0006 using namespace QInvidious;
0007 using namespace Qt::StringLiterals;
0008 
0009 VideoBasicInfo VideoBasicInfo::fromJson(const QJsonObject &obj, VideoBasicInfo &info)
0010 {
0011     const bool isPeerTube = obj.contains("id"_L1);
0012     const bool isInvidious = obj.contains("videoId"_L1);
0013     if (isPeerTube) {
0014         info.m_videoId = obj.value("uuid"_L1).toString();
0015         info.m_title = obj.value("name"_L1).toString();
0016         info.m_length = QTime(0, 0).addSecs(obj.value("duration"_L1).toInt());
0017         info.m_viewCount = obj.value("views"_L1).toInt();
0018 
0019         const auto channel = obj.value("channel"_L1).toObject();
0020 
0021         info.m_author = channel.value("displayName"_L1).toString();
0022         info.m_authorId = channel.value("name"_L1).toString();
0023         info.m_authorUrl = channel.value("url"_L1).toString();
0024         info.m_published = QDateTime::fromString(obj.value("createdAt"_L1).toString(), Qt::ISODate);
0025         info.m_description = obj.value("description"_L1).toString();
0026         info.m_descriptionHtml = obj.value("description"_L1).toString();
0027         info.m_liveNow = obj.value("isLive"_L1).toBool(false);
0028         info.m_paid = false;
0029         info.m_premium = false;
0030         info.m_upcoming = false;
0031 
0032         VideoThumbnail thumbnail;
0033         const auto thumbnailPath = obj.value("thumbnailPath"_L1).toString();
0034         if (thumbnailPath.startsWith(u'/')) {
0035             thumbnail.setUrl(QUrl(obj.value("thumbnailPath"_L1).toString()));
0036         } else {
0037             thumbnail.setUrl(QUrl::fromUserInput(thumbnailPath));
0038         }
0039         info.m_videoThumbnails.push_back(thumbnail);
0040     } else if (isInvidious) {
0041         info.m_videoId = obj.value("videoId"_L1).toString();
0042         info.m_title = obj.value("title"_L1).toString();
0043         info.m_length = QTime(0, 0).addSecs(obj.value("lengthSeconds"_L1).toInt());
0044         info.m_viewCount = obj.value("viewCount"_L1).toInt();
0045         info.m_author = obj.value("author"_L1).toString();
0046         info.m_authorId = obj.value("authorId"_L1).toString();
0047         info.m_authorUrl = obj.value("authorUrl"_L1).toString();
0048         // FIXME: 2038 problem (timestamp is only 32 bit long)
0049         if (obj.contains("published"_L1)) {
0050             info.m_published = QDateTime::fromSecsSinceEpoch(obj.value("published"_L1).toInt());
0051         }
0052         info.m_description = obj.value("description"_L1).toString();
0053         info.m_descriptionHtml = obj.value("descriptionHtml"_L1).toString();
0054         info.m_liveNow = obj.value("liveNow"_L1).toBool(false);
0055         info.m_paid = obj.value("paid"_L1).toBool(false);
0056         info.m_premium = obj.value("premium"_L1).toBool(false);
0057         info.m_upcoming = obj.value("isUpcoming"_L1).toBool(false);
0058         parseArray(obj.value("videoThumbnails"_L1), info.m_videoThumbnails);
0059         if (obj.contains("indexId"_L1)) {
0060             info.m_indexId = obj.value("indexId"_L1).toString();
0061         }
0062     } else {
0063         info.m_videoId = obj.value("url"_L1).toString().remove(QStringLiteral("/watch?v="));
0064         info.m_title = obj.value("title"_L1).toString();
0065         info.m_length = QTime(0, 0).addSecs(obj.value("duration"_L1).toInt());
0066         info.m_viewCount = obj.value("views"_L1).toInt();
0067         info.m_author = obj.value("uploaderName"_L1).toString();
0068         info.m_authorUrl = obj.value("uploaderUrl"_L1).toString();
0069         info.m_authorId = obj.value("uploaderUrl"_L1).toString().remove(QStringLiteral("/channel/"));
0070         // FIXME: 2038 problem (timestamp is only 32 bit long)
0071         info.m_published = QDateTime::fromSecsSinceEpoch(obj.value("uploaded"_L1).toInt());
0072         if (obj.contains("description"_L1)) {
0073             info.m_description = obj.value("description"_L1).toString();
0074             info.m_descriptionHtml = obj.value("description"_L1).toString();
0075         } else {
0076             info.m_description = obj.value("shortDescription"_L1).toString();
0077             info.m_descriptionHtml = obj.value("shortDescription"_L1).toString();
0078         }
0079         info.m_liveNow = false;
0080         info.m_paid = false;
0081         info.m_premium = false;
0082         info.m_upcoming = false;
0083 
0084         VideoThumbnail thumbnail;
0085         thumbnail.setUrl(QUrl::fromUserInput(obj.value("thumbnail"_L1).toString()));
0086         info.m_videoThumbnails.push_back(thumbnail);
0087     }
0088     return info;
0089 }
0090 
0091 QString VideoBasicInfo::videoId() const
0092 {
0093     return m_videoId;
0094 }
0095 
0096 void VideoBasicInfo::setVideoId(const QString &videoId)
0097 {
0098     m_videoId = videoId;
0099 }
0100 
0101 QString VideoBasicInfo::title() const
0102 {
0103     return m_title;
0104 }
0105 
0106 QList<VideoThumbnail> VideoBasicInfo::videoThumbnails() const
0107 {
0108     return m_videoThumbnails;
0109 }
0110 
0111 void VideoBasicInfo::setVideoThumbnails(const QList<VideoThumbnail> &videoThumbnails)
0112 {
0113     m_videoThumbnails = videoThumbnails;
0114 }
0115 
0116 VideoThumbnail VideoBasicInfo::thumbnail(const QString &quality) const
0117 {
0118     for (auto &thumb : m_videoThumbnails) {
0119         if (thumb.quality() == quality)
0120             return thumb;
0121     }
0122     // didn't found requested quality
0123     if (!m_videoThumbnails.isEmpty())
0124         return m_videoThumbnails.last();
0125     return {};
0126 }
0127 
0128 QTime VideoBasicInfo::length() const
0129 {
0130     return m_length;
0131 }
0132 
0133 qint64 VideoBasicInfo::viewCount() const
0134 {
0135     return m_viewCount;
0136 }
0137 
0138 QString VideoBasicInfo::author() const
0139 {
0140     return m_author;
0141 }
0142 
0143 QString VideoBasicInfo::authorId() const
0144 {
0145     return m_authorId;
0146 }
0147 
0148 QString VideoBasicInfo::authorUrl() const
0149 {
0150     return m_authorUrl;
0151 }
0152 
0153 QDateTime VideoBasicInfo::published() const
0154 {
0155     return m_published;
0156 }
0157 
0158 QString VideoBasicInfo::publishedText() const
0159 {
0160     return m_published.date().toString();
0161 }
0162 
0163 QString VideoBasicInfo::description() const
0164 {
0165     return m_description;
0166 }
0167 
0168 QString VideoBasicInfo::descriptionHtml() const
0169 {
0170     return m_descriptionHtml;
0171 }
0172 
0173 bool VideoBasicInfo::liveNow() const
0174 {
0175     return m_liveNow;
0176 }
0177 
0178 bool VideoBasicInfo::paid() const
0179 {
0180     return m_paid;
0181 }
0182 
0183 bool VideoBasicInfo::premium() const
0184 {
0185     return m_premium;
0186 }
0187 
0188 bool VideoBasicInfo::upcoming() const
0189 {
0190     return m_upcoming;
0191 }
0192 
0193 QString VideoBasicInfo::indexId() const
0194 {
0195     return m_indexId;
0196 }