Iteration 1

I want another script. I want you to check whether the provided file has a markdown header of recent 30 minutes in the end of the file. I’m okay with processing only the latest header in the file. The header starts with # symbol and a space followed by it.

If it’s 08:02 now, the header of # 07:32 or later is 30 minutes ago. After the header, there is an empty line and some text.

If the text after the latest (30 minutes recent) header is ERROR: Cannot fetch weather data, then remove the text and the header before it, and place the text with the current time in %HH:%MM format as the header, e.g. # 08:05. Then place the empty line and the word Weather after it.

If it is a different text after the latest (30 minutes recent) header, then do nothing and exit the script.

If the latest header was longer than 30 minutes ago or there’s no header with the time at all, then create the markdown header with the current time, empty line after it, and then write the word Weather.


Iteration 2

The script doesn’t work as I want it to. Simplify the logic.

Find the latest line, if it starts with the text ERROR: then remove the last 5 lines. Then do it again, until there’s no ERROR: line.

If the latest line isn’t ERROR: , then find the latest line that starts with the # .

Check whether it’s 30 minutes recent.

  • If it is, do nothing, exit the script.
  • If it’s older than 30 minutes ago, then place a new header with the current time, e.g. # 08:30, and an empty line, and the text Weather after it.

Combination of two scripts.

Great, it works as I want it to.

Now, I want you to combine it with another script that you helped me with a couple of days ago. The Weather script.

Instead of writing Weather, I want you to perform ansiweather command, like in this script I’m pasting. This time there’s no file provided to the script, its path is specified in the script itself.

The script I want you to add to this logic is:

#!/bin/bash

# Define the directory and file paths
DIRECTORY="$HOME/.notes/$(date +%Y)/$(date +"%m-%d-%a")"

mkdir -p "$DIRECTORY"
FILE="$DIRECTORY/Weather.md"
TEMP_FILE="${XDG_RUNTIME_DIR:-/tmp}/ansiweather_output"

# Fetch the weather information and save it to a temporary file
ansiweather > "$TEMP_FILE"

# Get the current date in the desired format
CURRENT_DATE=$(date +"# %a, %b %d, %Y")
CURRENT_DATETIME=$(date +"%Y-%m-%dT%H:%M:%S%z")

# Define the front matter
FRONT_MATTER="+++
title = 'Weather'
date = \"$CURRENT_DATETIME\"
draft = false
tags = ['weather', 'automatic']
+++"

# Extract sunrise and sunset information from the temporary file and format them as a list
SUNRISE=$(grep 'Sunrise' "$TEMP_FILE" | sed 's/^/- /')
SUNSET=$(grep 'Sunset' "$TEMP_FILE" | sed 's/^/- /')

# Remove Sunrise and Sunset from the main weather information
WEATHER_INFO=$(grep -v 'Sunrise\|Sunset' "$TEMP_FILE")

# Check if the file exists
if [ ! -f "$FILE" ]; then
    # File doesn't exist, create it with the front matter, current date, sunrise, and sunset information
    echo -e "$FRONT_MATTER\n\n$CURRENT_DATE\n\n$SUNRISE\n$SUNSET\n" > "$FILE"
fi

# Get the current time
CURRENT_TIME=$(date +"%H:%M")

# Append the current time as a markdown header and the weather information to the file
echo -e "\n\n## $CURRENT_TIME\n$WEATHER_INFO" >> "$FILE"

# Clean up the temporary file
rm "$TEMP_FILE"

Attempt 4

This time, the script doesn’t work as I want it to.

It inserted the info like the file do not exist. Let me outline the logic for you, once again.

  1. Check whether the file exists. If there’s no file, create the file and insert front matter into it. Then insert weather info, like it’s in the script I’ve provided: put the date and distill Sunrise and Sunset info.

Correct example:

+++
title = 'Weather'
date = "2024-07-27T00:00:29+0300"
draft = false
tags = ['weather', 'automatic']
+++

# Sat, Jul 27, 2024

- Sunrise: 05:45   
- Sunset: 21:14


## 00:00
 
Weather in Lviv: 16 °C ☁   
Feels like: 16 °C   UVI: 6.14   
Wind: 0.31 m/s WNW   
Humidity: 84%   
Pressure: 1016 hPa   

If it exists but the 1st line is not the front matter +++, then remove the file or rewrite the file, the behaviour is the same for when there’s no file.

  1. If file exists and starts with front matter (first line is +++) then find the latest line. If it starts with the text ERROR: then remove the last 5 lines. Then do it again, until the latest line is not ERROR: .

  2. If the latest line isn’t ERROR: , then find the latest line that starts with the # .

Check whether it’s 30 minutes recent.

  • If it is, do nothing, exit the script.
  • If it’s older than 30 minutes ago, then place a new header with the current time, e.g. # 08:30, and an empty line, and the weather after it. The weather should be without Sunrise and Sunset lines, as we specified those lines at the beginning of the file already. As they don’t change during the day, I don’t want duplicates.

That’s it, try this logic.