1
0
mirror of https://github.com/algora-io/tv.git synced 2024-11-26 01:00:20 +02:00

periodically fetch new stargazer count

This commit is contained in:
zafer 2024-04-27 22:06:46 +03:00
parent 64dcdbd8d5
commit 7836a8bf5c
3 changed files with 52 additions and 3 deletions

View File

@ -52,8 +52,8 @@ defmodule Algora.Application do
%{
id: Membrane.RTMP.Source.TcpServer,
start: {Membrane.RTMP.Source.TcpServer, :start_link, [tcp_server_options]}
}
},
Algora.Stargazer
# Start a worker by calling: Algora.Worker.start_link(arg)
# {Algora.Worker, arg}
]

48
lib/algora/stargazer.ex Normal file
View File

@ -0,0 +1,48 @@
defmodule Algora.Stargazer do
require Logger
use GenServer
@url "https://api.github.com/repos/algora-io/tv"
@poll_interval :timer.minutes(10)
def start_link(cmd) do
GenServer.start_link(__MODULE__, cmd, name: __MODULE__)
end
@impl true
def init(cmd) do
{:ok, schedule_fetch(%{count: nil}, cmd, 0)}
end
@impl true
def handle_info(cmd, state) do
count = fetch_count() || state.count
{:noreply, schedule_fetch(%{state | count: count}, cmd)}
end
defp schedule_fetch(state, cmd, after_ms \\ @poll_interval) do
Process.send_after(self(), cmd, after_ms)
state
end
defp fetch_count() do
with {:ok, %Finch.Response{status: 200, body: body}} <-
:get
|> Finch.build(@url)
|> Finch.request(Algora.Finch),
{:ok, %{"stargazers_count" => count}} <- Jason.decode(body) do
count
else
_ -> nil
end
end
def count() do
GenServer.call(__MODULE__, :get_count)
end
@impl true
def handle_call(:get_count, _from, state) do
{:reply, state.count, state}
end
end

View File

@ -137,6 +137,7 @@
</div>
</a>
<a
:if={Algora.Stargazer.count()}
class="group outline-none w-fit"
target="_blank"
rel="noopener noreferrer"
@ -166,7 +167,7 @@
</defs>
</svg>
<span class="hidden xl:block">Star</span>
<span class="font-semibold text-gray-300">22</span>
<span class="font-semibold text-gray-300"><%= Algora.Stargazer.count() %></span>
</div>
</div>
</a>