LibreY/engines/librex/fallback.php

69 lines
1.9 KiB
PHP
Raw Normal View History

2023-08-07 06:54:25 -07:00
<?php
2023-08-22 16:38:59 -07:00
class LibreXFallback extends EngineRequest {
public function __construct($instance, $opts, $mh) {
$this->instance = $instance;
parent::__construct($opts, $mh);
}
public function get_request_url() {
return $this->instance . "api.php?" . opts_to_params($this->opts);
}
public function get_results() {
$response = curl_exec($this->ch);
$response = json_decode($response, true);
if (!$response)
return array();
2023-08-25 08:23:53 -07:00
2023-08-22 16:38:59 -07:00
return array_values($response);
}
}
2023-08-25 08:23:53 -07:00
function load_instances($cooldowns) {
$instances_json = json_decode(file_get_contents("instances.json"), true);
if (empty($instances_json["instances"]))
return array();
$instances = array_map(fn($n) => $n['clearnet'], array_filter($instances_json['instances'], fn($n) => !is_null($n['clearnet'])));
$instances = array_filter($instances, fn($n) => !has_cooldown($n, $cooldowns));
shuffle($instances);
return $instances;
}
2023-08-07 06:54:25 -07:00
2023-08-22 16:38:59 -07:00
function get_librex_results($opts) {
if (!$opts->do_fallback)
2023-08-07 06:54:25 -07:00
return array();
2023-08-25 09:39:08 -07:00
$cooldowns = $opts->cooldowns;
2023-08-25 08:23:53 -07:00
$instances = load_instances($cooldowns);
2023-08-07 10:27:54 -07:00
$results = array();
2023-08-07 10:27:54 -07:00
$tries = 0;
do {
2023-08-07 10:27:54 -07:00
$tries++;
$instance = array_pop($instances);
if (parse_url($instance)["host"] == parse_url($_SERVER['HTTP_HOST'])["host"])
continue;
2023-08-22 16:38:59 -07:00
$librex_request = new LibreXFallback($instance, $opts, null);
$results = $librex_request->get_results();
2023-08-22 16:38:59 -07:00
if (count($results) > 1)
return $results;
2023-08-07 06:54:25 -07:00
2023-08-25 08:23:53 -07:00
// on fail then do this
$timeout = ($opts->request_cooldown ?? "1") * 60;
$cooldowns = set_cooldown($instance, $timeout, $cooldowns);
2023-08-19 12:16:43 -07:00
2023-08-25 08:23:53 -07:00
} while (!empty($instances));
2023-08-07 06:54:25 -07:00
2023-08-25 08:23:53 -07:00
return array();
2023-08-07 06:54:25 -07:00
}
2023-08-07 06:54:25 -07:00
?>