我对python和icalendar非常陌生,但我试图从Apple的Icloud网站上获取我的icalendar,然后能够访问日历信息,以便我可以在应用程序上显示它。我可以从不再发生的日历中获取任何事件,但它只会获取那些事件并且由于某种原因跳过重复发生的事件(这基本上是我现在安排的唯一类型的事件)这是我目前拥有的代码,关于如何获得重复发生事件的任何想法?
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 :(得分: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)
答案 1 :(得分:0)
我创建了一个library,因为我正在寻找完全相同的用例。
对于您来说,recurring-ical-events
可以这样嵌入:
events = recurring_ical_events.of(calendar).between(start_date, end_date)
for event in events:
# ...