-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle_calendar_fetcher.py
executable file
·249 lines (180 loc) · 7.07 KB
/
google_calendar_fetcher.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python3
''' Gets Google Calendar's account events and prints them to stdout '''
import getopt
from urllib.parse import urlencode
import httplib2
import datetime
from dateutil.relativedelta import *
import xml.etree.ElementTree as etree
from operator import itemgetter
__events__ = {}
def login(username, password):
''' Logins to Google Account '''
values = {'accountType': 'GOOGLE',
'Email': username,
'Passwd': password,
'source': 'google_calendar_fetcher',
'service': 'cl'}
url_login = 'https://www.google.com/accounts/ClientLogin'
response, content = httplib2.Http().request(url_login,
'POST',
urlencode(values),
headers={'Content-Type': 'application/x-www-form-urlencoded'})
if response.status == 403:
print('Bad username or password')
assert response.status == 200,\
"Can't log in (network error?)"
reply = content.decode().split('\n')
token = reply[2].split('=')[1]
return token
def get_calendars(token):
''' Gets calendars list '''
token_header = "GoogleLogin auth=" + str(token)
url_calendars = 'https://www.google.com/calendar'
url_calendars += '/feeds/default/allcalendars/full'
response, content = httplib2.Http().request(url_calendars,
headers={'Authorization': token_header})
assert response.status == 200,\
"Can't get calendars list (network error?)"
parse_calendars(content, token_header)
return content
def parse_calendars(xml_calendars, token_header):
''' Parses calendars list '''
tree = etree.XML(xml_calendars)
calendars = tree.findall('{http://www.w3.org/2005/Atom}entry')
for calendar in calendars:
calendar_content = calendar.find('{http://www.w3.org/2005/Atom}content')
calendar_id = calendar_content.get('src')
get_calendar_entries(calendar_id, token_header)
def get_calendar_entries(calendar_id, token_header):
''' Get entries (until one month) from calendar '''
now = datetime.date.today()
now_plus_month = now + relativedelta(months=+1)
values = {'start-min': now.strftime("%Y-%m-%d") + "T00:00:00",
'start-max': now_plus_month.strftime("%Y-%m-%d") + "T23:59:59"}
url = "%s?%s" % (calendar_id, urlencode(values))
response, content = httplib2.Http().request(url,
headers={'Authorization': token_header, 'cache-control': 'no-cache'})
assert response.status == 200,\
"Can't get entries from calendar (network error?)"
parse_events(content)
def parse_events(raw_xml):
''' Parses events '''
tree = etree.XML(raw_xml)
entries = tree.findall('{http://www.w3.org/2005/Atom}entry')
for entry in entries:
title = entry.find('{http://www.w3.org/2005/Atom}title')
when = entry.find('{http://schemas.google.com/g/2005}when')
if title.text is None:
__events__['No subject'] = when.get('startTime')
else:
__events__[title.text] = when.get('startTime')
def get_name_day():
''' Gets name day '''
url_name_day = 'http://svatky.adresa.info/txt'
response, content = httplib2.Http().request(url_name_day)
assert response.status == 200,\
"Can't get name day (network error?)"
if content.decode() == "":
return ""
temp_name_day = content.decode().split(";")
name_day = temp_name_day[1].split("\n")
return " - " + name_day[0]
def print_header():
''' Prints header to stdout '''
now = datetime.datetime.now()
output = ""
output = "Today is " + now.strftime("%d.%m.%Y") + ", "
if (int(now.strftime("%W"))) % 2 == 0:
output += "even week"
else:
output += "odd week"
output += get_name_day()
print(output)
def print_output():
''' Prints events to stdout '''
print_header()
output_line = ""
now = datetime.datetime.now()
events_sorted = sorted(__events__.items(), key=itemgetter(1))
# for key, value in events_sorted:
# print(key + " " + value)
for key, value in events_sorted:
if len(value) == 10:
event_start_time = datetime.datetime.strptime(value, "%Y-%m-%d")
time = False
else:
date_time = value.split('.')
event_start_time = datetime.datetime.strptime(date_time[0],
"%Y-%m-%dT%H:%M:%S")
time = True
delta = event_start_time - now
if (delta.seconds < 0):
continue
# print(key + " " + value + " " + str(delta.seconds) + " " + str(delta) + " " + str(delta.seconds // 3600))
if (time is True and (delta.days == 0 or delta.days == -1)):
if (delta.days >= 0):
diff = delta.seconds // 3600
if (now.day == event_start_time.day):
if (diff == 0):
output_line += "Now "
elif (diff == 1):
output_line += "In 1 hour "
else:
if (diff < 10):
output_line += "In " + str(diff) + " hours "
else:
output_line += "In " + str(diff) + " hours "
else:
output_line += "Tomorrow "
else:
continue
else:
if (delta.days < 0):
output_line += "Today "
elif (delta.days == 0):
output_line += "Tomorrow "
else:
if (delta.days > 8):
output_line += "In "
else:
output_line += "In "
output_line += str(delta.days + 1) + " days "
if (time is True):
output_line += event_start_time.strftime("%d.%m.%Y %H:%M ")
else:
if (delta.days <= -1):
output_line += now.strftime("%d.%m.%Y ")
else:
output_line += event_start_time.strftime("%d.%m.%Y ")
output_line += key
if (output_line != ""):
print(output_line)
output_line = ""
time = False
def main(username, password):
''' Entry point '''
token = login(username, password)
get_calendars(token)
print_output()
if __name__ == '__main__':
import sys
try:
opts, args = getopt.getopt(sys.argv[1:], "f", ["filename="])
except getopt.GetoptError as err:
print("Only argument is -f or --filename followed by name of file "
"that contains your Google username and password "
"on one line separated by space!")
sys.exit(2)
username = None
password = None
for option, argument in opts:
if option in ("-f", "--filename"):
try:
with open(argument, mode='r', encoding='utf-8') as credentials:
username, password = credentials.read().split()
except IOError:
print("File not found!")
sys.exit(2)
main(username, password)
sys.exit()