-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_to_calender.py
96 lines (75 loc) · 4.1 KB
/
add_to_calender.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
import pickle
from googleapiclient.discovery import build
from datetime import datetime, timedelta
import datefinder
import re
# timezone
time_zone = 'Europe/Dublin'
def create_event(start_time, end_time, summary, location, description):
credentials = pickle.load(open("token.pkl", "rb")) # get credentials to manipulate calendar
service = build("calendar", "v3", credentials=credentials)# v3 in reference https://developers.google.com/calendar/v3/reference
global time_zone
event = {
'summary': summary,
'location': location,
'description': description,
'start': {
'dateTime': start_time.strftime("%Y-%m-%dT%H:%M:%S"),
'timeZone': time_zone,
},
'end': {
'dateTime': end_time.strftime("%Y-%m-%dT%H:%M:%S"),
'timeZone': time_zone,
},
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'email', 'minutes': 24 * 60},
{'method': 'popup', 'minutes': 10},
],
},
}
print('Adding ' + summary + ' to calendar ...')
return service.events().insert(calendarId='primary', body=event).execute() # adds event to google calender
def add_events(chosen_events, event_df, society):
chosen_events = list(dict.fromkeys(chosen_events)) # removes duplicate values in user-input list
for event in chosen_events:
summary = event_df['event_title'].iloc[event-1]
start_time = event_df['event_time'].iloc[event-1]
location = event_df['event_loc'].iloc[event-1]
if society == "empty":
description = event_df['society'].iloc[event-1] + ' event.'
else:
description = society + ' event.'
# The string values in the ['event_time'] column must be manipulated slightly in order for datefinder.find_dates to accurately determine the start and end time
# This required a lot of testing and the following code fixes this
# *Note there are 5 different types of dates we could have collected from FB
# 1) Thursday, April 16, 2020 at 3:00 PM UTC+01
# 2) Friday, November 8, 2019 at 5:00 PM – 10:00 PM.
# 3) 5 Dates · Oct 31, 2019 - Nov 28, 2019 · UTC
# 4) Feb 16 at 11:30 AM – Feb 20 at 12:30 AM UTC
# 5) Saturday, February 9, 2019 at 7:00 PM – 2:30 AM UTC
filter_tuple = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
if '–' in start_time:
if start_time.startswith(filter_tuple) == True: # Formatting dates for type 4
start_time = start_time.replace('–', 'to')
matches = list(datefinder.find_dates(start_time))
else:
result = re.search(',(.*) at', start_time)
start_time = start_time.replace('–', 'to' + result.group(1) + ' at ')
matches = list(datefinder.find_dates(start_time)) # Formatting dates for type 2
end_time = matches[1]
if start_time.count('PM') == 1 and start_time.count('AM') == 1 and start_time.find('PM') < start_time.find('AM'): # Formatting dates for type 5 -> when a PM time and ends in an AM time
end_time = end_time + timedelta(hours = 24) #(in other words its the next day)
start_time = matches[0]
elif '-' in start_time:
start_time = start_time.replace('-', 'to') # Formatting dates for type 3
matches = list(datefinder.find_dates(start_time))
end_time = matches[1]
start_time = matches[0]
else:
matches = list(datefinder.find_dates(start_time)) # Formatting dates for type 1
start_time = matches[0].replace(tzinfo=None)
end_time = start_time + timedelta(hours = 1)
create_event(start_time, end_time, summary, location, description)
return 0