创建或使用ICS文件的修改只显示最后一小时活动 [英] Display only the last hour event created or modified with an ics file

查看:252
本文介绍了创建或使用ICS文件的修改只显示最后一小时活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个previous问题,新的问题和答案查尔斯达菲。我需要搜索的.ics文件并显示在IRC上每隔一小时,如果有是创建或修改一个新的事件。

老问题:解析ICS使用bash文件

@查尔斯 - 达菲的响应:

 #!/斌/庆典handle_event(){
  :#把你的意图逻辑的定义在这里
}声明-A含量=()#定义一个关联数组(又名地图,又名散)
声明-A TZID =()#时区的信息另一个关联数组而IFS =:读-r键值;做
  值= $ {值%$'\\ r'}#删除DOS新行
  如果[[$关键=&END功放;&安培; $值= VEVENT]];然后
    handle_event#定义这个功能是给你;见下文的建议
    内容=()
    TZID =()
  其他
    如果[[$关键= *; TZID =*];然后
      TZID [$键%;*] = $ {键## *; TZID =}
    科幻
    内容[$ {键%;*}] = $价值
  科幻
DONE

...其中 handle_event 是,做你所关心的实际工作的功能。例如,这可能是这样的:

  LOCAL_DATE(){
  当地TZ = $ {TZID [$ 1]}
  当地DT = $ {内容[$ 1]}
  如果[[$ DT = * Z]];然后
    TZ = UTC
    DT = $ {DT%Z}
  科幻  #注意,这需要GNU日期
  日期--date =TZ = \\$ TZ \\$ {DT:0:4} - $ {DT:4:2} - $ {DT:6:2} T $ {DT:9:2}: $ {dt的:11:2}
}handle_event(){
  如果[[$ {内容[最后修改]}=$ {内容[创建]}]];然后
    回声新事件创建
  其他
    回声修改事件
  科幻
  printf的'%S \\ T'$(LOCAL_DATE DTSTART)$ {内容[摘要]}$ {内容[位置]};回声
}

通过您给定的输入文件和上面的脚本,的bash解析,集成电路及LT; test.ics 产生下面的输出(我目前的区域设置,时区和语言):

 新事件创建
太阳6月12日15:10:00 CDT 2016年阿什VS尸变赛松1第9和; 10 OCS乔科
修改事件
周六6月11日十五时35分00秒CDT 2016劳拉赛松2第1集4 RTS联合国的奥秘(瑞士)


解决方案

最简单的事情是提取当前的日期,提取本地时间日期,并进行比较。

  LOCAL_DATE(){
  当地TZ = $ {TZID [$ 1]}
  当地DT = $ {内容[$ 1]}
  如果[[$ DT = * Z]];然后
    TZ = UTC
    DT = $ {DT%Z}
  科幻
  转移##< - 从参数列表中删除$ 1,所以$ @是所有附加参数  如果[[$ DT = * T *];然后
    DT =$ {DT:0:4} - $ {DT:4:2} - $ {DT:6:2} T $ {DT:9:2} $ {DT:11:2}
  其他
    dt的=$ {dt的:0:4} - $ {dt的:4:2} - $ {dt的:6:2}
  科幻  #注意,这需要GNU日期
  日期--date =TZ = \\$ TZ \\$ DT$ @
}

...然后:

  handle_event(){  ##返回,如果今天的日期是不
  如果[$(日期+%Y%M%D)=$(LOCAL_DATE DTSTART +%Y%M%D)]!]然后
    返回
  科幻  ##,否则,发出正常的内容作为输出
  如果[[$ {内容[最后修改]}=$ {内容[创建]}]];然后
    回声新事件创建
  其他
    回声修改事件
  科幻
  printf的'%S \\ T'$(LOCAL_DATE DTSTART)$ {内容[摘要]}$ {内容[位置]};回声
}


这工作,因为我们加入$ @日期,所以额外的参数参数列表如格式字符串,只有日期和时间不元素可以通过。

然后,通过比较 $(日期+%Y%M%D) - 今天的日期 - 和 $(LOCAL_DATE DTSTART +% Y%M%D) - 从文件解析的日期 - 我们能确定日期,但没有时间,比赛


最终输出的:

 修改事件
周三5月18日十三时55分00秒CDT 2016年世界街头赛松2集13安培; 14 TMC(法国)
修改事件
周三5月18日11:55:00 CDT 2016年的pretender赛松1集17安培; 18(VF)之三
新事件创建
周三5月18日十三时55分00秒CDT 2016年现存赛松2第7集9条之三
新事件创建
周三5月18日13:15:0​​0 CDT 2016年的Une赛松金赛松动物园5集31(VF)法国4 HD
修改事件
周三5月18日15:30:00 CDT 2016年少年狼赛松5第19集MTV

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)

解决方案

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

这篇关于创建或使用ICS文件的修改只显示最后一小时活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆