Search in sources :

Example 6 with DateTime

use of com.google.api.client.util.DateTime in project openhab1-addons by openhab.

the class GCalEventDownloader method downloadEventFeed.

/**
     * Connects to Google-Calendar Service and returns the specified Events
     *
     * @return the corresponding Events or <code>null</code> if an error
     *         occurs. <i>Note:</i> We do only return events if their startTime lies between
     *         <code>now</code> and <code>now + 2 * refreshInterval</code> to reduce
     *         the amount of events to process.
     */
private static Events downloadEventFeed() {
    if (StringUtils.isBlank(calendar_name)) {
        logger.warn("Login aborted no calendar name defined");
        return null;
    }
    // authorization
    CalendarListEntry calendarID = GCalGoogleOAuth.getCalendarId(calendar_name);
    if (calendarID == null) {
        return null;
    }
    DateTime start = new DateTime(new Date(), TimeZone.getTimeZone(calendarID.getTimeZone()));
    DateTime end = new DateTime(new Date(start.getValue() + (2 * refreshInterval)), TimeZone.getTimeZone(calendarID.getTimeZone()));
    logger.debug("Downloading calendar feed for time interval: {} to  {} ", start, end);
    Events feed = null;
    try {
        Credential credential = GCalGoogleOAuth.getCredential(false);
        // set up global Calendar instance
        Calendar client = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName("openHAB").build();
        Calendar.Events.List l = client.events().list(calendarID.getId()).setSingleEvents(true).setTimeMin(start).setTimeMax(end);
        // add the fulltext filter if it has been configured
        if (StringUtils.isNotBlank(filter)) {
            l = l.setQ(filter);
        }
        feed = l.execute();
    } catch (IOException e1) {
        logger.error("Event fetch failed: {}", e1.getMessage());
    }
    try {
        if (feed != null) {
            checkIfFullCalendarFeed(feed.getItems());
        }
        return feed;
    } catch (Exception e) {
        logger.error("downloading CalendarEventFeed throws exception: {}", e.getMessage());
    }
    return null;
}
Also used : CalendarListEntry(com.google.api.services.calendar.model.CalendarListEntry) Credential(com.google.api.client.auth.oauth2.Credential) Events(com.google.api.services.calendar.model.Events) Calendar(com.google.api.services.calendar.Calendar) TimeRangeCalendar(org.openhab.io.gcal.internal.util.TimeRangeCalendar) IOException(java.io.IOException) DateTime(com.google.api.client.util.DateTime) EventDateTime(com.google.api.services.calendar.model.EventDateTime) Date(java.util.Date) ConfigurationException(org.osgi.service.cm.ConfigurationException) SchedulerException(org.quartz.SchedulerException) IOException(java.io.IOException)

Example 7 with DateTime

use of com.google.api.client.util.DateTime in project api-samples by youtube.

the class CreateBroadcast method main.

/**
     * Create and insert a liveBroadcast resource.
     */
public static void main(String[] args) {
    // This OAuth 2.0 access scope allows for full read/write access to the
    // authenticated user's account.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "createbroadcast");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-createbroadcast-sample").build();
        // Prompt the user to enter a title for the broadcast.
        String title = getBroadcastTitle();
        System.out.println("You chose " + title + " for broadcast title.");
        // Create a snippet with the title and scheduled start and end
        // times for the broadcast. Currently, those times are hard-coded.
        LiveBroadcastSnippet broadcastSnippet = new LiveBroadcastSnippet();
        broadcastSnippet.setTitle(title);
        broadcastSnippet.setScheduledStartTime(new DateTime("2024-01-30T00:00:00.000Z"));
        broadcastSnippet.setScheduledEndTime(new DateTime("2024-01-31T00:00:00.000Z"));
        // Set the broadcast's privacy status to "private". See:
        // https://developers.google.com/youtube/v3/live/docs/liveBroadcasts#status.privacyStatus
        LiveBroadcastStatus status = new LiveBroadcastStatus();
        status.setPrivacyStatus("private");
        LiveBroadcast broadcast = new LiveBroadcast();
        broadcast.setKind("youtube#liveBroadcast");
        broadcast.setSnippet(broadcastSnippet);
        broadcast.setStatus(status);
        // Construct and execute the API request to insert the broadcast.
        YouTube.LiveBroadcasts.Insert liveBroadcastInsert = youtube.liveBroadcasts().insert("snippet,status", broadcast);
        LiveBroadcast returnedBroadcast = liveBroadcastInsert.execute();
        // Print information from the API response.
        System.out.println("\n================== Returned Broadcast ==================\n");
        System.out.println("  - Id: " + returnedBroadcast.getId());
        System.out.println("  - Title: " + returnedBroadcast.getSnippet().getTitle());
        System.out.println("  - Description: " + returnedBroadcast.getSnippet().getDescription());
        System.out.println("  - Published At: " + returnedBroadcast.getSnippet().getPublishedAt());
        System.out.println("  - Scheduled Start Time: " + returnedBroadcast.getSnippet().getScheduledStartTime());
        System.out.println("  - Scheduled End Time: " + returnedBroadcast.getSnippet().getScheduledEndTime());
        // Prompt the user to enter a title for the video stream.
        title = getStreamTitle();
        System.out.println("You chose " + title + " for stream title.");
        // Create a snippet with the video stream's title.
        LiveStreamSnippet streamSnippet = new LiveStreamSnippet();
        streamSnippet.setTitle(title);
        // Define the content distribution network settings for the
        // video stream. The settings specify the stream's format and
        // ingestion type. See:
        // https://developers.google.com/youtube/v3/live/docs/liveStreams#cdn
        CdnSettings cdnSettings = new CdnSettings();
        cdnSettings.setFormat("1080p");
        cdnSettings.setIngestionType("rtmp");
        LiveStream stream = new LiveStream();
        stream.setKind("youtube#liveStream");
        stream.setSnippet(streamSnippet);
        stream.setCdn(cdnSettings);
        // Construct and execute the API request to insert the stream.
        YouTube.LiveStreams.Insert liveStreamInsert = youtube.liveStreams().insert("snippet,cdn", stream);
        LiveStream returnedStream = liveStreamInsert.execute();
        // Print information from the API response.
        System.out.println("\n================== Returned Stream ==================\n");
        System.out.println("  - Id: " + returnedStream.getId());
        System.out.println("  - Title: " + returnedStream.getSnippet().getTitle());
        System.out.println("  - Description: " + returnedStream.getSnippet().getDescription());
        System.out.println("  - Published At: " + returnedStream.getSnippet().getPublishedAt());
        // Construct and execute a request to bind the new broadcast
        // and stream.
        YouTube.LiveBroadcasts.Bind liveBroadcastBind = youtube.liveBroadcasts().bind(returnedBroadcast.getId(), "id,contentDetails");
        liveBroadcastBind.setStreamId(returnedStream.getId());
        returnedBroadcast = liveBroadcastBind.execute();
        // Print information from the API response.
        System.out.println("\n================== Returned Bound Broadcast ==================\n");
        System.out.println("  - Broadcast Id: " + returnedBroadcast.getId());
        System.out.println("  - Bound Stream Id: " + returnedBroadcast.getContentDetails().getBoundStreamId());
    } catch (GoogleJsonResponseException e) {
        System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) DateTime(com.google.api.client.util.DateTime) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException)

Example 8 with DateTime

use of com.google.api.client.util.DateTime in project camel by apache.

the class CalendarEventsIntegrationTest method testInsert.

@Test
public void testInsert() throws Exception {
    Event event = new Event();
    event.setSummary("Feed the Camel");
    event.setLocation("Somewhere");
    ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
    attendees.add(new EventAttendee().setEmail("camel-google-calendar.janstey@gmail.com"));
    event.setAttendees(attendees);
    Date startDate = new Date();
    Date endDate = new Date(startDate.getTime() + 3600000);
    DateTime start = new DateTime(startDate, TimeZone.getTimeZone("UTC"));
    event.setStart(new EventDateTime().setDateTime(start));
    DateTime end = new DateTime(endDate, TimeZone.getTimeZone("UTC"));
    event.setEnd(new EventDateTime().setDateTime(end));
    final Map<String, Object> headers = new HashMap<String, Object>();
    // parameter type is String
    headers.put("CamelGoogleCalendar.calendarId", getCalendar().getId());
    // parameter type is com.google.api.services.calendar.model.Event
    headers.put("CamelGoogleCalendar.content", event);
    final com.google.api.services.calendar.model.Event result = requestBodyAndHeaders("direct://INSERT", null, headers);
    assertEquals("Feed the Camel", result.getSummary());
    LOG.debug("insert: " + result);
}
Also used : Event(com.google.api.services.calendar.model.Event) HashMap(java.util.HashMap) EventAttendee(com.google.api.services.calendar.model.EventAttendee) EventDateTime(com.google.api.services.calendar.model.EventDateTime) ArrayList(java.util.ArrayList) Date(java.util.Date) EventDateTime(com.google.api.services.calendar.model.EventDateTime) DateTime(com.google.api.client.util.DateTime) Event(com.google.api.services.calendar.model.Event) Test(org.junit.Test)

Example 9 with DateTime

use of com.google.api.client.util.DateTime in project camel by apache.

the class DriveFilesIntegrationTest method testTouch.

@Test
public void testTouch() throws Exception {
    File theTestFile = uploadTestFile();
    DateTime createdDate = theTestFile.getModifiedDate();
    // using String message body for single parameter "fileId"
    File result = requestBody("direct://TOUCH", theTestFile.getId());
    assertNotNull("touch result", result);
    assertTrue(result.getModifiedDate().getValue() > createdDate.getValue());
}
Also used : File(com.google.api.services.drive.model.File) DateTime(com.google.api.client.util.DateTime) Test(org.junit.Test)

Example 10 with DateTime

use of com.google.api.client.util.DateTime in project local-data-aragopedia by aragonopendata.

the class GoogleDriveAPI method listOwnerFilesAfterDate.

public List<File> listOwnerFilesAfterDate(String stringDateLastChange) {
    SimpleDateFormat formatFullDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date dateLastChange = null;
    try {
        dateLastChange = formatFullDate.parse(stringDateLastChange);
    } catch (ParseException e1) {
        log.error("Error parse date in list", e1);
    }
    FileList result;
    List<File> files = null;
    try {
        result = service.files().list().setMaxResults(500).execute();
        files = result.getItems();
    } catch (IOException e) {
        log.error("Error list files", e);
    }
    if (files == null || files.size() == 0) {
        log.error("No files found");
    } else {
        log.info("Files:\n");
        for (File file : files) {
            DateTime dateTime = file.getModifiedDate();
            Date dateModifyFile = new Date(dateTime.getValue());
            if (dateModifyFile.after(dateLastChange))
                log.info("Title " + file.getTitle() + " id " + file.getId() + " DateTime " + formatFullDate.format(dateModifyFile));
        }
    }
    return files;
}
Also used : FileList(com.google.api.services.drive.model.FileList) ParseException(java.text.ParseException) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) File(com.google.api.services.drive.model.File) Date(java.util.Date) DateTime(com.google.api.client.util.DateTime)

Aggregations

DateTime (com.google.api.client.util.DateTime)10 IOException (java.io.IOException)5 Date (java.util.Date)5 EventDateTime (com.google.api.services.calendar.model.EventDateTime)3 File (com.google.api.services.drive.model.File)3 Credential (com.google.api.client.auth.oauth2.Credential)2 Event (com.google.api.services.calendar.model.Event)2 FileList (com.google.api.services.drive.model.FileList)2 Function (com.google.common.base.Function)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 SuppressLint (android.annotation.SuppressLint)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Calendar (com.google.api.services.calendar.Calendar)1 CalendarListEntry (com.google.api.services.calendar.model.CalendarListEntry)1 EventAttendee (com.google.api.services.calendar.model.EventAttendee)1 Events (com.google.api.services.calendar.model.Events)1 Lifecycle (com.google.api.services.storage.model.Bucket.Lifecycle)1