use of com.google.api.services.calendar.Calendar in project Saber-Bot by notem.
the class SyncCommand method verify.
@Override
public String verify(String prefix, String[] args, MessageReceivedEvent event) {
String head = prefix + this.name();
int index = 0;
// check arg length
if (args.length < 1) {
return "That's not enough arguments!\n" + "Use ``" + head + " <channel> [<calendar address>]``";
}
if (args.length > 3) {
return "That's too many arguments!\n" + "Use ``" + head + " <channel> [<import|export> <calendar_address>]``";
}
// validate the supplied channel
String cId = args[0].replaceAll("[^\\d]", "");
if (!Main.getScheduleManager().isASchedule(cId)) {
return "Channel " + args[index] + " is not on my list of schedule channels for your guild.";
}
if (Main.getScheduleManager().isLocked(cId)) {
return "Schedule is locked while sorting or syncing. Please try again after I finish.";
}
// get user Google credentials (if they exist)
Credential credential = GoogleAuth.getCredential(event.getAuthor().getId());
if (credential == null)
return "I failed to connect to Google API Services!";
Calendar service = GoogleAuth.getCalendarService(credential);
// validate the calendar address
String address;
if (args.length == 2) {
index++;
address = args[index];
} else if (args.length == 3) {
index++;
switch(args[index].toLowerCase()) {
case "import":
case "export":
break;
default:
return "*" + args[index] + "* is invalid! Please use either *import* or *export*!";
}
index++;
address = args[index];
} else {
address = Main.getScheduleManager().getAddress(cId);
if (address.isEmpty()) {
return "Your channel, " + args[index] + ", is not setup with a Google Calendar address to sync with!";
}
}
if (!Main.getCalendarConverter().checkValidAddress(address, service)) {
return "Calendar address **" + address + "** is not valid!";
}
return "";
}
use of com.google.api.services.calendar.Calendar in project DisCal-Discord-Bot by NovaFox161.
the class EventEndpoint method updateEvent.
public static String updateEvent(Request request, Response response) {
JSONObject body = new JSONObject(request.body());
String eventId = body.getString("id");
GuildSettings settings;
if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
WebGuild g = (WebGuild) m.get("selected");
g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
settings = g.getSettings();
} else {
long guildId = body.getLong("guild_id");
settings = DatabaseManager.getManager().getSettings(guildId);
}
// Okay, time to update the event
try {
Calendar service;
if (settings.useExternalCalendar()) {
service = CalendarAuth.getCalendarService(settings);
} else {
service = CalendarAuth.getCalendarService();
}
CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
com.google.api.services.calendar.model.Calendar cal = service.calendars().get(calendarData.getCalendarId()).execute();
Event event = new Event();
event.setId(eventId);
event.setVisibility("public");
event.setSummary(body.getString("summary"));
event.setDescription(body.getString("description"));
EventDateTime start = new EventDateTime();
start.setDateTime(new DateTime(body.getLong("epochStart")));
event.setStart(start.setTimeZone(cal.getTimeZone()));
EventDateTime end = new EventDateTime();
end.setDateTime(new DateTime(body.getLong("epochEnd")));
event.setEnd(end.setTimeZone(cal.getTimeZone()));
if (!body.getString("color").equalsIgnoreCase("NONE")) {
event.setColorId(EventColor.fromNameOrHexOrID(body.getString("color")).getId().toString());
}
if (!body.getString("location").equalsIgnoreCase("") || !body.getString("location").equalsIgnoreCase("N/a")) {
event.setLocation(body.getString("location"));
}
JSONObject recur = body.getJSONObject("recurrence");
if (recur.getBoolean("recur")) {
// Handle recur
Recurrence recurrence = new Recurrence();
recurrence.setFrequency(EventFrequency.fromValue(recur.getString("frequency")));
recurrence.setCount(recur.getInt("count"));
recurrence.setInterval(recur.getInt("interval"));
String[] rr = new String[] { recurrence.toRRule() };
event.setRecurrence(Arrays.asList(rr));
}
EventData ed = new EventData(settings.getGuildID());
if (!body.getString("image").equalsIgnoreCase("") && ImageUtils.validate(body.getString("image"))) {
ed.setImageLink(body.getString("image"));
ed.setEventId(eventId);
ed.setEventEnd(event.getEnd().getDateTime().getValue());
}
if (ed.shouldBeSaved()) {
DatabaseManager.getManager().updateEventData(ed);
}
service.events().update(calendarData.getCalendarId(), eventId, event).execute();
response.status(200);
response.body(ResponseUtils.getJsonResponseMessage("Successfully updated event!"));
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB] Failed to update event!", e, EventEndpoint.class, true);
e.printStackTrace();
response.status(500);
response.body(ResponseUtils.getJsonResponseMessage("Failed to update event!"));
}
return response.body();
}
use of com.google.api.services.calendar.Calendar in project DisCal-Discord-Bot by NovaFox161.
the class EventEndpoint method createEvent.
public static String createEvent(Request request, Response response) {
JSONObject body = new JSONObject(request.body());
GuildSettings settings;
if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
WebGuild g = (WebGuild) m.get("selected");
g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
settings = g.getSettings();
} else {
long guildId = body.getLong("guild_id");
settings = DatabaseManager.getManager().getSettings(guildId);
}
// Okay, time to update the event
try {
Calendar service;
if (settings.useExternalCalendar()) {
service = CalendarAuth.getCalendarService(settings);
} else {
service = CalendarAuth.getCalendarService();
}
CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
com.google.api.services.calendar.model.Calendar cal = service.calendars().get(calendarData.getCalendarId()).execute();
Event event = new Event();
event.setId(KeyGenerator.generateEventId());
event.setVisibility("public");
event.setSummary(body.getString("summary"));
event.setDescription(body.getString("description"));
EventDateTime start = new EventDateTime();
start.setDateTime(new DateTime(body.getLong("epochStart")));
event.setStart(start.setTimeZone(cal.getTimeZone()));
EventDateTime end = new EventDateTime();
end.setDateTime(new DateTime(body.getLong("epochEnd")));
event.setEnd(end.setTimeZone(cal.getTimeZone()));
if (!body.getString("color").equalsIgnoreCase("NONE")) {
event.setColorId(EventColor.fromNameOrHexOrID(body.getString("color")).getId().toString());
}
if (!body.getString("location").equalsIgnoreCase("") || !body.getString("location").equalsIgnoreCase("N/a")) {
event.setLocation(body.getString("location"));
}
JSONObject recur = body.getJSONObject("recurrence");
if (recur.getBoolean("recur")) {
// Handle recur
Recurrence recurrence = new Recurrence();
recurrence.setFrequency(EventFrequency.fromValue(recur.getString("frequency")));
recurrence.setCount(recur.getInt("count"));
recurrence.setInterval(recur.getInt("interval"));
String[] rr = new String[] { recurrence.toRRule() };
event.setRecurrence(Arrays.asList(rr));
}
EventData ed = new EventData(settings.getGuildID());
if (!body.getString("image").equalsIgnoreCase("") && ImageUtils.validate(body.getString("image"))) {
ed.setImageLink(body.getString("image"));
ed.setEventEnd(event.getEnd().getDateTime().getValue());
}
if (ed.shouldBeSaved()) {
DatabaseManager.getManager().updateEventData(ed);
}
Event confirmed = service.events().insert(calendarData.getCalendarId(), event).execute();
response.status(200);
JSONObject respondBody = new JSONObject();
respondBody.put("Message", "Successfully create event!");
respondBody.put("id", confirmed.getId());
response.body(respondBody.toString());
} catch (Exception e) {
Logger.getLogger().exception(null, "[WEB] Failed to update event!", e, EventEndpoint.class, true);
e.printStackTrace();
response.status(500);
response.body(ResponseUtils.getJsonResponseMessage("Failed to update event!"));
}
return response.body();
}
use of com.google.api.services.calendar.Calendar in project DisCal-Discord-Bot by NovaFox161.
the class EventEndpoint method getEventsForSelectedDate.
public static String getEventsForSelectedDate(Request request, Response response) {
JSONObject requestBody = new JSONObject(request.body());
Long startEpoch = Long.valueOf(requestBody.getString("StartEpoch"));
Long endEpoch = startEpoch + 86400000L;
GuildSettings settings;
if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
WebGuild g = (WebGuild) m.get("selected");
g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
settings = g.getSettings();
} else {
long guildId = requestBody.getLong("guild_id");
settings = DatabaseManager.getManager().getSettings(guildId);
}
// okay, lets actually get the month's events.
try {
Calendar service;
if (settings.useExternalCalendar()) {
service = CalendarAuth.getCalendarService(settings);
} else {
service = CalendarAuth.getCalendarService();
}
CalendarData calendarData = DatabaseManager.getManager().getMainCalendar(settings.getGuildID());
Events events = service.events().list(calendarData.getCalendarAddress()).setTimeMin(new DateTime(startEpoch)).setTimeMax(new DateTime(endEpoch)).setOrderBy("startTime").setSingleEvents(true).setShowDeleted(false).execute();
List<Event> items = events.getItems();
String tz = "Error/Unknown";
try {
tz = service.calendars().get(calendarData.getCalendarAddress()).execute().getTimeZone();
} catch (Exception ignore) {
}
List<JSONObject> eventsJson = new ArrayList<>();
for (Event e : items) {
JSONObject jo = new JSONObject();
jo.put("id", e.getId());
jo.put("epochStart", e.getStart().getDateTime().getValue());
jo.put("epochEnd", e.getEnd().getDateTime().getValue());
jo.put("timezone", tz);
jo.put("summary", e.getSummary());
jo.put("description", e.getDescription());
if (e.getLocked() != null) {
jo.put("location", e.getLocation());
} else {
jo.put("location", "N/a");
}
jo.put("color", EventColor.fromNameOrHexOrID(e.getColorId()).name());
jo.put("isParent", !(e.getId().contains("_")));
if (e.getRecurrence() != null && e.getRecurrence().size() > 0) {
jo.put("recur", true);
Recurrence r = new Recurrence().fromRRule(e.getRecurrence().get(0));
JSONObject rjo = new JSONObject();
rjo.put("frequency", r.getFrequency().name());
rjo.put("count", r.getCount());
rjo.put("interval", r.getInterval());
jo.put("recurrence", rjo);
} else {
jo.put("recur", false);
JSONObject rjo = new JSONObject();
rjo.put("frequency", EventFrequency.DAILY.name());
rjo.put("count", -1);
rjo.put("interval", 1);
jo.put("recurrence", rjo);
}
EventData ed = DatabaseManager.getManager().getEventData(settings.getGuildID(), e.getId());
jo.put("image", ed.getImageLink());
eventsJson.add(jo);
}
JSONObject body = new JSONObject();
body.put("events", eventsJson);
body.put("count", eventsJson.size());
response.body(body.toString());
} catch (Exception e) {
response.body("Internal server error!");
Logger.getLogger().exception(null, "[WEB] Failed to retrieve events for specific date!", e, EventEndpoint.class, true);
return response.body();
}
return response.body();
}
use of com.google.api.services.calendar.Calendar in project muikku by otavanopisto.
the class GoogleCalendarClient method listPublicCalendars.
public List<fi.otavanopisto.muikku.calendar.Calendar> listPublicCalendars() throws CalendarServiceException {
try {
Calendar client = getClient();
ArrayList<fi.otavanopisto.muikku.calendar.Calendar> result = new ArrayList<>();
for (CalendarListEntry entry : client.calendarList().list().execute().getItems()) {
result.add(new GoogleCalendar(entry.getSummary(), entry.getDescription(), entry.getId(), isWritable(entry)));
}
return result;
} catch (IOException | GeneralSecurityException ex) {
throw new CalendarServiceException(ex);
}
}
Aggregations