From 83ed36c176ac888cd1d48d8c8fd8fcb8edeb2f33 Mon Sep 17 00:00:00 2001
From: Davide Aprea <davide@suma-ev.de>
Date: Thu, 25 Feb 2021 12:17:27 +0100
Subject: [PATCH] add command to generate a list of instances

---
 .gitignore                              |  1 +
 app/Console/Commands/SearxInstances.php | 59 +++++++++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 app/Console/Commands/SearxInstances.php

diff --git a/.gitignore b/.gitignore
index b4bd3bc..b30cf85 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@
 /public/hot
 /public/storage
 /storage/*.key
+/storage/instances.txt
 /vendor
 .env
 .env.backup
diff --git a/app/Console/Commands/SearxInstances.php b/app/Console/Commands/SearxInstances.php
new file mode 100644
index 0000000..cd7f637
--- /dev/null
+++ b/app/Console/Commands/SearxInstances.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+class SearxInstances extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'instances:load';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'loads a list of running searx instances ( https://searx.space )';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, 'https://searx.space/data/instances.json');
+        curl_setopt($ch, CURLOPT_HEADER, FALSE);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
+        $response = curl_exec($ch);
+        curl_close($ch);
+        $response = json_decode($response);
+        if($response !== null){
+            $f = fopen('storage/instances.txt', 'w');
+            foreach($response->instances as $instance => $data) {
+                if(!isset($data->{'error'}) && $data->{'network_type'} !== 'tor') {
+                    fwrite($f, $instance . "\n");
+                }
+            }
+            fclose($f);
+        } else {
+            echo "Unable to locate file. Check https://searx.space/data/instances.json for more info\n";
+        }
+    }
+}
-- 
GitLab