20250113.1736787084

This commit is contained in:
fz0x1 2025-01-13 17:51:24 +01:00
parent cfce6c21e1
commit ba47a5917e
3 changed files with 55 additions and 30 deletions

Binary file not shown.

View file

@ -16,15 +16,54 @@ if [[ -z "$OPENWEATHER_APIKEY" ]]; then
die "openweathermap api key is not set"
fi
if [[ ! -f "$CITY_FILE" ]]; then
die "cities.txt is not found"
fi
function manual_geo() {
if [[ ! -f "$CITY_FILE" ]]; then
die "cities.txt is not found"
fi
CITY=$(cat "$CITY_FILE" | fzf --prompt="Type a city: ")
CITY=$(cat "$CITY_FILE" | fzf --prompt="Type a city: ")
# Проверяем, выбран ли город
if [[ -z "$CITY" ]]; then
die "city not selected"
# Проверяем, выбран ли город
if [[ -z "$CITY" ]]; then
die "city not selected"
fi
while true; do
set_text_color 43
set_bold_text
echo -n "Type a geo: "
reset_text_format
read GEO
# if a string is empty then skip it
if [[ -z "$GEO" ]]; then
break
fi
if [[ "$GEO" =~ ^-?[0-9]+\.[0-9]+,-?[0-9]+\.[0-9]+$ ]]; then
break
else
set_text_color 1
set_bold_text
echo "Wrong geo format (lat and lon 52.18969496420507,20.90814238187223)"
reset_text_format
fi
done
}
connection=$(check_connection)
if [[ "$connection" = 0 ]]; then
manual_geo
else
http_code=$(curl -H "Authorization: Basic ${GEO_SERVER_AUTH}" -s -o /dev/null -w "%{http_code}" "${GEO_SERVER_URL}?action=diary&type=get_geo")
echo "$http_code"
if [ "$http_code" -ne 200 ]; then
echo "Connection problem...\n"
manual_geo
fi
location=$(curl -H "Authorization: Basic ${GEO_SERVER_AUTH}" -s "${GEO_SERVER_URL}?action=diary&type=get_geo")
IFS=":" read -r lat lon CITY country timestamp <<<"$location"
GEO="$lat,$lon"
fi
set_text_color 43
@ -37,28 +76,6 @@ if [[ -z "$TITLE" ]]; then
die "title not found"
fi
while true; do
set_text_color 43
set_bold_text
echo -n "Type a geo: "
reset_text_format
read GEO
# if a string is empty then skip it
if [[ -z "$GEO" ]]; then
break
fi
if [[ "$GEO" =~ ^-?[0-9]+\.[0-9]+,-?[0-9]+\.[0-9]+$ ]]; then
break
else
set_text_color 1
set_bold_text
echo "Wrong geo format (lat and lon 52.18969496420507,20.90814238187223)"
reset_text_format
fi
done
RESPONSE=$(curl -s "https://api.openweathermap.org/data/2.5/weather?q=${CITY}&appid=${OPENWEATHER_APIKEY}&units=metric")
if [[ "$(echo "$RESPONSE" | jq '.cod')" != "200" ]]; then
@ -85,7 +102,7 @@ if [[ "$weather" -eq 1 ]]; then
WEATHER="$(echo "$RESPONSE" | jq -r '.weather[0].description')"
TZ="$(calculate_timezone "$(echo "$RESPONSE" | jq -r '.timezone')")"
WEATHER_ICON="$(echo "$RESPONSE" | jq -r '.weather[0].icon')"
TEMPERATURES="🌡️ $(round $(echo "$RESPONSE" | jq -r '.main.temp')) ($(round $(echo "$RESPONSE" | jq -r '.main.feels_like')))"
TEMPERATURES="🌡️ $(round $(echo "$RESPONSE" | jq -r '.main.temp'))C ($(round $(echo "$RESPONSE" | jq -r '.main.feels_like'))C)"
SUNRISE="$(date --date='@'"$(echo "$RESPONSE" | jq -r '.sys.sunrise')" +'%H:%M')"
SUNSET="$(date --date='@'"$(echo "$RESPONSE" | jq -r '.sys.sunset')" +'%H:%M')"

View file

@ -69,3 +69,11 @@ function generate_random_string() {
function checksumm() {
echo $(sha256sum "${1:?}" | awk '{print $1}')
}
function check_connection() {
if ping -c 1 google.com &>/dev/null; then
echo 1
else
echo 0
fi
}