-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyt.cpp
64 lines (51 loc) · 1.84 KB
/
myt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "myt.h"
#include "mainwindow.h"
#include <QDebug>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QNetworkReply>
#include <QProcess>
MYTApp::MYTApp()
{
connect(&manager, &QNetworkAccessManager::finished, this, &MYTApp::responseReceived);
}
void MYTApp::getSearch(QString query, int page)
{
QUrl url = QUrl("http://youtube-scrape.herokuapp.com/api/search?q=" + query + "&page=" + QString::number(page));
manager.get(QNetworkRequest(url));
}
void MYTApp::responseReceived(QNetworkReply *reply)
{
searchResults.clear();
QJsonObject results = QJsonDocument::fromJson(reply->readAll()).object();
for(QJsonValue element : results["results"].toArray())
{
QJsonObject node = element.toObject();
QJsonValue video = node["video"];
QJsonValue uploader = node["uploader"];
videoData vid;
vid.title.setText(video["title"].toString());
vid.views.setText(video["views"].toString());
vid.channel.setText(uploader["username"].toString());
vid.videoUrl.setText(video["url"].toString());
vid.thumbnail.setData(Qt::DecorationRole, thumbGrab.fetchThumbnail(video["thumbnail_src"].toString()).scaledToWidth(150, Qt::SmoothTransformation));
vid.uploadDate.setText(video["upload_date"].toString());
searchResults.append(vid);
}
emit searchFinished(searchResults);
}
//! TODO: decide if we should wait for the child process or not.
void MYTApp::playVideo(QString url)
{
std::string command = "mpv " + url.toStdString();
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.c_str(), "r"), pclose);
if (!pipe)
{
qDebug() << "In MYTApp::playVideo: failed to run MPV process.";
}
// QProcess::execute(command);
// QProcess *proc = new QProcess();
// proc->start(command);
// delete proc;
}