Testing ChatGPT on coding - a script to make CSV to ICS

 So I gave ChatGPT a task of writing a CSV to ICS convertor, to take a spreadsheet of events and turn it into individual calendar file invitations. The idea is you can make a calendar with lots of event items in a spreadsheet, and then output ICS that you can just double-click to add to your calendar automatically.

So here's the spreadsheet:

"Event Name","Event Start Date","Event End Date","Event Start Time","Event End Time","event description or notes","URL applicable"

"Meeting with John","2024-06-10","2024-06-10","09:00","10:00","Discuss project updates.","http://example.com"

And here's the script ChatGPT made. It seems to get confused with things like Daylight Savings and/or Timezone but you can fix that manually. It took some debugging and I had to copy/paste my Mac's manpage for DATE into ChatGPT, but it got it eventually.

The only part of this that I wrote was the check for a filename.


#!/bin/bash


# Check if a filename is provided as the first parameter

filename=$1

if [ -z "$filename" ] ; then

    echo "Give a csv file to read in the following order please:"

    echo "Event Name, Event Start Date, Event End Date, Event Start Time, Event End Time, event description or notes, and URL applicable"

    exit 1

fi


# Function to convert date and time to the required format for .ics

convert_to_ics_datetime() {

    local date_part="$1"

    local time_part="$2"

    local datetime_str="$date_part $time_part"


    if [[ "$OSTYPE" == "darwin"* ]]; then

        # For macOS, set the timezone explicitly to UTC

        local result=$(TZ=UTC date -ju -f "%Y-%m-%d %H:%M:%S" "$datetime_str" +"%Y%m%dT%H%M%SZ")

        echo "$result"

    else

        # For Linux, set the timezone explicitly to UTC

        local result=$(TZ=UTC date -d "$datetime_str" +"%Y%m%dT%H%M%SZ")

        echo "$result"

    fi

}


# Read the CSV file line by line, skipping the header

line_number=1

tail -n +2 "$filename" | while IFS=, read -r event_name start_date end_date start_time end_time description url; do

    # Increment line number

    ((line_number++))


    # Skip processing if any of the required fields are empty

    if [[ -z "$event_name" || -z "$start_date" || -z "$end_date" || -z "$start_time" || -z "$end_time" ]]; then

        echo "Ignoring blank line at line $line_number"

        continue

    fi


    # Remove quotes from the fields

    event_name=$(echo $event_name | tr -d '"')

    start_date=$(echo $start_date | tr -d '"')

    end_date=$(echo $end_date | tr -d '"')

    start_time=$(echo $start_time | tr -d '"')

    end_time=$(echo $end_time | tr -d '"')

    description=$(echo $description | tr -d '"')

    url=$(echo $url | tr -d '"')


    # Ensure the time fields have seconds if not empty

    if [[ -n "$start_time" ]]; then

        start_time="${start_time}:00"

    fi


    if [[ -n "$end_time" ]]; then

        end_time="${end_time}:00"

    fi


    # Debug: print verbose output

    echo "Processing line $line_number: $event_name"


    # Generate UID and timestamps

    uid=$(uuidgen)

    dtstamp=$(date -u +"%Y%m%dT%H%M%SZ")

    dtstart=$(convert_to_ics_datetime "$start_date" "$start_time")

    dtend=$(convert_to_ics_datetime "$end_date" "$end_time")


    # Create .ics file for the event

    ics_filename="${event_name// /_}.ics"

    cat <<EOF > "$ics_filename"

BEGIN:VCALENDAR

VERSION:2.0

PRODID:-//Your Organization//Your Product//EN

BEGIN:VEVENT

UID:$uid

DTSTAMP:$dtstamp

DTSTART:$dtstart

DTEND:$dtend

SUMMARY:$event_name

DESCRIPTION:$description

URL:$url

END:VEVENT

END:VCALENDAR

EOF


    echo "Created $ics_filename"


done


Popular posts from this blog

Some rough notes on FreedomGPT and PrivateGPT

What is a "token" , what is "temperature", and what is "n-shot"?