1
0
Fork 0
mirror of https://github.com/benbusby/farside.git synced 2025-04-20 10:58:42 +00:00

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", "text/html"},
{"Accept-Language", "en-US,en;q=0.5"}, {"Accept-Language", "en-US,en;q=0.5"},
{"Accept-Encoding", "gzip, deflate, br"} {"Accept-Encoding", "gzip, deflate, br"}
],
queries: [
"weather",
"time"
] ]

View file

@ -7,8 +7,9 @@ defmodule Farside.Application do
@impl true @impl true
def start(_type, _args) do def start(_type, _args) do
plug_children =
plug_children = System.get_env("FARSIDE_NO_ROUTER") && [] || [ (System.get_env("FARSIDE_NO_ROUTER") && []) ||
[
Plug.Cowboy.child_spec( Plug.Cowboy.child_spec(
scheme: :http, scheme: :http,
plug: Farside.Router, plug: Farside.Router,
@ -19,7 +20,8 @@ defmodule Farside.Application do
{PlugAttack.Storage.Ets, name: Farside.Throttle.Storage, clean_period: 60_000} {PlugAttack.Storage.Ets, name: Farside.Throttle.Storage, clean_period: 60_000}
] ]
children = [ children =
[
{Redix, {@redis_conn, [name: :redix]}}, {Redix, {@redis_conn, [name: :redix]}},
Farside.Scheduler, Farside.Scheduler,
Farside.Server Farside.Server

View file

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

View file

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

View file

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