202412301735586432 HAPPY NEW YEAR
This commit is contained in:
parent
9789a3db29
commit
65240b8408
10 changed files with 29481 additions and 25 deletions
121
global/scripts/bin/diary.sh
Executable file
121
global/scripts/bin/diary.sh
Executable file
|
@ -0,0 +1,121 @@
|
|||
#!/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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
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')) ($(round $(echo "$RESPONSE" | jq -r '.main.feels_like')))"
|
||||
|
||||
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:?}"
|
29272
global/scripts/cities.txt
Normal file
29272
global/scripts/cities.txt
Normal file
File diff suppressed because it is too large
Load diff
71
global/scripts/functions.sh
Normal file
71
global/scripts/functions.sh
Normal file
|
@ -0,0 +1,71 @@
|
|||
declare -A weather_emoji
|
||||
|
||||
weather_emoji[01d]=🌞
|
||||
weather_emoji[02d]=🌥
|
||||
weather_emoji[03d]=☁
|
||||
weather_emoji[04d]=☁
|
||||
weather_emoji[09d]=🌧
|
||||
weather_emoji[10d]=🌦
|
||||
weather_emoji[11d]=⛈
|
||||
weather_emoji[13d]=❄
|
||||
weather_emoji[50d]=🌫
|
||||
weather_emoji[01n]=🌑
|
||||
weather_emoji[02n]=🌑
|
||||
weather_emoji[03n]=☁
|
||||
weather_emoji[04n]=☁☁
|
||||
weather_emoji[09n]=🌧
|
||||
weather_emoji[10n]=☔
|
||||
weather_emoji[11n]=⛈
|
||||
weather_emoji[13n]=❄
|
||||
weather_emoji[50n]=🌫
|
||||
|
||||
function set_text_color() {
|
||||
tput setaf "$1"
|
||||
}
|
||||
|
||||
function set_bold_text() {
|
||||
tput bold
|
||||
}
|
||||
|
||||
function reset_text_format() {
|
||||
tput sgr0
|
||||
}
|
||||
|
||||
function die() {
|
||||
set_text_color 1
|
||||
echo "Script failed: $1, exiting..."
|
||||
reset_text_format
|
||||
exit 1
|
||||
}
|
||||
|
||||
function round() {
|
||||
echo $(echo "$1" | awk '{print ($1 > int($1)) ? int($1)+1 : int($1)}')
|
||||
}
|
||||
|
||||
function check_tools() {
|
||||
tools="$1"
|
||||
for tool in "${tools[@]}"; do
|
||||
if [ ! -x "$(command -v "$tool")" ]; then
|
||||
die "$tool is not installed"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
function calculate_timezone() {
|
||||
offset_hours=$(("${1:?}" / 3600))
|
||||
sign="+"
|
||||
if ((offset_hours < 0)); then
|
||||
sign="-"
|
||||
offset_hours=$((-"$offset_hours"))
|
||||
fi
|
||||
echo "${offset_hours:?}"
|
||||
}
|
||||
|
||||
function generate_random_string() {
|
||||
length="${1:-10}"
|
||||
echo $(cat /dev/urandom | tr -dc 'a-zA-Z09' | head -c "$length")
|
||||
}
|
||||
|
||||
function checksumm() {
|
||||
echo $(sha256sum "${1:?}" | awk '{print $1}')
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue