Use format str for search engine queries

Although the current list of queries is only 2 elements ("time" and
"weather"), this allows searches for Whoogle and Searx to be slightly
more random, and potentially avoid any issues with their parent engine
rate limiting them.
This commit is contained in:
Ben Busby 2021-11-29 16:25:00 -07:00
parent 17f83649c1
commit 04388fcf3c
No known key found for this signature in database
GPG key ID: B9B7231E01D924A1
5 changed files with 48 additions and 27 deletions

View file

@ -14,4 +14,8 @@ config :farside,
{"Accept", "text/html"},
{"Accept-Language", "en-US,en;q=0.5"},
{"Accept-Encoding", "gzip, deflate, br"}
],
queries: [
"weather",
"time"
]

View file

@ -7,23 +7,25 @@ defmodule Farside.Application do
@impl true
def start(_type, _args) do
plug_children = System.get_env("FARSIDE_NO_ROUTER") && [] || [
Plug.Cowboy.child_spec(
scheme: :http,
plug: Farside.Router,
options: [
port: @farside_port
plug_children =
(System.get_env("FARSIDE_NO_ROUTER") && []) ||
[
Plug.Cowboy.child_spec(
scheme: :http,
plug: Farside.Router,
options: [
port: @farside_port
]
),
{PlugAttack.Storage.Ets, name: Farside.Throttle.Storage, clean_period: 60_000}
]
),
{PlugAttack.Storage.Ets, name: Farside.Throttle.Storage, clean_period: 60_000}
]
children = [
{Redix, {@redis_conn, [name: :redix]}},
Farside.Scheduler,
Farside.Server
] ++ plug_children
children =
[
{Redix, {@redis_conn, [name: :redix]}},
Farside.Scheduler,
Farside.Server
] ++ plug_children
opts = [strategy: :one_for_one, name: Farside.Supervisor]
Supervisor.start_link(children, opts)

View file

@ -4,6 +4,7 @@ defmodule Farside.Instances do
@services_json Application.fetch_env!(:farside, :services_json)
@service_prefix Application.fetch_env!(:farside, :service_prefix)
@headers Application.fetch_env!(:farside, :headers)
@queries Application.fetch_env!(:farside, :queries)
def sync() do
File.rename(@update_file, "#{@update_file}-prev")
@ -21,11 +22,13 @@ defmodule Farside.Instances do
cond do
System.get_env("FARSIDE_TEST") ->
:good
true ->
case HTTPoison.get(url, @headers) do
{:ok, %HTTPoison.Response{status_code: 200}} ->
# TODO: Add validation of results, not just status code
:good
_ ->
:bad
end
@ -38,11 +41,20 @@ defmodule Farside.Instances do
# Loop through all instances and check each for availability
for service <- json do
IO.puts "======== " <> service.type
result = Enum.filter(service.instances, fn(instance_url) ->
IO.puts " " <> instance_url
request(instance_url <> service.test_url) == :good
end)
IO.puts("======== " <> service.type)
result =
Enum.filter(service.instances, fn instance_url ->
request_url = instance_url <>
EEx.eval_string(
service.test_url,
query: Enum.random(@queries)
)
IO.puts(" " <> request_url)
request(request_url) == :good
end)
add_to_redis(service, result)
log_results(service.type, result)
@ -57,10 +69,13 @@ defmodule Farside.Instances do
])
# Update with new list of available instances
Redix.command(:redix, [
"LPUSH",
"#{@service_prefix}#{service.type}"
] ++ instances)
Redix.command(
:redix,
[
"LPUSH",
"#{@service_prefix}#{service.type}"
] ++ instances
)
# Set fallback to one of the available instances,
# or the default instance if all are "down"

View file

@ -13,7 +13,7 @@ defmodule Farside.Server do
Farside.Scheduler.new_job()
|> Quantum.Job.set_name(:sync)
|> Quantum.Job.set_schedule(~e[*/5 * * * *])
|> Quantum.Job.set_task(fn -> Farside.Instances.sync end)
|> Quantum.Job.set_task(fn -> Farside.Instances.sync() end)
|> Farside.Scheduler.add_job()
end

View file

@ -114,7 +114,7 @@
},
{
"type": "whoogle",
"test_url": "/search?q=github",
"test_url": "/search?q=<%=query%>",
"fallback": "https://search.garudalinux.org",
"instances": [
"https://search.albony.xyz",
@ -131,7 +131,7 @@
},
{
"type": "searx",
"test_url": "/search?q=github",
"test_url": "/search?q=<%=query%>",
"fallback": "https://searx.be",
"instances": [
"https://anon.sx",