This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.groovy
executable file
·145 lines (126 loc) · 5.26 KB
/
create.groovy
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
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.DateTime;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.Events;
import com.google.api.services.calendar.Calendar.Events.Update;
import com.google.api.services.calendar.CalendarRequest;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.CalendarListEntry;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.common.collect.ImmutableSet;
public class CreateCalendarTask {
private static final Calendar _service = getCalendarService();
public static void main(String[] args) throws IOException, NoSuchProviderException,
MessagingException, GeneralSecurityException {
CalendarRequest<Event> calendarAction;
if (args.length < 1) {
//throw new RuntimeException("No task text specified");
System.err.print(".");
return;
}
calendarAction = createInsertTask(args[0]);
commit(calendarAction);
System.err.println("CreateCalendarTask.main() - Created event with title '" + args[0] + "'");
}
private static CalendarRequest<Event> createInsertTask(String title) throws IOException {
CalendarRequest<Event> updateTask;
Event event = new Event();
event.setSummary(title);
// 3 hours from now
event.setStart(new EventDateTime().setDateTime(new DateTime(System.currentTimeMillis() + 10800000)));
// 3.5 hours from now
event.setEnd(new EventDateTime().setDateTime(new DateTime(System.currentTimeMillis() + 10800000 + 1800000)));
updateTask = _service.events().insert("primary", event);
return updateTask;
}
private static void commit(final CalendarRequest<Event> update) throws NoSuchProviderException,
MessagingException, IOException {
// All persistent changes are done right at the end, so that any
// exceptions can get thrown first.
executeCalendarRequest(update);
}
private static void executeCalendarRequest(final CalendarRequest<Event> update) {
new Thread() {
@Override
public void run() {
Event updatedEvent;
try {
updatedEvent = update.execute();
// Print the updated date.
System.err.println(updatedEvent.getUpdated());
System.err.println(updatedEvent.getHtmlLink());
System.err.println("Calendar updated");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
private static Calendar getCalendarService() {
System.out.println("Authenticating...");
HttpTransport httpTransport;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Calendar client = new Calendar.Builder(httpTransport,
JacksonFactory.getDefaultInstance(), new AuthorizationCodeInstalledApp(
new GoogleAuthorizationCodeFlow.Builder(httpTransport,
JacksonFactory.getDefaultInstance(), GoogleClientSecrets.load(
JacksonFactory.getDefaultInstance(),
new InputStreamReader(CreateCalendarTask.class
.getResourceAsStream("/client_secrets.json"))),
ImmutableSet.of(CalendarScopes.CALENDAR,
CalendarScopes.CALENDAR_READONLY)).setDataStoreFactory(
new FileDataStoreFactory(new java.io.File(System
.getProperty("user.home"), ".store/calendar_sample")))
.build(), new LocalServerReceiver()).authorize("user"))
.setApplicationName("gcal-task-warrior").build();
return client;
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static Store connect() throws NoSuchProviderException, MessagingException {
Properties props = System.getProperties();
String password = System.getenv("GMAIL_PASSWORD");
if (password == null) {
throw new RuntimeException(
"Please specify your password by running export GMAIL_PASSWORD=mypassword groovy mail.groovy");
}
props.setProperty("mail.store.protocol", "imap");
Store theImapClient = Session.getInstance(props).getStore("imaps");
theImapClient.connect("imap.gmail.com", "sarnobat.hotmail@gmail.com", password);
return theImapClient;
}
}