Search in sources :

Example 21 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class MicrosoftCalendarExporter method export.

@Override
public ExportResult<CalendarContainerResource> export(UUID jobId, TokensAndUrlAuthData authData, Optional<ExportInformation> exportInformation) {
    if (exportInformation.isPresent()) {
        // TODO support pagination
        throw new UnsupportedOperationException();
    }
    Request.Builder calendarsBuilder = getBuilder(baseUrl + CALENDARS_SUBPATH, authData);
    List<CalendarModel> calendarModels = new ArrayList<>();
    try (Response graphResponse = client.newCall(calendarsBuilder.build()).execute()) {
        ResponseBody body = graphResponse.body();
        if (body == null) {
            return new ExportResult<>(new Exception("Error retrieving contacts: response body was null"));
        }
        String graphBody = new String(body.bytes());
        Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
        // TODO String nextLink = (String) graphMap.get(ODATA_NEXT);
        // TODO ContinuationData continuationData = nextLink == null
        // ? null : new ContinuationData(new GraphPagination(nextLink));
        @SuppressWarnings("unchecked") List<Map<String, Object>> rawCalendars = (List<Map<String, Object>>) graphMap.get("value");
        if (rawCalendars == null) {
            return new ExportResult<>(ExportResult.ResultType.END);
        }
        for (Map<String, Object> rawCalendar : rawCalendars) {
            TransformResult<CalendarModel> result = transformerService.transform(CalendarModel.class, rawCalendar);
            if (result.hasProblems()) {
                // FIXME log problem
                continue;
            }
            calendarModels.add(result.getTransformed());
        }
    } catch (IOException e) {
        // FIXME log error
        e.printStackTrace();
        return new ExportResult<>(e);
    }
    List<CalendarEventModel> calendarEventModels = new ArrayList<>();
    for (CalendarModel calendarModel : calendarModels) {
        String id = calendarModel.getId();
        Request.Builder eventsBuilder = getBuilder(calculateEventsUrl(id), authData);
        try (Response graphResponse = client.newCall(eventsBuilder.build()).execute()) {
            ResponseBody body = graphResponse.body();
            if (body == null) {
                return new ExportResult<>(new Exception("Error retrieving calendar: response body was null"));
            }
            String graphBody = new String(body.bytes());
            Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
            // TODO String nextLink = (String) graphMap.get(ODATA_NEXT);
            // TODO ContinuationData continuationData = nextLink == null
            // ? null : new ContinuationData(new GraphPagination(nextLink));
            @SuppressWarnings("unchecked") List<Map<String, Object>> rawEvents = (List<Map<String, Object>>) graphMap.get("value");
            if (rawEvents == null) {
                return new ExportResult<>(ExportResult.ResultType.END);
            }
            for (Map<String, Object> rawEvent : rawEvents) {
                Map<String, String> properties = new HashMap<>();
                properties.put(CALENDAR_ID, id);
                TransformResult<CalendarEventModel> result = transformerService.transform(CalendarEventModel.class, rawEvent, properties);
                if (result.hasProblems()) {
                    // FIXME log problem
                    continue;
                }
                calendarEventModels.add(result.getTransformed());
            }
        } catch (IOException e) {
            // FIXME log error
            e.printStackTrace();
            return new ExportResult<>(e);
        }
    }
    CalendarContainerResource resource = new CalendarContainerResource(calendarModels, calendarEventModels);
    return new ExportResult<>(ExportResult.ResultType.END, resource, null);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CalendarModel(org.datatransferproject.types.common.models.calendar.CalendarModel) ArrayList(java.util.ArrayList) List(java.util.List) Request(okhttp3.Request) IOException(java.io.IOException) CalendarEventModel(org.datatransferproject.types.common.models.calendar.CalendarEventModel) CalendarContainerResource(org.datatransferproject.types.common.models.calendar.CalendarContainerResource) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) HashMap(java.util.HashMap) Map(java.util.Map) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 22 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class MicrosoftContactsExporter method doExport.

@SuppressWarnings("unchecked")
private ExportResult<ContactsModelWrapper> doExport(TokensAndUrlAuthData authData, String url) {
    Request.Builder graphReqBuilder = new Request.Builder().url(url);
    graphReqBuilder.header("Authorization", "Bearer " + authData.getAccessToken());
    try (Response graphResponse = client.newCall(graphReqBuilder.build()).execute()) {
        ResponseBody body = graphResponse.body();
        if (body == null) {
            return new ExportResult<>(new Exception("Error retrieving contacts: response body was null"));
        }
        String graphBody = new String(body.bytes());
        Map graphMap = objectMapper.reader().forType(Map.class).readValue(graphBody);
        String nextLink = (String) graphMap.get(ODATA_NEXT);
        ContinuationData continuationData = nextLink == null ? null : new ContinuationData(new GraphPagination(nextLink));
        List<Map<String, Object>> rawContacts = (List<Map<String, Object>>) graphMap.get("value");
        if (rawContacts == null) {
            return new ExportResult<>(ExportResult.ResultType.END);
        }
        ContactsModelWrapper wrapper = transform(rawContacts);
        return new ExportResult<>(ExportResult.ResultType.CONTINUE, wrapper, continuationData);
    } catch (IOException e) {
        // FIXME log error
        e.printStackTrace();
        return new ExportResult<>(e);
    }
}
Also used : GraphPagination(org.datatransferproject.transfer.microsoft.types.GraphPagination) Request(okhttp3.Request) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) ContactsModelWrapper(org.datatransferproject.types.common.models.contacts.ContactsModelWrapper) IOException(java.io.IOException) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) List(java.util.List) Map(java.util.Map) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 23 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class MastodonActivityExport method export.

@Override
public ExportResult<SocialActivityContainerResource> export(UUID jobId, CookiesAndUrlAuthData authData, Optional<ExportInformation> exportInformation) throws Exception {
    checkState(authData.getCookies().size() == 1, "Exactly 1 cookie expected: %s", authData.getCookies());
    String maxId = null;
    if (exportInformation.isPresent()) {
        StringPaginationToken pageData = (StringPaginationToken) exportInformation.get().getPaginationData();
        if (!Strings.isNullOrEmpty(pageData.getToken())) {
            maxId = pageData.getToken();
        }
    }
    MastodonHttpUtilities utilities = new MastodonHttpUtilities(authData.getCookies().get(0), authData.getUrl());
    Account account = utilities.getAccount();
    Status[] statuses = utilities.getStatuses(maxId);
    List<SocialActivityModel> activityList = new ArrayList<>(statuses.length);
    SocialActivityActor actor = new SocialActivityActor("acct:" + account.getUsername() + "@" + utilities.getHostName(), account.getDisplayName(), account.getUrl());
    ContinuationData continuationData = null;
    if (statuses.length > 0) {
        String lastId = null;
        for (Status status : statuses) {
            activityList.add(statusToActivity(account, status, utilities));
            lastId = status.getId();
        }
        continuationData = new ContinuationData(new StringPaginationToken(lastId));
    }
    return new ExportResult<>(continuationData == null ? ResultType.END : ResultType.CONTINUE, new SocialActivityContainerResource(account.getId() + maxId, actor, activityList), continuationData);
}
Also used : Status(org.datatransferproject.transfer.mastodon.model.Status) Account(org.datatransferproject.transfer.mastodon.model.Account) SocialActivityContainerResource(org.datatransferproject.types.common.models.social.SocialActivityContainerResource) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) SocialActivityModel(org.datatransferproject.types.common.models.social.SocialActivityModel) StringPaginationToken(org.datatransferproject.types.common.StringPaginationToken) SocialActivityActor(org.datatransferproject.types.common.models.social.SocialActivityActor) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 24 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class ImgurPhotosExporter method requestAlbums.

/**
 * Exports albums.
 *
 * @param authData authentication information
 * @param paginationData pagination information to use for subsequent calls
 */
private ExportResult<PhotosContainerResource> requestAlbums(TokensAndUrlAuthData authData, PaginationData paginationData) throws IOException {
    ImmutableList.Builder<PhotoAlbum> albumBuilder = ImmutableList.builder();
    List<IdOnlyContainerResource> albumIds = new ArrayList<>();
    int page = paginationData == null ? 0 : ((IntPaginationToken) paginationData).getStart();
    String url = format(ALBUMS_URL_TEMPLATE, page);
    List<Map<String, Object>> items = requestData(authData, url);
    // Request result doesn't indicate if it's the last page
    boolean hasMore = (items != null && items.size() != 0);
    for (Map<String, Object> item : items) {
        albumBuilder.add(new PhotoAlbum((String) item.get("id"), (String) item.get("title"), (String) item.get("description")));
        // Save album id for recalling export to get all the photos in albums
        albumIds.add(new IdOnlyContainerResource((String) item.get("id")));
    }
    if (page == 0) {
        // For checking non-album photos. Their export should be performed after all the others
        // Album will be created later
        albumIds.add(new IdOnlyContainerResource(DEFAULT_ALBUM_ID));
    }
    PaginationData newPage = null;
    if (hasMore) {
        newPage = new IntPaginationToken(page + 1);
        int start = ((IntPaginationToken) newPage).getStart();
        monitor.info(() -> format("albums size: %s, newPage: %s", items.size(), start));
    }
    PhotosContainerResource photosContainerResource = new PhotosContainerResource(albumBuilder.build(), null);
    ContinuationData continuationData = new ContinuationData(newPage);
    albumIds.forEach(continuationData::addContainerResource);
    ExportResult.ResultType resultType = ExportResult.ResultType.CONTINUE;
    if (newPage == null) {
        resultType = ExportResult.ResultType.END;
    }
    return new ExportResult<>(resultType, photosContainerResource, continuationData);
}
Also used : IntPaginationToken(org.datatransferproject.types.common.IntPaginationToken) PaginationData(org.datatransferproject.types.common.PaginationData) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) ContinuationData(org.datatransferproject.spi.transfer.types.ContinuationData) PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) IdOnlyContainerResource(org.datatransferproject.types.common.models.IdOnlyContainerResource) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) Map(java.util.Map) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Example 25 with ExportResult

use of org.datatransferproject.spi.transfer.provider.ExportResult in project data-transfer-project by google.

the class InstagramPhotoExporter method exportPhotos.

private ExportResult<PhotosContainerResource> exportPhotos(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
    Preconditions.checkNotNull(authData);
    MediaResponse response;
    try {
        response = makeRequest(MEDIA_URL, MediaResponse.class, authData);
    } catch (IOException e) {
        return new ExportResult<>(e);
    }
    List<PhotoModel> photos = new ArrayList<>();
    // TODO: check out paging.
    for (MediaFeedData photo : response.getData()) {
        // TODO json mapping is broken.
        String photoId = photo.getId();
        String url = photo.getImages().getStandardResolution().getUrl();
        String text = (photo.getCaption() != null) ? photo.getCaption().getText() : null;
        photos.add(new PhotoModel("Instagram photo: " + photoId, url, text, null, photoId, FAKE_ALBUM_ID, false));
    }
    List<PhotoAlbum> albums = new ArrayList<>();
    if (!photos.isEmpty() && !pageData.isPresent()) {
        albums.add(new PhotoAlbum(FAKE_ALBUM_ID, "Imported Instagram Photos", "Photos imported from instagram"));
    }
    return new ExportResult<>(ResultType.END, new PhotosContainerResource(albums, photos));
}
Also used : PhotosContainerResource(org.datatransferproject.types.common.models.photos.PhotosContainerResource) MediaResponse(org.datatransferproject.transfer.instagram.photos.model.MediaResponse) PhotoModel(org.datatransferproject.types.common.models.photos.PhotoModel) ArrayList(java.util.ArrayList) MediaFeedData(org.datatransferproject.transfer.instagram.photos.model.MediaFeedData) IOException(java.io.IOException) PhotoAlbum(org.datatransferproject.types.common.models.photos.PhotoAlbum) ExportResult(org.datatransferproject.spi.transfer.provider.ExportResult)

Aggregations

ExportResult (org.datatransferproject.spi.transfer.provider.ExportResult)44 ContinuationData (org.datatransferproject.spi.transfer.types.ContinuationData)33 ArrayList (java.util.ArrayList)26 IOException (java.io.IOException)23 StringPaginationToken (org.datatransferproject.types.common.StringPaginationToken)22 PhotosContainerResource (org.datatransferproject.types.common.models.photos.PhotosContainerResource)21 PaginationData (org.datatransferproject.types.common.PaginationData)19 IdOnlyContainerResource (org.datatransferproject.types.common.models.IdOnlyContainerResource)18 PhotoAlbum (org.datatransferproject.types.common.models.photos.PhotoAlbum)16 PhotoModel (org.datatransferproject.types.common.models.photos.PhotoModel)16 ResultType (org.datatransferproject.spi.transfer.provider.ExportResult.ResultType)14 List (java.util.List)11 Optional (java.util.Optional)9 UUID (java.util.UUID)9 Collectors (java.util.stream.Collectors)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 ImmutableList (com.google.common.collect.ImmutableList)7 Collection (java.util.Collection)6 TokensAndUrlAuthData (org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData)6 Before (org.junit.Before)6