about summary refs log blame commit diff stats
path: root/src/invapi.cpp
blob: b96d18bab0480e1891d24bd0d4568cc9e1e4d378 (plain) (tree)
1
2
3
4
5
6
7
8
9

                 
                   



                            

                     























                                                                                     
#include <string>
#include <vector>
#include <iostream>

#include <cpr/cpr.h>
#include <nlohmann/json.hpp>

#include "invapi.hpp"

using namespace std;
using json = nlohmann::json;

vector<string> getInstances() {

    vector<string> result;
    cpr::Response r = cpr::Get(cpr::Url{"https://api.invidious.io/instances.json"}, 
        cpr::Parameters{{"sort_by", "health"}});

    if (r.status_code > 299) {
        
        cerr << "invidious.io returned error code " << r.status_code << "!!" << endl;
        throw runtime_error("bad response");
    }

    json j = json::parse(r.text);
    for ( json i: j ) {
        if ( i[1]["api"] != json::value_t::null && i[1]["api"]) {

            result.push_back( i[1]["uri"] );
        }
    }
    return result;
}