l radio news: use openweather api
This commit is contained in:
parent
2cc551b8d2
commit
5befc5a6f2
@ -1,29 +1,27 @@
|
|||||||
{ config, lib, pkgs, ... }:
|
{ config, lib, pkgs, ... }:
|
||||||
let
|
let
|
||||||
|
weather_for_ips = pkgs.writers.writePython3Bin "weather_for_ips" {
|
||||||
|
libraries = [ pkgs.python3Packages.geoip2 ];
|
||||||
|
} ./weather_for_ips.py;
|
||||||
|
|
||||||
weather_report = pkgs.writers.writeDashBin "weather_report" ''
|
weather_report = pkgs.writers.writeDashBin "weather_report" ''
|
||||||
set -efu
|
set -efu
|
||||||
export PATH="${lib.makeBinPath [
|
export PATH="${lib.makeBinPath [
|
||||||
pkgs.iproute2
|
pkgs.iproute2
|
||||||
pkgs.coreutils
|
pkgs.coreutils
|
||||||
pkgs.jq
|
|
||||||
pkgs.curl
|
pkgs.curl
|
||||||
pkgs.gnugrep
|
pkgs.gnugrep
|
||||||
pkgs.gnused
|
pkgs.gnused
|
||||||
]}"
|
]}"
|
||||||
|
curl -z /tmp/GeoLite2-City.mmdb -o /tmp/GeoLite2-City.mmdb http://c.r/GeoLite2-City.mmdb
|
||||||
|
MAXMIND_GEOIP_DB="/tmp/GeoLite2-City.mmdb"; export MAXMIND_GEOIP_DB
|
||||||
|
OPENWEATHER_API_KEY=$(cat "$CREDENTIALS_DIRECTORY/openweather_api"); export OPENWEATHER_API_KEY
|
||||||
ss -Hno state established 'sport = :8000' |
|
ss -Hno state established 'sport = :8000' |
|
||||||
grep '^tcp' | sed 's/.*\[.*\].*\[\(::ffff:\)\{0,1\}\(.*\)\].*/\2/' |
|
grep '^tcp' | sed 's/.*\[.*\].*\[\(::ffff:\)\{0,1\}\(.*\)\].*/\2/' |
|
||||||
sed '/127.0.0.1/d;/:/d' |
|
sed '/127.0.0.1/d;/::1/d' |
|
||||||
while read -r ip; do
|
${weather_for_ips}/bin/weather_for_ips
|
||||||
curl -sSL "https://wttr.in/@$ip?format=j1"
|
|
||||||
done | jq -rs 'unique_by(.nearest_area[0].areaName[0].value) |
|
|
||||||
map((.nearest_area[0] |
|
|
||||||
"Weather report for \(.areaName[0].value), \(.country[0].value).")
|
|
||||||
+ (.current_condition[0] |
|
|
||||||
" Currently it is \(.weatherDesc[0].value) outside with a temperature of \(.temp_C) degrees."
|
|
||||||
)
|
|
||||||
) | unique | .[]'
|
|
||||||
'
|
|
||||||
'';
|
'';
|
||||||
|
|
||||||
send_to_radio = pkgs.writers.writeDashBin "send_to_radio" ''
|
send_to_radio = pkgs.writers.writeDashBin "send_to_radio" ''
|
||||||
${pkgs.vorbisTools}/bin/oggenc - |
|
${pkgs.vorbisTools}/bin/oggenc - |
|
||||||
${pkgs.libshout}/bin/shout --format ogg --host localhost --port 1338 --mount /live
|
${pkgs.libshout}/bin/shout --format ogg --host localhost --port 1338 --mount /live
|
||||||
@ -72,6 +70,9 @@ in
|
|||||||
startAt = "*:00:00";
|
startAt = "*:00:00";
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
User = "radio-news";
|
User = "radio-news";
|
||||||
|
LoadCredential = [
|
||||||
|
"openweather_api:${toString <secrets>}/openweather_api_key"
|
||||||
|
];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
33
lass/2configs/radio/weather_for_ips.py
Normal file
33
lass/2configs/radio/weather_for_ips.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import geoip2.database
|
||||||
|
import fileinput
|
||||||
|
import json
|
||||||
|
import requests
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
geoip = geoip2.database.Reader(os.environ['MAXMIND_GEOIP_DB'])
|
||||||
|
seen = {}
|
||||||
|
output = []
|
||||||
|
for ip in fileinput.input():
|
||||||
|
location = geoip.city(ip.strip())
|
||||||
|
if location.city.geoname_id not in seen:
|
||||||
|
seen[location.city.geoname_id] = True
|
||||||
|
weather_api_key = os.environ['OPENWEATHER_API_KEY']
|
||||||
|
url = (
|
||||||
|
f'https://api.openweathermap.org/data/2.5/onecall'
|
||||||
|
f'?lat={location.location.latitude}'
|
||||||
|
f'&lon={location.location.longitude}'
|
||||||
|
f'&appid={weather_api_key}'
|
||||||
|
f'&units=metric'
|
||||||
|
)
|
||||||
|
resp = requests.get(url)
|
||||||
|
weather = json.loads(resp.text)['current']
|
||||||
|
output.append(
|
||||||
|
f'Weather report for {location.city.name}'
|
||||||
|
f', {location.country.name}. '
|
||||||
|
f'Currently it is {weather["weather"][0]["description"]} outside '
|
||||||
|
f'with a temperature of {weather["temp"]} degrees '
|
||||||
|
f'and a windspeed of {weather["wind_speed"]} meter per second. '
|
||||||
|
)
|
||||||
|
|
||||||
|
print('\n'.join(output))
|
Loading…
Reference in New Issue
Block a user