225 lines
8.7 KiB
Java
225 lines
8.7 KiB
Java
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.security.GeneralSecurityException;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
|
|
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.javanet.NetHttpTransport;
|
|
import com.google.api.client.json.JsonFactory;
|
|
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.CalendarScopes;
|
|
import com.google.api.services.calendar.model.CalendarListEntry;
|
|
import com.google.api.services.calendar.model.Event;
|
|
import com.google.api.services.calendar.model.Events;
|
|
|
|
public class CalendarQuickstart {
|
|
private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart";
|
|
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
|
|
private static final String TOKENS_DIRECTORY_PATH = "tokens";
|
|
|
|
/**
|
|
* 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);
|
|
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
|
|
|
|
|
|
private static HashMap<String, String> colorMap;
|
|
|
|
static {
|
|
colorMap = new HashMap<String, String>();
|
|
colorMap.put("1", "7986cb"); // Lavendel
|
|
colorMap.put("2", "33b679"); // Salbei
|
|
colorMap.put("3", "8e24aa"); // Weintraube
|
|
colorMap.put("4", "e67c73"); // Flamingo
|
|
colorMap.put("5", "f6bf26"); // Banane
|
|
colorMap.put("6", "f4511e"); // Mandarine
|
|
colorMap.put("7", "039be5"); // Pfau
|
|
colorMap.put("8", "616161"); // Grafit
|
|
colorMap.put("9", "3f51b5"); // Heidelbeere
|
|
colorMap.put("10", "0b8043"); // Basilikum
|
|
colorMap.put("11", "d50000"); // Tomate
|
|
}
|
|
|
|
/**
|
|
* Creates an authorized Credential object.
|
|
* @param HTTP_TRANSPORT The network HTTP Transport.
|
|
* @return An authorized Credential object.
|
|
* @throws IOException If the credentials.json file cannot be found.
|
|
*/
|
|
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
|
|
// Load client secrets.
|
|
InputStream in = CalendarQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
|
|
if (in == null) {
|
|
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
|
|
}
|
|
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(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
|
|
// .setAccessType("offline")
|
|
.build();
|
|
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
|
|
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
|
|
}
|
|
|
|
public static void main(String... args) throws IOException, GeneralSecurityException {
|
|
// Build a new authorized API client service.
|
|
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
|
|
Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
|
|
.setApplicationName(APPLICATION_NAME)
|
|
.build();
|
|
|
|
System.out.println("The list:");
|
|
com.google.api.services.calendar.model.CalendarList calList = service.calendarList().list().execute();
|
|
|
|
System.out.println("size: " + calList.size());
|
|
for (CalendarListEntry entry : calList.getItems()) {
|
|
System.out.println("ID " + entry.getId() + ", Description " + entry.getDescription() + ", Summary: " + entry.getSummary());
|
|
}
|
|
|
|
|
|
System.out.println("\nEvents:");
|
|
int ct = 0;
|
|
int colCt = 0;
|
|
int hitCt = 0;
|
|
int pageCt = 0;
|
|
int cngCt = 0;
|
|
|
|
String srcId = "tero23q0dqqfc45r4jjcecl7bs@group.calendar.google.com";
|
|
String trgId = "rpg91b8ls4e437a2gu63jhmlfk@group.calendar.google.com";
|
|
|
|
Events events = null;
|
|
List<Event> srcItems = null;
|
|
|
|
do {
|
|
String nextPage = null;
|
|
if (events != null) {
|
|
nextPage = events.getNextPageToken();
|
|
events = service.events().list(srcId)
|
|
// .setSingleEvents(true)
|
|
.setPageToken(nextPage)
|
|
.execute();
|
|
}
|
|
else {
|
|
events = service.events().list(srcId)
|
|
// .setMaxResults(2450)
|
|
// .setTimeMin(new DateTime(1230800400000l)) // 1230800400000l 1497690000000l
|
|
// .setOrderBy("startTime")
|
|
// .setShowDeleted(false)
|
|
// .setOrderBy("updated")
|
|
// .setUpdatedMin(new DateTime(1559379600000l))
|
|
// .setSingleEvents(true)
|
|
.execute();
|
|
}
|
|
|
|
if (srcItems == null)
|
|
srcItems = events.getItems();
|
|
else
|
|
srcItems.addAll(events.getItems());
|
|
|
|
pageCt++;
|
|
} while (events.getNextPageToken() != null);
|
|
|
|
|
|
|
|
List<Event> trgItems = null;
|
|
|
|
do {
|
|
String nextPage = null;
|
|
if (events != null) {
|
|
nextPage = events.getNextPageToken();
|
|
events = service.events().list(srcId)
|
|
.setPageToken(nextPage)
|
|
.execute();
|
|
}
|
|
else {
|
|
events = service.events().list(srcId).execute();
|
|
}
|
|
|
|
if (trgItems == null)
|
|
trgItems = events.getItems();
|
|
else
|
|
trgItems.addAll(events.getItems());
|
|
} while (events.getNextPageToken() != null);
|
|
|
|
|
|
if (srcItems.isEmpty()) {
|
|
System.out.println("No upcoming events found.");
|
|
} else {
|
|
|
|
System.out.println("# src items: " + srcItems.size());
|
|
|
|
for (Event event : srcItems) {
|
|
|
|
ct++;
|
|
|
|
// XXX Restart from number
|
|
// if (ct < 1300)
|
|
// continue;
|
|
|
|
// XXX This event is not updatable for some reason
|
|
if (event.getId().equals("6cqj2d3265h34bb574qj4b9kcgr36bb2ckrjebb160rj4e3465imcdr26g"))
|
|
continue;
|
|
|
|
if (event.getColorId() != null) {
|
|
|
|
colCt++;
|
|
|
|
String startStr = null;
|
|
if (event.getStart() != null && event.getStart().getDateTime() != null)
|
|
startStr = event.getStart().getDateTime().toString();
|
|
|
|
System.out.println("ID: " + event.getId()
|
|
+ ", CREATED: " + event.getCreated()
|
|
+ ", UPDATED: " + event.getUpdated()
|
|
+ ", COLOR: " + event.getColorId()
|
|
+ ", START: " + startStr
|
|
+ ", " + ct
|
|
+ ", DESCRIPTION: " + event.getDescription()
|
|
+ ", SUMMARY: " + event.getSummary());
|
|
|
|
for (Event trgEvent : trgItems) {
|
|
if (trgEvent.getId().equals(event.getId())) {
|
|
|
|
Event currentEvent = service.events().get(trgId, trgEvent.getId()).execute();
|
|
trgEvent.setSequence(currentEvent.getSequence());
|
|
|
|
trgEvent.setColorId(event.getColorId());
|
|
service.events().update(trgId, trgEvent.getId(), trgEvent).execute();
|
|
cngCt++;
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
System.out.println("Page count: " + pageCt);
|
|
System.out.println("Colored source events: " + colCt);
|
|
System.out.println("Target hits: " + hitCt);
|
|
System.out.println("Change count: " + cngCt);
|
|
|
|
}
|
|
} |