Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing ICS file with bash

This is a google calendar ics file.

I download it each time to check whether new play events have been added or changed and I appear on IRC.

I need convert a file like this :

BEGIN:VEVENT
DTSTART:20160612T201000Z
DTEND:20160612T211000Z
DTSTAMP:20160519T200239Z
UID:[email protected]
CREATED:20160518T153226Z
DESCRIPTION:
LAST-MODIFIED:20160518T153226Z
LOCATION:OCS Choc
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Ash vs Evil Dead Saison 1 Episode 9 & 10
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART;TZID=Europe/Brussels:20160611T203500
DTEND;TZID=Europe/Brussels:20160611T233500
DTSTAMP:20160519T202440Z
UID:[email protected]
RECURRENCE-ID;TZID=Europe/Brussels:20160611T203500
CREATED:20160503T144152Z
DESCRIPTION:
LAST-MODIFIED:20160518T123213Z
LOCATION:RTS Un (Suisse)
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:The Mysteries Of Laura Saison 2 Episode 1 à 4
TRANSP:TRANSPARENT
END:VEVENT

to

New Events Created :
dim. juin 12  20:10  Ash vs Evil Dead Saison 1 Episode 9 & 10 - OCS Choc

Last Modified Event :
sam. juin 11  20:35  The Mysteries Of Laura Saison 2 Episode 1 à 4 - RTS Un (Suisse)

I need convert with a bash script. I have to get :

DTSTART CREATED LAST-MODIFIED LOCATION SUMMARY

And i need compare CREATED and LAST-MODIFIED

pseudo-code :

if (created = LastModified)
then 
     echo createdevent
else
     echo lastModifiedEvent
fi
like image 229
Saber Pendragon Avatar asked Dec 24 '22 05:12

Saber Pendragon


1 Answers

A native bash implementation (for shell version 4.0 or newer -- older versions lack associative arrays) would look something like the following:

#!/bin/bash

handle_event() {
  : # put a definition of your intended logic here
}

declare -A content=( ) # define an associative array (aka map, aka hash)
declare -A tzid=( )    # another associative array for timezone info

while IFS=: read -r key value; do
  value=${value%$'\r'} # remove DOS newlines
  if [[ $key = END && $value = VEVENT ]]; then
    handle_event # defining this function is up to you; see suggestion below
    content=( )
    tzid=( )
  else
    if [[ $key = *";TZID="* ]]; then
      tzid[${key%%";"*}]=${key##*";TZID="}
    fi
    content[${key%%";"*}]=$value
  fi
done

...where handle_event is a function that does the actual work you care about. For instance, that might look like this:

local_date() {
  local tz=${tzid[$1]}
  local dt=${content[$1]}
  if [[ $dt = *Z ]]; then
    tz=UTC
    dt=${dt%Z}
  fi
  shift

  if [[ $dt = *T* ]]; then
    dt="${dt:0:4}-${dt:4:2}-${dt:6:2}T${dt:9:2}:${dt:11:2}"
  else
    dt="${dt:0:4}-${dt:4:2}-${dt:6:2}"
  fi

  # note that this requires GNU date
  date --date="TZ=\"$tz\" $dt" "$@"
}

handle_event() {
  if [[ "${content[LAST-MODIFIED]}" = "${content[CREATED]}" ]]; then
    echo "New Event Created"
  else
    echo "Modified Event"
  fi
  printf '%s\t' "$(local_date DTSTART)" "${content[SUMMARY]}" "${content[LOCATION]}"; echo
}

With your given input file and the above script, bash parse-ics <test.ics yields the following output (with my current locale, timezone and language):

New Event Created
Sun Jun 12 15:10:00 CDT 2016    Ash vs Evil Dead Saison 1 Episode 9 & 10        OCS Choc
Modified Event
Sat Jun 11 15:35:00 CDT 2016    The Mysteries Of Laura Saison 2 Episode 1 à 4   RTS Un (Suisse)
like image 105
Charles Duffy Avatar answered Dec 29 '22 12:12

Charles Duffy