3 #include "cachedhttp.h"
6 #include "throttledhttp.h"
8 Invidious &Invidious::instance() {
13 Http &Invidious::http() {
15 Http *http = new Http;
16 http->addRequestHeader("User-Agent", HttpUtils::stealthUserAgent());
17 http->setMaxRetries(0);
23 Http &Invidious::cachedHttp() {
25 ThrottledHttp *throttledHttp = new ThrottledHttp(http());
26 throttledHttp->setMilliseconds(300);
28 CachedHttp *cachedHttp = new CachedHttp(*throttledHttp, "iv");
29 cachedHttp->setMaxSeconds(86400);
30 cachedHttp->setIgnoreHostname(true);
36 Invidious::Invidious(QObject *parent) : QObject(parent) {}
38 void Invidious::initServers() {
40 QUrl url("https://instances.invidio.us/instances.json?sort_by=type,health,users");
41 auto reply = HttpUtils::yt().get(url);
42 connect(reply, &HttpReply::finished, this, [this](auto &reply) {
43 if (reply.isSuccessful()) {
45 QStringList keywords = settings.value("recentKeywords").toStringList();
46 QString testKeyword = keywords.isEmpty() ? "test" : keywords.first();
48 bool haveEnoughServers = false;
49 QJsonDocument doc = QJsonDocument::fromJson(reply.body());
50 for (const auto &v : doc.array()) {
51 auto serverArray = v.toArray();
52 QString host = serverArray.first().toString();
53 QJsonObject serverObj = serverArray.at(1).toObject();
54 if (serverObj["type"] == "https") {
55 QString url = "https://" + host;
57 if (haveEnoughServers) break;
58 QUrl testUrl(url + "/api/v1/search?q=" + testKeyword);
59 auto reply = http().get(testUrl);
60 connect(reply, &HttpReply::finished, this,
61 [this, url, &haveEnoughServers](auto &reply) {
62 if (!haveEnoughServers && reply.isSuccessful()) {
63 QJsonDocument doc = QJsonDocument::fromJson(reply.body());
64 if (!doc.array().isEmpty()) {
66 if (servers.size() > 4) {
67 haveEnoughServers = true;
68 std::shuffle(servers.begin(), servers.end(),
69 *QRandomGenerator::global());
71 emit serversInitialized();
82 QString Invidious::baseUrl() {
84 if (servers.isEmpty())
85 host = "https://invidious.snopyta.org";
87 host = servers.first();
88 QString url = host + QLatin1String("/api/v1/");
92 QUrl Invidious::method(const QString &name) {
93 QUrl url(baseUrl() + name);