diff --git a/global/.zshrc-secrets b/global/.zshrc-secrets index 3c6258c..5666a7e 100644 Binary files a/global/.zshrc-secrets and b/global/.zshrc-secrets differ diff --git a/global/scripts/bin/diary.sh b/global/scripts/bin/diary.sh index f0db9dd..e70275d 100755 --- a/global/scripts/bin/diary.sh +++ b/global/scripts/bin/diary.sh @@ -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')" diff --git a/global/scripts/functions.sh b/global/scripts/functions.sh index 44027f3..bc7ca0a 100644 --- a/global/scripts/functions.sh +++ b/global/scripts/functions.sh @@ -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 +}