Add support for google calendar

This commit is contained in:
2022-03-16 14:03:09 +01:00
parent 8ba07bab8e
commit ea2697e114
16 changed files with 278 additions and 630 deletions
@@ -9,23 +9,15 @@ import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.DateTime;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.EventDateTime;
import com.google.api.services.calendar.model.EventReminder;
import com.google.api.services.calendar.model.Events;
import de.jeyp91.tippliga.TLWLeague;
import de.jeyp91.tippliga.TLWMatchday;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -38,7 +30,7 @@ public class GoogleCalendarConnector {
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR_READONLY);
private static final List<String> SCOPES = Collections.singletonList(CalendarScopes.CALENDAR);
private static final String CREDENTIALS_FILE_PATH = "/Google_Credentials.json";
/**
@@ -62,95 +54,25 @@ public class GoogleCalendarConnector {
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
//returns an authorized Credential object.
return credential;
}
private static com.google.api.services.calendar.Calendar getSerivce() {
public static com.google.api.services.calendar.Calendar getSerivce() {
com.google.api.services.calendar.Calendar service = null;
final NetHttpTransport HTTP_TRANSPORT;
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
service = new com.google.api.services.calendar.Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
return service;
}
public static List<Event> getAllEventsStartingFromDateTime(int league, DateTime dateTime) {
com.google.api.services.calendar.Calendar service = getSerivce();
Events events = null;
String calendarUrl = CalendarConfigProvider.getCalendarUrl(league);
// Build a new authorized API client service.
try {
events = service.events().list(calendarUrl)
.setMaxResults(100)
.setTimeMin(dateTime)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute();
} catch (IOException e) {
e.printStackTrace();
}
// List the next 10 events from the primary calendar.
assert events != null;
List<Event> items = events.getItems();
return items;
}
public static void createNewEvent(TLWMatchday matchday) {
Calendar service = getSerivce();
String calendarId = CalendarConfigProvider.getCalendarId(matchday.getLeague());
String matchdayName = matchday.getMatchdayName() == "" ? matchday.getMatchday().toString() + ". Spieltag" : matchday.getMatchdayName();
String summary = "Tippabgabeschluss " + TLWLeague.getLeagueNameCalendar(matchday.getLeague()) + " " + matchdayName;
String description = "Tippabgabeschluss " + TLWLeague.getLeagueNameCalendar(matchday.getLeague()) + " " + matchdayName;
Event event = new Event()
.setSummary(summary)
.setLocation("https://www.tippliga-wuerzburg.de/app.php/football/bet")
.setDescription(description);
DateTime deliveryDate = new DateTime(matchday.getDeliveryDate().replace(" ", "T"));
EventDateTime date = new EventDateTime()
.setDateTime(deliveryDate)
.setTimeZone("Europe/Berlin");
event.setStart(date);
event.setEnd(date);
// Set reminder to 12 hours before
Event.Reminders reminders = new Event.Reminders();
EventReminder[] reminderOverrides = new EventReminder[] {
new EventReminder().setMethod("popup").setMinutes(12 * 60),
};
reminders.setOverrides(Arrays.asList(reminderOverrides));
event.setReminders(reminders);
try {
service.events().insert(calendarId, event).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void updateMatchdayDate(TLWMatchday matchday) {
}
public static Event getEventForMatchday(TLWMatchday matchday) {
return null;
}
}