Commits

Afriza N. Arief committed d767a4b

Add codes to import MUIS Singapore Prayer Times to Google Calendar

1. Use Adobe Acrobat Reader to open the prayer timetable in PDF format
2. File > Save As > Text
3. Get OAuth Client ID from Google API Console
( https://code.google.com/apis/console )
4. Edit shared/shared-sample-cmdline/src/main/resources/client_secrets.json
5. Run this application to import the text file to Google Calendar

Comments (0)

Files changed (8)

calendar-cmdline-sample/pom.xml

     </plugins>
     <finalName>${project.artifactId}-${project.version}</finalName>
   </build>
+  <repositories>
+    <repository>
+      <id>google-api-services</id>
+      <url>http://mavenrepo.google-api-java-client.googlecode.com/hg</url>
+    </repository>
+  </repositories>
   <dependencies>
     <dependency>
       <groupId>com.google.apis-samples</groupId>
       <artifactId>shared-sample-calendar</artifactId>
       <version>v2-1.1.0</version>
     </dependency>
+    <dependency>
+      <groupId>com.google.apis</groupId>
+      <artifactId>google-api-services-calendar</artifactId>
+      <version>v3-1.3.1-beta</version>
+    </dependency>
   </dependencies>
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

calendar-cmdline-sample/src/main/java/cc/co/abuaasiya/muis/BatchInsertPrayerTimes.java

+/*
+ * Copyright (c) 2012 Afriza N. Arief
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package cc.co.abuaasiya.muis;
+
+import com.google.api.client.http.HttpStatusCodes;
+import com.google.api.client.util.DateTime;
+import com.google.api.services.calendar.Calendar;
+import com.google.api.services.calendar.CalendarClient;
+import com.google.api.services.calendar.CalendarUrl;
+import com.google.api.services.calendar.model.BatchStatus;
+import com.google.api.services.calendar.model.Event;
+import com.google.api.services.calendar.model.EventDateTime;
+import com.google.api.services.calendar.model.EventEntry;
+import com.google.api.services.calendar.model.EventFeed;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * @author afriza.na@gmail.com (Afriza N. Arief)
+ * 
+ */
+public class BatchInsertPrayerTimes {
+  private String calendarUrl =
+      "https://www.google.com/calendar/feeds/7ja3tv2ojb5dbvpd828oujtb8s%40group.calendar.google.com/private/full";
+  private CalendarClient client_v2;
+  private Calendar service;
+  private String calendarId;
+  private List<EventEntry> pendingEvents = new LinkedList<EventEntry>();
+  private int batchSequence = 0;
+  private int batchMinSize = 10;
+  private int batchMaxSize = 50;
+
+  public BatchInsertPrayerTimes(CalendarClient client, Calendar service, String calendarId) {
+    this.client_v2 = client;
+    this.service = service;
+    this.calendarId = calendarId;
+  }
+
+  public void initialize() {
+    pendingEvents.clear();
+    batchSequence = 0;
+  }
+
+  public void addPrayerTime(PrayerTime pt) {
+    // EventEntry event = new EventEntry();
+    // event.title = pt.name;
+    // event.when = new When();
+    // event.when.startTime = new DateTime(pt.calendar.getTime(), pt.calendar.getTimeZone());
+    // event.batchId = Integer.toString(batchSequence++);
+    // event.batchOperation = BatchOperation.INSERT;
+    // pendingEvents.add(event);
+    // autoExecute();
+    try {
+      Event event = new Event();
+      event.setSummary(pt.name);
+      event.setStart(new EventDateTime().setDateTime(new DateTime(pt.calendar.getTime(),
+          pt.calendar.getTimeZone())));
+      event.setEnd(new EventDateTime().setDateTime(event.getStart().getDateTime()));
+      event = service.events().insert(calendarId, event).execute();
+      System.out.println("-> " + event.getId());
+    } catch (IOException exception) {
+      exception.printStackTrace();
+    }
+  }
+
+  public void remove(Event event) {
+    try {
+      service.events().delete(calendarId, event.getId()).execute();
+    } catch (IOException exception) {
+      exception.printStackTrace();
+    }
+  }
+
+  protected void autoExecute() {
+    // if (pendingEvents.size() >= batchMinSize) {
+    // execute();
+    // initialize();
+    // }
+  }
+
+  public void execute() {
+    if (pendingEvents.size() == 0) return;
+    // batch link
+    try {
+      CalendarUrl eventFeedUrl = new CalendarUrl(calendarUrl);
+      eventFeedUrl.maxResults = 0;
+      CalendarUrl batchUrl =
+          new CalendarUrl(client_v2.eventFeed().list().execute(eventFeedUrl).getBatchLink());
+
+      EventFeed batchFeed = new EventFeed();
+      for (Iterator<EventEntry> iterator = pendingEvents.iterator(); iterator.hasNext();) {
+        EventEntry ev = iterator.next();
+        batchFeed.events.add(ev);
+        iterator.remove();
+        if (batchFeed.events.size() == batchMaxSize) {
+          send(batchUrl, batchFeed);
+          batchFeed = new EventFeed();
+        }
+      }
+      if (batchFeed.events.size() > 0) {
+        send(batchUrl, batchFeed);
+        batchFeed = new EventFeed();
+      }
+      initialize();
+    } catch (IOException exception) {
+      exception.printStackTrace();
+    }
+  }
+
+  /**
+   * @param batchUrl
+   * @param batchFeed
+   * @return
+   * @throws IOException
+   */
+  private EventFeed send(CalendarUrl batchUrl, EventFeed batchFeed) throws IOException {
+    EventFeed result = client_v2.eventFeed().batch().execute(batchFeed, batchUrl);
+    for (EventEntry event : result.events) {
+      BatchStatus batchStatus = event.batchStatus;
+      if (batchStatus != null && !HttpStatusCodes.isSuccess(batchStatus.code)) {
+        System.err.println("Error posting event: " + batchStatus.reason);
+      }
+    }
+    return result;
+  }
+
+}

calendar-cmdline-sample/src/main/java/cc/co/abuaasiya/muis/Environment.java

+/*
+ * Copyright (c) 2012 Afriza N. Arief
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package cc.co.abuaasiya.muis;
+
+import java.io.File;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+import java.util.TimeZone;
+
+/**
+ * @author afriza.na@gmail.com (Afriza N. Arief)
+ * 
+ */
+public class Environment {
+  public static final DateFormat dateFmt = new SimpleDateFormat("d/M/yyyy HH:mm");
+  public static final String calendarId = "7ja3tv2ojb5dbvpd828oujtb8s@group.calendar.google.com";
+  public static final TimeZone tz = TimeZone.getTimeZone("Asia/Singapore");
+  public static final Locale locale = new Locale("en", "SG");
+  public static final Calendar calendarPrototype = new GregorianCalendar(tz, locale);
+
+  /**
+   * @return
+   */
+  public static File getMUISPrayerTimetableTextFile() {
+    return new File("C:/Users/arief/Desktop/Prayer Timetable 2012.txt");
+  }
+
+  /**
+   * @return
+   */
+  public static Calendar createDateTimeCalendar() {
+    return (Calendar) calendarPrototype.clone();
+  }
+
+}

calendar-cmdline-sample/src/main/java/cc/co/abuaasiya/muis/ImportPrayerTimes.java

+/*
+ * Copyright (c) 2012 Afriza N. Arief
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package cc.co.abuaasiya.muis;
+
+import com.google.api.client.http.HttpResponseException;
+import com.google.api.services.calendar.Calendar;
+import com.google.api.services.calendar.CalendarClient;
+import com.google.api.services.samples.shared.cmdline.CmdlineUtils;
+
+import java.io.IOException;
+
+/**
+ * @author afriza.na@gmail.com (Afriza N. Arief)
+ * 
+ */
+public class ImportPrayerTimes {
+  public static CalendarClient client_v2;
+  public static Calendar service;
+
+  /**
+   * @param args
+   */
+  public static void run() {
+    PrayerTimetable timetable = new PrayerTimetable();
+    try {
+      timetable.read(Environment.getMUISPrayerTimetableTextFile());
+      // timetable.write(System.out);
+      insertMissingPrayerTimes(timetable, null);
+    } catch (IOException exception) {
+      exception.printStackTrace();
+    }
+  }
+
+  /**
+   * @param timetable
+   * @param string
+   */
+  private static void insertMissingPrayerTimes(PrayerTimetable timetable, String calendarUri) {
+    try {
+      // GoogleAccessProtectedResource accessProtectedResource =
+      // OAuth2Native.authorize(new LocalServerReceiver(), null, "google-chrome",
+      // CalendarUrl.ROOT_URL);
+      // CalendarClient client =
+      // new CalendarClient(
+      // new CalendarCmdlineRequestInitializer(accessProtectedResource).createRequestFactory());
+      // client.setPrettyPrint(true);
+      // client.setApplicationName("Google-CalendarSample/1.0");
+      try {
+        new InsertMissingPrayerTimes(service, timetable).run();
+      } catch (HttpResponseException e) {
+        System.err.println(e.getMessage());
+        if (e.getResponse().isSuccessStatusCode())
+          System.err.println(e.getResponse().parseAsString());
+        throw e;
+      }
+    } catch (Throwable t) {
+      t.printStackTrace();
+      try {
+        CmdlineUtils.getHttpTransport().shutdown();
+      } catch (IOException e) {
+        e.printStackTrace();
+      }
+      // System.exit(1);
+    }
+  }
+}

calendar-cmdline-sample/src/main/java/cc/co/abuaasiya/muis/InsertMissingPrayerTimes.java

+/*
+ * Copyright (c) 2012 Afriza N. Arief
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package cc.co.abuaasiya.muis;
+
+import com.google.api.client.util.DateTime;
+import com.google.api.services.calendar.Calendar;
+import com.google.api.services.calendar.model.Event;
+import com.google.api.services.calendar.model.Events;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.util.Iterator;
+
+/**
+ * @author afriza.na@gmail.com (Afriza N. Arief)
+ * 
+ */
+public class InsertMissingPrayerTimes {
+  private Calendar service;
+  private PrayerTimetable timetable;
+  private BatchInsertPrayerTimes batch;
+
+  public InsertMissingPrayerTimes(Calendar service, PrayerTimetable timetable) {
+    this.service = service;
+    this.timetable = timetable;
+    this.batch =
+        new BatchInsertPrayerTimes(ImportPrayerTimes.client_v2, service, Environment.calendarId);
+  }
+
+  public void run() throws IOException {
+    batch.initialize();
+    {
+      final String timeMin;
+      final String timeMax;
+      {
+        java.util.Calendar c;
+        c = timetable.getFirstDate();
+        timeMin = new DateTime(c.getTime(), c.getTimeZone()).toStringRfc3339();
+        c = timetable.getLastDate();
+        c.add(java.util.Calendar.DATE, 1);
+        timeMax = new DateTime(c.getTime(), c.getTimeZone()).toStringRfc3339();
+      }
+      String pageToken = null;
+      Iterator<PrayerTime> ptIt = timetable.getPrayerTimes().iterator();
+      PrayerTime curPT;
+      long ptSecs;
+      curPT = ptIt.hasNext() ? ptIt.next() : null;
+      ptSecs = curPT != null ? curPT.getSeconds() : -1;
+
+      Event evOld = null;
+      long evSecsOld = -1;
+      while (true) {
+        Events events =
+            service.events().list(Environment.calendarId).setSingleEvents(true)
+                .setOrderBy("startTime").setTimeMin(timeMin).setTimeMax(timeMax).setMaxResults(500)
+                .setPageToken(pageToken).execute();
+        // write(System.out, events);
+        for (Event event : events.getItems()) {
+          if (curPT == null) break;
+          long evSecs = event.getStart().getDateTime().getValue() / 1000;
+
+          // insert missing prayer times
+          while (curPT != null && ptSecs <= evSecs) {
+            if (ptSecs != evSecs) {
+
+              System.out.println("=====<<missing>>=====");
+              printPrayerTime(curPT, ptSecs);
+              printEvent(event, evSecs);
+
+              batch.addPrayerTime(curPT);
+            }
+
+            curPT = ptIt.hasNext() ? ptIt.next() : null;
+            ptSecs = curPT != null ? curPT.getSeconds() : -1;
+          }
+
+          // remove duplicate
+          if (evSecs == evSecsOld) {
+            System.out.println("=====<<duplicate>>=====");
+            printEvent(evOld, evSecsOld);
+            printEvent(event, evSecs);
+            batch.remove(event);
+          }
+
+          evOld = event;
+          evSecsOld = evSecs;
+        }
+        batch.execute();
+        pageToken = events.getNextPageToken();
+        if (pageToken == null || pageToken.isEmpty()) {
+          break;
+        }
+      }
+      // handle tailing remainder
+      while (curPT != null) {
+
+        System.out.println("=====<<missing>>=====");
+        printPrayerTime(curPT, ptSecs);
+
+        batch.addPrayerTime(curPT);
+
+        curPT = ptIt.hasNext() ? ptIt.next() : null;
+        ptSecs = curPT != null ? curPT.getSeconds() : -1;
+      }
+    }
+    batch.execute();
+    System.out.println("=====<<finished>>=====");
+  }
+
+  /**
+   * @param event
+   * @param eventInSeconds
+   */
+  void printEvent(Event event, long eventInSeconds) {
+    System.out.print(event.getStart().getDateTime().toStringRfc3339());
+    System.out.print(" ");
+    System.out.print(eventInSeconds);
+    System.out.print(" ");
+    System.out.print(event.getSummary());
+    System.out.print(" -> ");
+    System.out.println(event.getId());
+  }
+
+  /**
+   * @param prayerTime
+   * @param prayerTimeInSeconds
+   */
+  void printPrayerTime(PrayerTime prayerTime, long prayerTimeInSeconds) {
+    System.out.print(Environment.dateFmt.format(prayerTime.calendar.getTime()));
+    System.out.print(" ");
+    System.out.print(prayerTimeInSeconds);
+    System.out.print(" ");
+    System.out.println(prayerTime.name);
+  }
+
+  /**
+   * 
+   */
+  protected void write(OutputStream os, Events events) {
+    PrintStream ps = new PrintStream(os);
+    for (Event ev : events.getItems()) {
+      ps.print(ev.getStart().getDateTime());
+      ps.print(" ");
+      ps.println(ev.getSummary());
+    }
+  }
+}

calendar-cmdline-sample/src/main/java/cc/co/abuaasiya/muis/PrayerTime.java

+/*
+ * Copyright (c) 2012 Afriza N. Arief
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package cc.co.abuaasiya.muis;
+
+import java.util.Calendar;
+
+
+/**
+ * @author afriza.na@gmail.com (Afriza N. Arief)
+ * 
+ */
+public class PrayerTime {
+  String name;
+  Calendar calendar;
+
+  public long getSeconds() {
+    if (calendar == null) return -1;
+    return calendar.getTimeInMillis() / 1000;
+  }
+}

calendar-cmdline-sample/src/main/java/cc/co/abuaasiya/muis/PrayerTimetable.java

+/*
+ * Copyright (c) 2012 Afriza N. Arief
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package cc.co.abuaasiya.muis;
+
+import com.google.common.io.Files;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.nio.charset.Charset;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+
+/**
+ * @author afriza.na@gmail.com (Afriza N. Arief)
+ * 
+ */
+public class PrayerTimetable {
+  private List<PrayerTime> prayerTimes = new ArrayList<PrayerTime>(366 * 6);
+
+  private static class PrayerTimesLineProcessor {
+    private List<String> prayerTimeNames = null;
+    private List<PrayerTime> result = null;
+
+    PrayerTimesLineProcessor() {
+    }
+
+    boolean handleLine(String line) {
+      String lineSplit[] = line.split("\\s+");
+      if (handlePrayerTimesLine(lineSplit)) return true;
+      if (handlePrayerNamesLine(lineSplit)) return false;
+      return false;
+    }
+
+    private boolean handlePrayerNamesLine(String[] lineSplit) {
+      if (prayerTimeNames != null) return false;
+      if (lineSplit.length != 8 || !lineSplit[0].equals("Date") || !lineSplit[1].equals("Day"))
+        return false;
+
+      prayerTimeNames = new ArrayList<String>(6);
+      for (int i = 2; i < lineSplit.length; ++i) {
+        prayerTimeNames.add(lineSplit[i]);
+      }
+      return true;
+    }
+
+    private boolean handlePrayerTimesLine(String[] lineSplit) {
+      if (prayerTimeNames == null) return false;
+      if (lineSplit.length != 14) return false;
+
+
+      try {
+        result = new ArrayList<PrayerTime>(6);
+        for (int i = 0; i < 6; ++i) {
+          PrayerTime pt = new PrayerTime();
+
+          pt.name = prayerTimeNames.get(i);
+
+          final int DATE = 0;
+          final int HOUR = (i + 1) * 2;
+          final int MINUTE = HOUR + 1;
+          StringBuffer sbuf = new StringBuffer();
+          sbuf.append(lineSplit[DATE]);
+          sbuf.append(" ");
+          sbuf.append(lineSplit[HOUR]);
+          sbuf.append(":");
+          sbuf.append(lineSplit[MINUTE]);
+          Date date = Environment.dateFmt.parse(sbuf.toString());
+          pt.calendar = Environment.createDateTimeCalendar();
+          pt.calendar.setTime(date);
+
+          result.add(pt);
+        }
+        fixAmPm(result);
+      } catch (ParseException exception) {
+        exception.printStackTrace();
+        return false;
+      }
+
+      return true;
+    }
+
+    /**
+     * Fix AM/PM
+     */
+    void fixAmPm(List<PrayerTime> prayerTimes) {
+      final int ZOHOR = 2;
+      for (int i = ZOHOR; i < prayerTimes.size(); ++i) {
+        PrayerTime pt = prayerTimes.get(i);
+        PrayerTime ptPrev = prayerTimes.get(i - 1);
+        if (pt.calendar.get(Calendar.HOUR_OF_DAY) < ptPrev.calendar.get(Calendar.HOUR_OF_DAY))
+          pt.calendar.add(Calendar.HOUR_OF_DAY, 12);
+      }
+    }
+
+    List<PrayerTime> getResult() {
+      return result;
+    }
+  }
+
+  /**
+   * @return the prayerTimes
+   */
+  public List<PrayerTime> getPrayerTimes() {
+    return prayerTimes;
+  }
+
+  public Calendar getFirstDate() {
+    if (prayerTimes.size() == 0) return null;
+    Calendar first = (Calendar) prayerTimes.get(0).calendar.clone();
+    first.set(Calendar.HOUR_OF_DAY, 0);
+    first.set(Calendar.MINUTE, 0);
+    first.set(Calendar.SECOND, 0);
+    first.set(Calendar.MILLISECOND, 0);
+    return first;
+  }
+
+  public Calendar getLastDate() {
+    if (prayerTimes.size() == 0) return null;
+    Calendar last = (Calendar) prayerTimes.get(prayerTimes.size() - 1).calendar.clone();
+    last.set(Calendar.HOUR_OF_DAY, 0);
+    last.set(Calendar.MINUTE, 0);
+    last.set(Calendar.SECOND, 0);
+    last.set(Calendar.MILLISECOND, 0);
+    return last;
+  }
+
+  public void read(File file) throws IOException {
+    PrayerTimesLineProcessor processor = new PrayerTimesLineProcessor();
+    for (String line : Files.readLines(file, Charset.forName("UTF-8"))) {
+      if (processor.handleLine(line)) prayerTimes.addAll(processor.getResult());
+    }
+  }
+
+  public void write(OutputStream ostream) {
+    PrintStream ps = new PrintStream(ostream);
+    for (PrayerTime pt : prayerTimes) {
+      ps.print(Environment.dateFmt.format(pt.calendar.getTime()));
+      ps.print(" ");
+      ps.println(pt.name);
+    }
+  }
+}

calendar-cmdline-sample/src/main/java/com/google/api/services/samples/calendar/cmdline/CalendarSample.java

 import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
 import com.google.api.client.http.HttpResponseException;
 import com.google.api.client.http.HttpStatusCodes;
+import com.google.api.client.http.javanet.NetHttpTransport;
+import com.google.api.client.json.jackson.JacksonFactory;
 import com.google.api.client.util.DateTime;
+import com.google.api.services.calendar.Calendar;
 import com.google.api.services.calendar.CalendarClient;
+import com.google.api.services.calendar.CalendarRequestInitializer;
 import com.google.api.services.calendar.CalendarUrl;
 import com.google.api.services.calendar.model.BatchOperation;
 import com.google.api.services.calendar.model.BatchStatus;
 import com.google.api.services.samples.shared.cmdline.oauth2.LocalServerReceiver;
 import com.google.api.services.samples.shared.cmdline.oauth2.OAuth2Native;
 
+import cc.co.abuaasiya.muis.ImportPrayerTimes;
+
 import java.io.IOException;
 import java.util.Date;
 
       GoogleAccessProtectedResource accessProtectedResource =
           OAuth2Native.authorize(new LocalServerReceiver(), null, "google-chrome",
               CalendarUrl.ROOT_URL);
-      CalendarClient client =
-          new CalendarClient(
-              new CalendarCmdlineRequestInitializer(accessProtectedResource).createRequestFactory());
+      CalendarRequestInitializer cReqInit =
+          new CalendarCmdlineRequestInitializer(accessProtectedResource);
+      CalendarClient client = new CalendarClient(cReqInit.createRequestFactory());
       client.setPrettyPrint(true);
       client.setApplicationName("Google-CalendarSample/1.0");
+      Calendar service =
+          Calendar
+              .builder(new NetHttpTransport(), new JacksonFactory())
+              .setHttpRequestInitializer(
+                  new CalendarCmdlineRequestInitializer(accessProtectedResource)).build();
       try {
-        run(client);
+        ImportPrayerTimes.client_v2 = client;
+        ImportPrayerTimes.service = service;
+        ImportPrayerTimes.run();
+        // run(client);
+        if (ImportPrayerTimes.service == null) throw new HttpResponseException(null);
       } catch (HttpResponseException e) {
         System.err.println(e.getResponse().parseAsString());
         throw e;