mirror of
https://github.com/DreamExposure/DisCal-Discord-Bot.git
synced 2026-05-06 17:21:07 -05:00
Add Google Calendar APIs and support.
This commit is contained in:
+1
-1
@@ -13,7 +13,6 @@
|
||||
.idea/dataSources.local.xml
|
||||
.idea/sqlDataSources.xml
|
||||
.idea/dynamic.xml
|
||||
.idea/uiDesigner.xml
|
||||
|
||||
# Gradle:
|
||||
.idea/gradle.xml
|
||||
@@ -59,3 +58,4 @@ hs_err_pid*
|
||||
/.idea/
|
||||
/src/test/
|
||||
/DisCal.iml
|
||||
/src/main/resources/client_secret.json
|
||||
|
||||
@@ -45,6 +45,16 @@
|
||||
<artifactId>google-api-client</artifactId>
|
||||
<version>1.22.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.apis</groupId>
|
||||
<artifactId>google-api-services-calendar</artifactId>
|
||||
<version>v3-rev225-1.22.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.oauth-client</groupId>
|
||||
<artifactId>google-oauth-client-jetty</artifactId>
|
||||
<version>1.22.0</version>
|
||||
</dependency>
|
||||
<!--MySQL Driver-->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.cloudcraftgaming;
|
||||
import com.cloudcraftgaming.database.DatabaseInfo;
|
||||
import com.cloudcraftgaming.database.MySQL;
|
||||
import com.cloudcraftgaming.eventlisteners.MessageListener;
|
||||
import com.cloudcraftgaming.eventlisteners.ReadyEventListener;
|
||||
import com.cloudcraftgaming.internal.calendar.CalendarAuth;
|
||||
import com.cloudcraftgaming.internal.consolecommand.ConsoleCommandExecutor;
|
||||
import com.cloudcraftgaming.internal.file.ReadFile;
|
||||
import sx.blah.discord.api.ClientBuilder;
|
||||
@@ -10,6 +12,7 @@ import sx.blah.discord.api.IDiscordClient;
|
||||
import sx.blah.discord.api.events.EventDispatcher;
|
||||
import sx.blah.discord.util.DiscordException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@@ -35,8 +38,16 @@ public class Main {
|
||||
MySQL mySQL = ReadFile.readDatabaseSettings(args[1]);
|
||||
connectToMySQL(mySQL);
|
||||
|
||||
//Connect to Google Calendar
|
||||
try {
|
||||
CalendarAuth.init(args);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//Register events
|
||||
EventDispatcher dispatcher = client.getDispatcher();
|
||||
dispatcher.registerListener(new ReadyEventListener());
|
||||
dispatcher.registerListener(new MessageListener());
|
||||
|
||||
//Accept commands
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.cloudcraftgaming.eventlisteners;
|
||||
|
||||
import com.cloudcraftgaming.Main;
|
||||
import sx.blah.discord.api.events.EventSubscriber;
|
||||
import sx.blah.discord.handle.impl.events.ReadyEvent;
|
||||
import sx.blah.discord.handle.obj.Status;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/2/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class ReadyEventListener {
|
||||
|
||||
@EventSubscriber
|
||||
public void onReadyEvent(ReadyEvent event) {
|
||||
Main.client.changeStatus(Status.game("Google Calendar"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.cloudcraftgaming.internal.calendar;
|
||||
|
||||
import com.google.api.client.auth.oauth2.Credential;
|
||||
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.json.JsonFactory;
|
||||
import com.google.api.client.util.store.FileDataStoreFactory;
|
||||
import com.google.api.client.util.DateTime;
|
||||
|
||||
import com.google.api.services.calendar.CalendarScopes;
|
||||
import com.google.api.services.calendar.model.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CalendarAuth {
|
||||
/** Application name. */
|
||||
private static final String APPLICATION_NAME =
|
||||
"DisCal";
|
||||
|
||||
/** Directory to store user credentials for this application. */
|
||||
private static final java.io.File DATA_STORE_DIR = new java.io.File(
|
||||
System.getProperty("user.home"), ".credentials/calendar-java-quickstart");
|
||||
|
||||
/** Global instance of the {@link FileDataStoreFactory}. */
|
||||
private static FileDataStoreFactory DATA_STORE_FACTORY;
|
||||
|
||||
/** Global instance of the JSON factory. */
|
||||
private static final JsonFactory JSON_FACTORY =
|
||||
JacksonFactory.getDefaultInstance();
|
||||
|
||||
/** Global instance of the HTTP transport. */
|
||||
private static HttpTransport HTTP_TRANSPORT;
|
||||
|
||||
/** Global instance of the scopes required by this quickstart.
|
||||
*
|
||||
* If modifying these scopes, delete your previously saved credentials
|
||||
* at ~/.credentials/calendar-java-quickstart
|
||||
*/
|
||||
private static final List<String> SCOPES =
|
||||
Arrays.asList(CalendarScopes.CALENDAR_READONLY);
|
||||
|
||||
static {
|
||||
try {
|
||||
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
|
||||
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an authorized Credential object.
|
||||
* @return an authorized Credential object.
|
||||
* @throws IOException
|
||||
*/
|
||||
public static Credential authorize() throws IOException {
|
||||
// Load client secrets.
|
||||
InputStream in =
|
||||
CalendarAuth.class.getResourceAsStream("/client_secret.json");
|
||||
GoogleClientSecrets clientSecrets =
|
||||
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
|
||||
|
||||
// Build flow and trigger user authorization request.
|
||||
GoogleAuthorizationCodeFlow flow =
|
||||
new GoogleAuthorizationCodeFlow.Builder(
|
||||
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
|
||||
.setDataStoreFactory(DATA_STORE_FACTORY)
|
||||
.setAccessType("offline")
|
||||
.build();
|
||||
Credential credential = new AuthorizationCodeInstalledApp(
|
||||
flow, new LocalServerReceiver()).authorize("user");
|
||||
System.out.println(
|
||||
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
|
||||
return credential;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build and return an authorized Calendar client service.
|
||||
* @return an authorized Calendar client service
|
||||
* @throws IOException
|
||||
*/
|
||||
public static com.google.api.services.calendar.Calendar
|
||||
getCalendarService() throws IOException {
|
||||
Credential credential = authorize();
|
||||
return new com.google.api.services.calendar.Calendar.Builder(
|
||||
HTTP_TRANSPORT, JSON_FACTORY, credential)
|
||||
.setApplicationName(APPLICATION_NAME)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static void init(String[] args) throws IOException {
|
||||
// Build a new authorized API client service.
|
||||
// Note: Do not confuse this class with the
|
||||
// com.google.api.services.calendar.model.Calendar class.
|
||||
com.google.api.services.calendar.Calendar service =
|
||||
getCalendarService();
|
||||
|
||||
// List the next 10 events from the primary calendar.
|
||||
DateTime now = new DateTime(System.currentTimeMillis());
|
||||
Events events = service.events().list("primary")
|
||||
.setMaxResults(10)
|
||||
.setTimeMin(now)
|
||||
.setOrderBy("startTime")
|
||||
.setSingleEvents(true)
|
||||
.execute();
|
||||
List<Event> items = events.getItems();
|
||||
if (items.size() == 0) {
|
||||
System.out.println("No upcoming events found.");
|
||||
} else {
|
||||
System.out.println("Upcoming events");
|
||||
for (Event event : items) {
|
||||
DateTime start = event.getStart().getDateTime();
|
||||
if (start == null) {
|
||||
start = event.getStart().getDate();
|
||||
}
|
||||
System.out.printf("%s (%s)\n", event.getSummary(), start);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.cloudcraftgaming.internal.data;
|
||||
|
||||
/**
|
||||
* Created by Nova Fox on 1/2/2017.
|
||||
* Website: www.cloudcraftgaming.com
|
||||
* For Project: DisCal
|
||||
*/
|
||||
public class BotData {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user