Browse Source

feat: switch to openweathermap onecall api

tags/v0.63.0
Inhji Y. 2 weeks ago
parent
commit
1eaa55a0b5
5 changed files with 23 additions and 13 deletions
  1. +1
    -1
      apps/indie/lib/token.ex
  2. +3
    -3
      apps/scraper/lib/scraper/weather.ex
  3. +14
    -6
      apps/scraper/lib/scraper/workers/weather.ex
  4. +3
    -3
      apps/tomie_web/lib/tomie_web/templates/page/index.html.leex
  5. +2
    -0
      config/config.exs

+ 1
- 1
apps/indie/lib/token.ex View File

@@ -67,7 +67,7 @@ defmodule Indie.Token do
:ok

_ ->
Logger.warn("Hostnames do not match: Given #{host_uri}, Actual: #{@hostname}")
Logger.warn("Hostnames do not match: Given #{host_uri}, Actual: #{own_hostname}")
{:error, "verify_hostname_match", "hostname does not match"}
end
end


+ 3
- 3
apps/scraper/lib/scraper/weather.ex View File

@@ -1,8 +1,8 @@
defmodule Scraper.Weather do
def get_weather() do
[api_key: api_key, city_id: city_id] = Application.fetch_env!(:scraper, :weather)
base_url = "http://api.openweathermap.org/data/2.5/weather"
url = "#{base_url}?id=#{city_id}&appid=#{api_key}&units=metric"
[api_key: api_key, latitude: lat, longitude: lon, city_id: _] = Application.fetch_env!(:scraper, :weather)
base_url = "http://api.openweathermap.org/data/2.5/onecall"
url = "#{base_url}?lat=#{lat}&lon=#{lon}&appid=#{api_key}&units=metric,exclude=minutely,daily"

with %{body: body, status_code: 200} <- Http.get!(url),
{:ok, json} <- Jason.decode!(body, keys: :atoms) do


+ 14
- 6
apps/scraper/lib/scraper/workers/weather.ex View File

@@ -8,14 +8,22 @@ defmodule Scraper.Workers.Weather do

@impl Oban.Worker
def perform(_args, _job) do
weather = Scraper.Weather.get_weather()
%{
current: current,
daily: _daily,
hourly: _hourly,
lat: _lat,
lon: _lon,
timezone: _timezone,
timezone_offset: _timezone_offset
} = Scraper.Weather.get_weather()

Cachex.put(:weather, :now, %{
pretty: weather.weather |> List.first(),
temp: weather.main,
clouds: weather.clouds,
location: weather.name,
timestamp: weather.dt
pretty: current.weather |> List.first(),
temp: current.temp,
clouds: current.clouds,
timestamp: current.dt,
humidity: current.humidity
})
end
end

+ 3
- 3
apps/tomie_web/lib/tomie_web/templates/page/index.html.leex View File

@@ -7,11 +7,11 @@
</div>

<div class="body">
<h3><span class="text-secondary font-bold"><%= @weather.temp.temp %>°</span> and <%= @weather.pretty.main %> in <%= @weather.location %></h3>
<p><%= @weather.clouds.all %>% cloudy, <%= @weather.temp.humidity %>% humidity</p>
<h3><span class="text-secondary font-bold"><%= @weather.temp %>°</span> and <%= @weather.pretty.main %></h3>
<p><%= @weather.clouds %>% cloudy, <%= @weather.humidity %>% humidity</p>

<div class="meta">
<span><%= @weather.timestamp |>DateTime.from_unix! |> Timex.from_now %></span>
<span><%= @weather.timestamp |> DateTime.from_unix! |> Timex.from_now %></span>
</div>
</div>
<% else %>


+ 2
- 0
config/config.exs View File

@@ -21,6 +21,8 @@ config :listens,

config :scraper, :weather,
api_key: "5e8d6569a828fd7860acf87bf1113a9e",
latitude: 50.583832,
longitude: 8.677890,
city_id: "2920512"

config :indie,


Loading…
Cancel
Save