summaryrefslogtreecommitdiff
path: root/lib/farside.ex
diff options
context:
space:
mode:
authorBen Busby <contact@benbusby.com>2025-01-21 13:46:29 -0700
committerBen Busby <contact@benbusby.com>2025-01-21 13:46:29 -0700
commitb5bad4defc6c75b9b969658229ce5fd2f3a46107 (patch)
treeacc460a4e15669e71dc61f0df2df5a27c6d2f965 /lib/farside.ex
parente0e395f3c82627190897683a40e4ba28104a03f9 (diff)
downloadfarside-b5bad4defc6c75b9b969658229ce5fd2f3a46107.tar.gz
Rewrite project, add daily update of services list
The project was rewritten from Elixir to Go, primarily because: - I don't write Elixir anymore and don't want to maintain a project in a language I no longer write - I already write Go for other projects, including my day job, so it's a safer bet for a project that I want to maintain long term - Go allows me to build portable executables that will make it easier for others to run farside on their own machines The Go version of Farsside also has a built in task to fetch the latest services{-full}.json file from the repo and ingest it, which makes running a farside server a lot simpler. It also automatically fetches the latest instance state from https://farside.link unless configured as a primary farside node, which will allow others to use farside without increasing traffic to all instances that are queried by farside (just to the farside node itself).
Diffstat (limited to 'lib/farside.ex')
-rw-r--r--lib/farside.ex142
1 files changed, 0 insertions, 142 deletions
diff --git a/lib/farside.ex b/lib/farside.ex
deleted file mode 100644
index 348f77c..0000000
--- a/lib/farside.ex
+++ /dev/null
@@ -1,142 +0,0 @@
- defmodule Farside do
- @service_prefix Application.compile_env!(:farside, :service_prefix)
- @fallback_suffix Application.compile_env!(:farside, :fallback_suffix)
- @previous_suffix Application.compile_env!(:farside, :previous_suffix)
-
- # Define relation between available services and their parent service.
- # This enables Farside to redirect with links such as:
- # farside.link/https://www.youtube.com/watch?v=dQw4w9WgXcQ
- @youtube_regex ~r/youtu(.be|be.com)|invidious|piped/
- @twitter_regex ~r/twitter.com|x.com|nitter/
- @reddit_regex ~r/reddit.com|libreddit|redlib/
- @instagram_regex ~r/instagram.com|proxigram/
- @wikipedia_regex ~r/wikipedia.org|wikiless/
- @medium_regex ~r/medium.com|scribe/
- @odysee_regex ~r/odysee.com|librarian/
- @imgur_regex ~r/imgur.com|rimgo/
- @gtranslate_regex ~r/translate.google.com|lingva/
- @tiktok_regex ~r/tiktok.com|proxitok/
- @imdb_regex ~r/imdb.com|libremdb/
- @quora_regex ~r/quora.com|quetre/
- @gsearch_regex ~r/google.com\/search|whoogle/
- @fandom_regex ~r/fandom.com|breezewiki/
- @github_regex ~r/github.com|gothub/
- @stackoverflow_regex ~r/stackoverflow.com|anonymousoverflow/
-
- @parent_services %{
- @youtube_regex => ["invidious", "piped"],
- @reddit_regex => ["libreddit", "redlib"],
- @instagram_regex => ["proxigram"],
- @twitter_regex => ["nitter"],
- @wikipedia_regex => ["wikiless"],
- @medium_regex => ["scribe"],
- @odysee_regex => ["librarian"],
- @imgur_regex => ["rimgo"],
- @gtranslate_regex => ["lingva"],
- @tiktok_regex => ["proxitok"],
- @imdb_regex => ["libremdb"],
- @quora_regex => ["quetre"],
- @gsearch_regex => ["whoogle"],
- @fandom_regex => ["breezewiki"],
- @github_regex => ["gothub"],
- @stackoverflow_regex => ["anonymousoverflow"]
- }
-
- def get_services_map do
- service_list = CubDB.select(CubDB)
- |> Stream.map(fn {key, _value} -> key end)
- |> Stream.filter(fn key -> String.starts_with?(key, @service_prefix) end)
- |> Enum.to_list
-
- # Match service name to list of available instances
- Enum.reduce(service_list, %{}, fn service, acc ->
- instance_list = CubDB.get(CubDB, service)
-
- Map.put(
- acc,
- String.replace_prefix(
- service,
- @service_prefix,
- ""
- ),
- instance_list
- )
- end)
- end
-
- def get_service(service) do
- # Check if service has an entry in the db, otherwise try to
- # match against available parent services
- service_name = cond do
- !check_service(service) ->
- Enum.find_value(
- @parent_services,
- fn {k, v} ->
- String.match?(service, k) && Enum.random(v)
- end)
- true ->
- service
- end
-
- service_name
- end
-
- def check_service(service) do
- # Checks to see if a specific service has instances available
- instances = CubDB.get(CubDB, "#{@service_prefix}#{service}")
-
- instances != nil && Enum.count(instances) > 0
- end
-
- def last_instance(service) do
- # Fetches the last selected instance for a particular service
- CubDB.get(CubDB, "#{service}#{@previous_suffix}")
- end
-
- def pick_instance(service) do
- instances = CubDB.get(CubDB, "#{@service_prefix}#{service}")
-
- # Either pick a random available instance,
- # or fall back to the default one
- instance =
- if instances != nil && Enum.count(instances) > 0 do
- if Enum.count(instances) == 1 do
- # If there's only one instance, just return that one...
- List.first(instances)
- else
- # ...otherwise pick a random one from the list, ensuring
- # that the same instance is never picked twice in a row.
- instance =
- Enum.filter(instances, &(&1 != last_instance(service)))
- |> Enum.random()
-
- CubDB.put(CubDB, "#{service}#{@previous_suffix}", instance)
-
- instance
- end
- else
- CubDB.get(CubDB, "#{service}#{@fallback_suffix}")
- end
- instance
- end
-
- def amend_instance(instance, service, path) do
- cond do
- String.match?(service, @fandom_regex) ->
- # Fandom links require the subdomain to be preserved, otherwise the
- # requested path won't work.
- if String.contains?(service, ".fandom.com") do
- wiki = String.replace(service, ".fandom.com", "")
- "#{instance}/#{wiki}"
- else
- instance
- end
- true ->
- instance
- end
- end
-
- def get_last_updated do
- CubDB.get(CubDB, "last_updated")
- end
-end