Skip to content

Commit

Permalink
new trending
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviotordini committed Jun 20, 2021
1 parent 73235cd commit 8923cd2
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ void MainWindow::createActions() {
action->setEnabled(false);
actionMap.insert("refineSearch", action);

action = new QAction(YTRegions::worldwideRegion().name, this);
action = new QAction(YTRegions::defaultRegion().name, this);
actionMap.insert("worldwideRegion", action);

action = new QAction(YTRegions::localRegion().name, this);
Expand Down
2 changes: 1 addition & 1 deletion src/regionsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ RegionsView::RegionsView(QWidget *parent) : View(parent) {
layout->setSpacing(0);
l->addLayout(layout);

addRegion(YTRegions::worldwideRegion());
addRegion(YTRegions::defaultRegion());
foreach (YTRegion region, YTRegions::list())
addRegion(region);

Expand Down
22 changes: 19 additions & 3 deletions src/standardfeedsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ along with Minitube. If not, see <http://www.gnu.org/licenses/>.
#include "ivvideolist.h"
#include "videoapi.h"

#include "ytjstrending.h"

StandardFeedsView::StandardFeedsView(QWidget *parent) : View(parent), layout(0) {
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
Expand All @@ -46,12 +48,26 @@ void StandardFeedsView::load() {

YTRegion region = YTRegions::currentRegion();

// TODO consolidate in YT
if (VideoAPI::impl() == VideoAPI::YT3) {
YTCategories *youTubeCategories = new YTCategories(this);
connect(youTubeCategories, SIGNAL(categoriesLoaded(const QVector<YTCategory> &)),
SLOT(layoutCategories(const QVector<YTCategory> &)));
youTubeCategories->loadCategories();
addVideoSourceWidget(buildStandardFeed("most_popular", tr("Most Popular")));
} else if (VideoAPI::impl() == VideoAPI::JS) {
const QMap<QString, QString> pages = {{"default", tr("Trending")},
{"music", tr("Music")},
{"movies", tr("Movies")},
{"gaming", tr("Gaming")}};
auto i = pages.constBegin();
while (i != pages.constEnd()) {
addVideoSourceWidget(
new YTJSTrending(i.value(), {{"page", i.key()}, {"geoLocation", region.id}}));
++i;
}

setUpdatesEnabled(true);
} else {
QString regionParam = "region=" + region.id;
addVideoSourceWidget(new IVVideoList("popular?" + regionParam, tr("Most Popular")));
Expand Down Expand Up @@ -88,7 +104,7 @@ void StandardFeedsView::addVideoSourceWidget(VideoSource *videoSource) {
connect(w, SIGNAL(unavailable(VideoSourceWidget *)),
SLOT(removeVideoSourceWidget(VideoSourceWidget *)));
int i = layout->count();
const int cols = VideoAPI::impl() == VideoAPI::YT3 ? 5 : 3;
const int cols = VideoAPI::impl() == VideoAPI::YT3 ? 5 : 2;
layout->addWidget(w, i / cols, i % cols);
}

Expand All @@ -107,7 +123,7 @@ void StandardFeedsView::removeVideoSourceWidget(VideoSourceWidget *videoSourceWi
}

const int itemCount = items.size();
const int cols = 4; // itemCount / 3;
const int cols = 2; // itemCount / 3;
for (int i = itemCount - 1; i >= 0; i--) {
QLayoutItem *item = items.at(i);
int index = itemCount - 1 - i;
Expand Down Expand Up @@ -155,7 +171,7 @@ void StandardFeedsView::disappear() {
}

void StandardFeedsView::selectWorldwideRegion() {
YTRegions::setRegion(YTRegions::worldwideRegion().id);
YTRegions::setRegion(YTRegions::defaultRegion().id);
load();
}

Expand Down
2 changes: 2 additions & 0 deletions src/yt/ytjs/ytjs.pri
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ HEADERS += \
$$PWD/ytjschannelsource.h \
$$PWD/ytjssearch.h \
$$PWD/ytjssinglevideosource.h \
$$PWD/ytjstrending.h \
$$PWD/ytjsvideo.h

SOURCES += \
$$PWD/ytjschannel.cpp \
$$PWD/ytjschannelsource.cpp \
$$PWD/ytjssearch.cpp \
$$PWD/ytjssinglevideosource.cpp \
$$PWD/ytjstrending.cpp \
$$PWD/ytjsvideo.cpp
104 changes: 104 additions & 0 deletions src/yt/ytjs/ytjstrending.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "ytjstrending.h"

#include "js.h"
#include "video.h"

namespace {

QDateTime parsePublishedText(const QString &s) {
int num = 0;
const auto parts = s.splitRef(' ');
for (const auto &part : parts) {
num = part.toInt();
if (num > 0) break;
}
if (num == 0) return QDateTime();

auto now = QDateTime::currentDateTimeUtc();
if (s.contains("hour")) {
return now.addSecs(-num * 3600);
} else if (s.contains("day")) {
return now.addDays(-num);
} else if (s.contains("week")) {
return now.addDays(-num * 7);
} else if (s.contains("month")) {
return now.addMonths(-num);
} else if (s.contains("year")) {
return now.addDays(-num * 365);
}
return QDateTime();
}

} // namespace

YTJSTrending::YTJSTrending(QString name, QVariantMap params, QObject *parent)
: VideoSource(parent), name(name), params(params) {}

void YTJSTrending::loadVideos(int max, int startIndex) {
aborted = false;

auto &js = JS::instance();

QJSValue options = js.getEngine().toScriptValue(params);

js.callFunction(new JSResult(this), "trending", {options})
.onJson([this](auto &doc) {
const auto items = doc.array();

QVector<Video *> videos;
videos.reserve(items.size());

for (const auto &i : items) {
QString type = i["type"].toString();
if (type != "video") continue;

Video *video = new Video();

QString id = i["videoId"].toString();
video->setId(id);

QString title = i["title"].toString();
video->setTitle(title);

QString desc = i["description"].toString();
if (desc.isEmpty()) desc = i["desc"].toString();
video->setDescription(desc);

const auto thumbs = i["videoThumbnails"].toArray();
for (const auto &t : thumbs) {
video->addThumb(t["width"].toInt(), t["height"].toInt(),
t["url"].toString());
}

int views = i["viewCount"].toInt();
video->setViewCount(views);

int duration = i["lengthSeconds"].toInt();
video->setDuration(duration);

auto published = parsePublishedText(i["publishedText"].toString());
if (published.isValid()) video->setPublished(published);

QString channelName = i["author"].toString();
video->setChannelTitle(channelName);
QString channelId = i["authorId"].toString();
video->setChannelId(channelId);

videos << video;
}

emit gotVideos(videos);
emit finished(videos.size());
})
.onError([this, max, startIndex](auto &msg) {
static int retries = 0;
if (retries < 3) {
qDebug() << "Retrying...";
QTimer::singleShot(0, this,
[this, max, startIndex] { loadVideos(max, startIndex); });
retries++;
} else {
emit error(msg);
}
});
}
23 changes: 23 additions & 0 deletions src/yt/ytjs/ytjstrending.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef YTJSTRENDING_H
#define YTJSTRENDING_H

#include "videosource.h"

class Video;

class YTJSTrending : public VideoSource {
Q_OBJECT

public:
YTJSTrending(QString name, QVariantMap params, QObject *parent = 0);
void loadVideos(int max, int startIndex);
void abort() { aborted = true; }
QString getName() { return name; }

private:
const QString name;
const QVariantMap params;
bool aborted = false;
};

#endif // YTJSTRENDING_H
8 changes: 4 additions & 4 deletions src/ytregions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ const YTRegion &YTRegions::localRegion() {
return region;
}

const YTRegion &YTRegions::worldwideRegion() {
static const YTRegion region = {"", tr("Worldwide")};
const YTRegion &YTRegions::defaultRegion() {
static const YTRegion region = {"US", tr("United States")};
return region;
}

Expand All @@ -115,11 +115,11 @@ const YTRegion &YTRegions::currentRegion() {
}

const YTRegion &YTRegions::regionById(const QString &id) {
if (id.isEmpty()) return worldwideRegion();
if (id.isEmpty()) return defaultRegion();
for (const YTRegion &r : list()) {
if (r.id == id) return r;
}
return worldwideRegion();
return defaultRegion();
}

QIcon YTRegions::iconForRegionId(const QString &regionId) {
Expand Down
2 changes: 1 addition & 1 deletion src/ytregions.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class YTRegions : public QObject {
public:
static const QVector<YTRegion> & list();
static const YTRegion & localRegion();
static const YTRegion & worldwideRegion();
static const YTRegion & defaultRegion();
static void setRegion(const QString &regionId);
static QString currentRegionId();
static const YTRegion &currentRegion();
Expand Down

0 comments on commit 8923cd2

Please sign in to comment.