Display only the last hour event created or modifi

2019-09-11 02:45发布

I have a new question about this previous question and answer to Charles Duffy. I need to search the .ics file and display every hour on IRC if there is a new event that is created or modified.

The old question: Parsing ICS file with bash

The response of @charles-duffy :

#!/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

  # note that this requires GNU date
  date --date="TZ=\"$tz\" ${dt:0:4}-${dt:4:2}-${dt:6:2}T${dt:9:2}:${dt:11:2}"
}

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)

1条回答
干净又极端
2楼-- · 2019-09-11 02:50

The simple thing to do is to extract the current date, extract the date in localtime, and compare.

local_date() {
  local tz=${tzid[$1]}
  local dt=${content[$1]}
  if [[ $dt = *Z ]]; then
    tz=UTC
    dt=${dt%Z}
  fi
  shift ## <- remove $1 from the argument list, so "$@" is all extra arguments

  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" "$@"
}

...and then:

handle_event() {

  ## return if date is not today
  if [[ "$(date +%Y%m%d)" != "$(local_date DTSTART +%Y%m%d)" ]]; then
    return
  fi

  ## otherwise, emit normal content as output
  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
}

This works because we're adding "$@" to the argument list for date, so extra arguments such as a format string with only date and not time elements can be passed through.

Then, by comparing $(date +%Y%m%d) -- today's date -- and $(local_date DTSTART +%Y%m%d) -- the date parsed from the file -- we can determine if the dates, but not the times, match.


Final output:

Modified Event
Wed May 18 13:55:00 CDT 2016    Gotham Saison 2 Episode 13 & 14 TMC (France)
Modified Event
Wed May 18 11:55:00 CDT 2016    The Pretender Saison 1 Episode 17 & 18 (VF)     6ter
New Event Created
Wed May 18 13:55:00 CDT 2016    Extant Saison 2 Episode 7 à 9   6ter
New Event Created
Wed May 18 13:15:00 CDT 2016    Une saison au zoo Saison 5 Episode 31 (VF)      France 4 HD
Modified Event
Wed May 18 15:30:00 CDT 2016    Teen Wolf Saison 5 Episode 19   MTV
查看更多
登录 后发表回答