2017-09-28 26 views
1

私はPythonとicalendarには非常に新しいですが、私はAppleのIcloudウェブサイトから自分のicalendarを取得し、カレンダー情報にアクセスできるようにしてアプリに表示できます。私は再発しないカレンダーから任意のイベントを取得することができますが、それはそれらのイベントを取得し、何らかの理由で再発するものをスキップします(これは基本的に私が現在スケジュールしているイベントの唯一のタイプです)。私が現在持っている、どのように再発するイベントを得るためのアイデア?Pythonでイベントを再解析するための構文解析

from icalendar import Calendar, Event 
import urllib.request 

def getCalendar(): 
    urlHome = urllib.request.urlopen('https://p10-calendarws.icloud.com/ca/subscribe/1/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX').read() 

    return urlHome 

def displayCalendar(calendar): 

    showCalHome = Calendar.from_ical(calHome) 
    for event in showCalHome.walk('VEVENT'): 
     date = event.get('dtstart').dt 
     summary = event.get('summary') 
     print(summary,date) 


calendar = getCalendar() 
displayCalendar(calendar) 

私は、単一のイベントのリストを取得するだけでなく、何ができるか上の任意の考えやアイデアを、だけでなく、再発のイベント?

+0

を私が抱えている問題は、繰り返される過去の出来事としてではなく、解決策を見つけるのを助けるかもしれないとして、起こっているイベントとして再発するイベントを認識しないということです。 – jgummersall

答えて

0

は、ICSファイル自体によって解析した文字列に値の多くを回した後、私が探していたものに、文字列を比較することによって、それを考え出し、このようなものだった:私は、メインのいずれかを推測

import json 
import requests 
from icalendar import Calendar, Event 
import urllib.request 
from datetime import * 
import datetime 
from dateutil.rrule import * 


def getCalendar(): 
    urlWork = urllib.request.urlopen('https://p10-calendarws.icloud.com/ca/subscribe/1/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX').read() 
    urlHome = urllib.request.urlopen('https://p10-calendarws.icloud.com/ca/subscribe/1/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX').read() 

    calendars = [urlHome, urlWork] 

    return calendars 

def getTodaysEvents(calendars): 

    calHome = calendars[0] 
    calWork = calendars[1] 

    allEvents = [] 
    singleEvents = {} 
    homeEvents = {} 
    workEvents = {} 

    today = str(date.today()) 
    year = today[0:4] 
    month = today[5:7] 
    day = today[8:10] 
    currentDay = year + month + day 

    showCalHome = Calendar.from_ical(calHome) 
    for component in showCalHome.walk(): 
     if component.name == "VEVENT": 
      rule = component.get('rrule') 
      eventDay = str(component.get('dtstart').dt) 
      if rule is not None: 
       rrule = dict(rule) 
       if 'UNTIL' in rrule.keys(): 
        eventEnd = str(rrule['UNTIL'][0]) 
        eventEndYear = eventEnd[0:4] 
        eventEndMonth = eventEnd[5:7] 
        eventEndDay = eventEnd[8:10] 
        endEvent = eventEndYear + eventEndMonth + eventEndDay 
        if int(endEvent) >= int(currentDay): 
         print(component.get('summary')) 
         homeEvents['CALENDAR'] = "HOME" 
         homeEvents['SUMMARY'] = (component.get('summary')) 
         homeEvents['LOCATION'] = (component.get('location')) 
         homeEvents['START'] = (component.get('dtstart').dt) 
         homeEvents['END'] = (component.get('dtend').dt) 
         allEvents.append(homeEvents) 
       # else: (NEED TO IMPLEMENT WEEKLY EVENTS THAT DON'T END and Events that aren't Reoccuring) 
       # print(component.get('summary')) 
       # if component.get('location') != 'None': 
       #  print(component.get('location')) 
       # print(component.get('dtstart').dt) 
       # print(component.get('dtend').dt) 
      else: 
       if str(date.today()) == eventDay[0:10]: 
        print(component.get('summary')) 
        singleEvents['CALENDAR'] = "HOME" 
        singleEvents['SUMMARY'] = (component.get('summary')) 
        singleEvents['LOCATION'] = (component.get('location')) 
        singleEvents['START'] = (component.get('dtstart').dt) 
        singleEvents['END'] = (component.get('dtend').dt) 
        allEvents.append(singleEvents) 
      print(allEvents) 


    showCalWork = Calendar.from_ical(calWork) 
    for component in showCalWork.walk(): 
     if component.name == "VEVENT": 
      rule = component.get('rrule') 
      if rule is not None: 
       rrule = dict(rule) 
       today = str(date.today()) 
       year = today[0:4] 
       month = today[5:7] 
       day = today[8:10] 
       currentDay = year + month + day 
       if 'UNTIL' in rrule.keys(): 
        eventEnd = str(rrule['UNTIL'][0]) 
        eventEndYear = eventEnd[0:4] 
        eventEndMonth = eventEnd[5:7] 
        eventEndDay = eventEnd[8:10] 
        endEvent = eventEndYear + eventEndMonth + eventEndDay 
        if int(endEvent) >= int(currentDay): 
         workEvents['CALENDAR'] = "WORK" 
         workEvents['SUMMARY'] = (component.get('summary')) 
         workEvents['LOCATION'] = (component.get('location')) 
         workEvents['START'] = (component.get('dtstart').dt) 
         workEvents['END'] = (component.get('dtend').dt) 
         allEvents.append(workEvents) 
       # else: 
       # print(component.get('summary')) 
       # if component.get('location') != 'None': 
       #  print(component.get('location')) 
       # print(component.get('dtstart').dt) 
       # print(component.get('dtend').dt) 

    return allEvents 


def displayEvents(events): 
    print(events) 
    print() 
    print("TODAY:") 
    for event in range(len(events)): 
     start = str(events[event]['START'])[11:16] 
     end = str(events[event]["END"])[11:16] 
     if int(start[0:2]) < 12: 
      sT = "AM" 
     else: 
      pmtime = int(start[0:2]) - 12 
      start[0:2].replace(start[0:2], str(pmtime)) 
      sT = "PM" 
     if int(end[0:2]) < 12: 
      eT = "AM" 
     else: 
      pmtime = int(end[0:2]) - 12 
      end = str(pmtime) + end[2:5] 
      eT = "PM" 

     print(events[event]['SUMMARY'] + " - " + events[event]["CALENDAR"]) 
     if str(events[event]['LOCATION']) != "None": 
      print(events[event]['LOCATION']) 
     if start[0] == "0" and end[0] == "0": 
      print(start[1:5] + sT + " - " + end[1:5] + eT) 
     elif start[0] == "0": 
      print(start[1:5] + sT + " - " + end + eT) 
     elif end[0] == "0": 
      print(start + sT + " - " + end[1:5] + eT) 
     else: 
      print(start + sT + " - " + end + eT) 

calendars = getCalendar() 
events = getTodaysEvents(calendars) 
displayEvents(events)