Prompt

Another script. I want you to process all Weather.md files in the specified directory and check if they have front matter (the first line is +++). If they have front matter, then do nothing, go to the next file.

If the file has no front matter, insert it into the beginning of the file:

+++
title = 'Weather'
date = "2023-05-14T00:00:00+0300"
draft = false
tags = ['weather', 'automatic']
+++

The date and time you can take from the file’s metadata. (Get the earliest date-time.)

I want you to take Sunrise and Sunset info (two latest lines in a file without front matter), and re-format the file as you formatted other files.

Example of the input file:


Weather in Lviv: 8 °C ☁   
Feels like: 6 °C   UVI: 5.48   
Wind: 3.64 m/s NNE   
Humidity: 83%   
Pressure: 1025 hPa   
Sunrise: May 04 05:56:17 AM   
Sunset: May 04 08:45:04 PM

Example of the output file:

+++
title = 'Weather'
date = "2023-05-04T11:48:00+0300"
draft = false
tags = ['weather', 'automatic']
+++
 
# Thu, May 04, 2023

- Sunrise: 05:56 
- Sunset: 20:45

Weather in Lviv: 13 °C ☁   
Feels like: 13 °C   UVI: 5.8   
Wind: 5.45 m/s ESE   
Humidity: 70%   
Pressure: 1017 hPa

You’re allowed to use temporary file, if needed. Also, I want to specify a directory as an argument for the script.

Reply

I apologize for the issues with the script. Let’s correct the date parsing and ensure that the Sunrise and Sunset times are correctly formatted. Here’s the updated script:

#!/bin/bash

# Check if a directory is provided as an argument
if [ -z "$1" ]; then
  echo "Usage: $0 <directory>"
  exit 1
fi

DIRECTORY="$1"

# Function to extract the earliest date-time from file metadata
get_earliest_datetime() {
  local file="$1"
  stat -c %y "$file" | awk '{print $1"T"$2}' | sed 's/\..*//'
}

# Function to re-format the weather file
reformat_weather_file() {
  local file="$1"
  local temp_file="${XDG_RUNTIME_DIR:-/tmp}/weather_temp"
  
  # Get the earliest date-time from the file's metadata
  local earliest_datetime=$(get_earliest_datetime "$file")
  
  # Get the Sunrise and Sunset information
  local sunrise=$(grep 'Sunrise' "$file" | sed 's/^Sunrise: //')
  local sunset=$(grep 'Sunset' "$file" | sed 's/^Sunset: //')
  
  # Convert sunrise and sunset times to HH:MM format
  local sunrise_hm=$(date -d "$sunrise" +"%H:%M" 2>/dev/null || echo "00:00")
  local sunset_hm=$(date -d "$sunset" +"%H:%M" 2>/dev/null || echo "00:00")
  
  # Create the front matter
  local front_matter="+++
title = 'Weather'
date = \"$earliest_datetime\"
draft = false
tags = ['weather', 'automatic']
+++"

  # Get the current date in the desired format
  local current_date=$(date +"# %a, %b %d, %Y" -d "$earliest_datetime")

  # Re-format the file content
  echo -e "$front_matter\n\n$current_date\n\n- Sunrise: $sunrise_hm\n- Sunset: $sunset_hm\n" > "$temp_file"
  grep -v 'Sunrise\|Sunset' "$file" >> "$temp_file"
  
  # Replace the original file with the re-formatted content
  mv "$temp_file" "$file"
}

# Process all Weather.md files in the specified directory
find "$DIRECTORY" -type f -name "Weather.md" | while read -r file; do
  # Check if the file has front matter (first line is +++)
  if ! head -n 1 "$file" | grep -q "^+++$"; then
    reformat_weather_file "$file"
  fi
done

Explanation of Changes:

  1. Date Parsing: Fixed the date parsing to remove the fractional seconds part which was causing the date: invalid date error.
  2. Sunrise and Sunset Time Handling: Used a fallback for sunrise and sunset times if the date command fails to parse them.

Usage:

  1. Save the script to a file, e.g., process_weather_files.sh.
  2. Make the script executable: chmod +x process_weather_files.sh.
  3. Run the script with the target directory as an argument: ./process_weather_files.sh /path/to/directory.

This should address the issues with date parsing and ensure that the Sunrise and Sunset times are correctly formatted.