#!/usr/bin/env bash set -e tput clear source "${HOME:?}/scripts/functions.sh" CITY_FILE="${HOME:?}/scripts/cities.txt" # do we use weather info? weather=1 tools=("fzf" "jq" "jrnl" "nvim" "sha256sum") check_tools "$tools" if [[ -z "$OPENWEATHER_APIKEY" ]]; then die "openweathermap api key is not set" 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: ") # Проверяем, выбран ли город 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 set_bold_text echo -n "Type a title: " reset_text_format read TITLE if [[ -z "$TITLE" ]]; then die "title not found" fi 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 set_text_color 1 set_bold_text echo "Error: API Response: $RESPONSE" reset_text_format while true; do set_text_color 50 set_bold_text read -p "continue? (y|n) " bad_weather_answer reset_text_format if [[ "$bad_weather_answer" = "n" ]]; then exit 1 else weather=0 break fi done fi # Парсим данные о погоде 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'))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')" SUNINFO="🌅 "$SUNRISE", 🌇 "$SUNSET"" IFS=',' read -r geo1 geo2 <<<"$GEO" fi TEMPLATE="${TITLE} - ${weather_emoji[WEATHER_ICON]} Weather: - ${CITY} - ${TEMPERATURES:-"none"} - ${SUNINFO:-"none"} - Geo: - ${geo1:-"none"} - ${geo2:-"none"}" FILENAME="/tmp/$(generate_random_string).jrnl" echo "$TEMPLATE" >"${FILENAME:?}" CHECKSUMM="$(checksumm "${FILENAME:?}")" nvim "${FILENAME:?}" || rm "${FILENAME:?}" if [[ "$CHECKSUMM" = "$(checksumm "${FILENAME:?}")" ]]; then rm "${FILENAME:?}" set_bold_text echo "Nothing saving..." reset_text_format exit 0 fi tput clear jrnl --import --file "${FILENAME:?}"