You've already forked algora-tv
							
							
				mirror of
				https://github.com/algora-io/tv.git
				synced 2025-10-30 23:07:56 +02:00 
			
		
		
		
	periodically fetch new stargazer count
This commit is contained in:
		| @@ -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
									
								
							
							
						
						
									
										48
									
								
								lib/algora/stargazer.ex
									
									
									
									
									
										Normal 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 | ||||
| @@ -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> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user