-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
186 lines (149 loc) · 5.03 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from caldavclient import CaldavClient
import json
import time
from caldavclient import util
import time
from slackclient import SlackClient
with open('key.json') as json_data:
d = json.load(json_data)
userId = d['naver']['id']
userPw = d['naver']['pw']
bot_token = d['bot_token']
slackClient = SlackClient(bot_token)
#hostname = "https://caldav.icloud.com"
hostname = "https://caldav.calendar.naver.com/principals/users/jspiner"
## 기본 client 생성
client = CaldavClient(
hostname,
userId,
userPw
)
principal = client.getPrincipal()
homeset = principal.getHomeSet()
calendars = homeset.getCalendars()
for calendar in calendars:
print(calendar.calendarName + " " + calendar.calendarId + " " + calendar.cTag)
##최초 동기화
eventList = calendars[0].getAllEvent()
for event in eventList:
print (event.eventId + " " + event.eTag)
## 아래 정보를 db에 저장
"""
ACCOUNT
host_name -> hostname
user_id -> userId
user_pw -> 추후 BASE64로 처리
home_set_cal_url -> principal.getHomeSet().homesetUrl
CALENDAR
host_name -> hostname
user_id -> userId
calendar_id -> homeset.getCalendars()[n].calendarId (선택된 캘린더들 전부다)
calendar_url -> homeset.getCalendars()[n].calendarUrl
calendar_name -> homeset.getCalendars()[n].calendarName
cTag -> homeset.getCalendars()[n].cTag
EVENT
host_name -> hostname
user_id -> userId
calendar_id -> homeset.getCalendars()[n].calendarId (선택된 캘린더들 전부다)
eventId -> calendar.getAllEVent()[i].eventId
eventUrl -> calendar.getAllEVent()[i].eventUrl
eTag -> calendar.getAllEVent()[i].eTag
"""
## 주기적 동기화
"""
#client 객체에 db에서 데이터를 불러와 넣어줌
client = (
CaldavClient(
hostname,
userId,
userPw
).setPrincipal("principal_url") #db 에서 로드
.setHomeSet("home_set_cal_url") #db 에서 로드
.setCalendars("calendarList") #db에서 로드해서 list calendar object 로 삽입
)
"""
"""
list calendar object 만드는법
calendarList = []
for i in db.inter()
calendar = Calendar(
calendarUrl = calendarUrl,
calendarName = calendarName,
cTag = cTag
)
calendarList.append(calendar)
https://caldav.icloud.com:443/8171895891/principal/
https://p51-caldav.icloud.com:443/8171895891/calendars/
/8171895891/calendars/home/
"""
"""test code """
calendarList=[]
calendarList.append(calendars[0])
"""
CaldavClient.Calendar(
calendarUrl = "/caldav/jspiner/calendar/25443380/",
calendarName = "내 캘린더",
cTag = "2017-01-24 21:15:19"
)
"""
client = (
CaldavClient(
hostname,
userId,
userPw
).setPrincipal("https://caldav.calendar.naver.com:443/principals/users/jspiner") #db 에서 로드
.setHomeSet("https://caldav.calendar.naver.com:443/principals/users/jspiner/") #db 에서 로드
.setCalendars(calendarList) #db에서 로드해서 list calendar object 로 삽입
)
calendars = client.getPrincipal().getHomeSet().getCalendars()
def sendMessage(text):
slackClient.api_call(
"chat.postMessage",
channel="#testbed",
text=text,
username='hotsan',
icon_emoji=':robot_face:'
)
## 주기적으로 돌면서 diff 체크
while True:
print("start sync")
##동기화할 캘린더 선택
calendarToSync = calendars[0]
if calendarToSync.isChanged():
print("something changed")
oldEventList = calendarToSync.getAllEvent() #db에서 이전 event리스트들을 불러옴
newEventList = calendarToSync.updateAllEvent()
eventDiff = util.diffEvent(oldEventList, newEventList)
text = ""
if len(eventDiff.added()) != 0:
sendMessage("일정이 추가되었다.")
if len(eventDiff.removed()) !=0:
sendMessage("일정이 삭제되었다.")
if len(eventDiff.changed()) !=0:
sendMessage("일정이 변경되었다.")
print("add : " + str(eventDiff.added()))
print("removed : " + str(eventDiff.removed()))
print("changed : " + str(eventDiff.changed()))
print("unchanged : " + str(eventDiff.unchanged()))
else:
print("nothing changed")
time.sleep(10)
"""
calendarList=[]
calendarList.append(
CaldavClient.Calendar(
calendarUrl = "/caldav/jspiner/calendar/25443380/",
calendarName = "내 캘린더",
cTag = "2017-01-24 21:15:19"
)
)
client = (
CaldavClient(
hostname,
userId,
userPw
).setPrincipal("https://caldav.calendar.naver.com:443/principals/users/jspiner") #db 에서 로드
.setHomeSet("https://caldav.calendar.naver.com:443/principals/users/jspiner/") #db 에서 로드
.setCalendars(calendarList) #db에서 로드해서 list calendar object 로 삽입
)
"""