com.echobox.api.tiktok.connection.VideoConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ebx-tiktok-sdk Show documentation
Show all versions of ebx-tiktok-sdk Show documentation
ebx-tiktok-sdk is a pure Java TikTok API client. It implements the v1.3 API.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.echobox.api.tiktok.connection;
import com.echobox.api.tiktok.client.Parameter;
import com.echobox.api.tiktok.client.TikTokClient;
import com.echobox.api.tiktok.model.BusinessId;
import com.echobox.api.tiktok.model.PostInfo;
import com.echobox.api.tiktok.model.TikTokShare;
import com.echobox.api.tiktok.model.request.PublishRequest;
import com.echobox.api.tiktok.model.response.ListResponse;
import com.echobox.api.tiktok.model.response.PublishResponse;
import lombok.AllArgsConstructor;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* Connection class for video endpoints
*
* @author eddspencer
*/
@AllArgsConstructor
public class VideoConnection extends ConnectionBase {
private static final String ENDPOINT_PUBLISH = "/business/video/publish/";
private static final String ENDPOINT_LIST = "/business/video/list/";
private static final String MAX_COUNT = "20";
private static final int MAX_VIDEO_IDS_FILTER_SIZE = 20;
private final TikTokClient tikTokClient;
/**
* Publish a video to a page with the given post info, returning the share id of the publish
* request. The request will happen asynchronously please listen to the webhook for updates
* @see Publish a public video to an owned account
*
* @param businessId the business id
* @param videoURL the video url to publish
* @param postInfo the post info
* @return the share id
*/
public PublishResponse publish(BusinessId businessId, String videoURL, PostInfo postInfo) {
final PublishRequest requestData = new PublishRequest(businessId.getId(), videoURL, postInfo);
return tikTokClient.publish(ENDPOINT_PUBLISH, PublishResponse.class, requestData);
}
/**
* List all videos published to a given page going backwards in time from the given unix
* time until the specified time or the total count is reached. This will call the API to get
* pages of data 20 videos at a time.
* @see Get video data of a Business Account
*
* @param businessId the business id
* @param lowerUnixTimeMillis the unix time in the past that it should stop going backwards to
* * fetch data - can be null
* @param upperUnixTimeMillis the unix time to start going backwards in time from - can be null
* @param totalCount the total count of videos to fetch
* @param fields the fields to populate for each video
* @return the videos returned by the API
*/
public List list(BusinessId businessId, Long lowerUnixTimeMillis,
Long upperUnixTimeMillis, Integer totalCount, String... fields) {
List parameters = generateCommonParameters(businessId, fields);
Connection connection =
tikTokClient.fetchConnection(ENDPOINT_LIST, ListResponse.class, parameters.toArray(
new Parameter[0]));
return connection.getData(lowerUnixTimeMillis, upperUnixTimeMillis, totalCount);
}
/**
* Retrieve all videos by their video id published to a given page.
* @see Get video data of a Business Account
*
* @param businessId the business id
* @param videoIds the list of video ids that we want to filter in the result set
* @param fields the fields to populate for each video
* @return the videos returned by the API
*/
public List get(BusinessId businessId, List videoIds, String... fields) {
List parameters = generateCommonParameters(businessId, fields);
List tikTokShares = new ArrayList<>();
if (!videoIds.isEmpty()) {
AtomicInteger index = new AtomicInteger();
videoIds.stream()
.collect(Collectors.groupingBy(i -> index.getAndIncrement() / MAX_VIDEO_IDS_FILTER_SIZE))
.values()
.forEach(batch -> {
String videoIdsBatchFilter = String.format("{\"video_ids\":%s}",
formatFields(batch.toArray(new String[0])));
List parametersForBatch = new ArrayList<>(parameters);
parametersForBatch.add(Parameter.with("filters", videoIdsBatchFilter));
Connection connection = tikTokClient.fetchConnection(
ENDPOINT_LIST, ListResponse.class, parametersForBatch.toArray(new Parameter[0]));
tikTokShares.addAll(connection.getLatestData(batch.size()));
});
}
return tikTokShares;
}
/**
* Generates the parameters passed to the list endpoint
* @param businessId the business id
* @param fields the fields to populate for each video
* @return a list of parameters
*/
private List generateCommonParameters(BusinessId businessId, String... fields) {
List parameters = new ArrayList<>();
parameters.add(Parameter.with("business_id", businessId.getId()));
parameters.add(Parameter.with("max_count", MAX_COUNT));
if (fields.length > 0) {
String fieldsValue = formatFields(fields);
parameters.add(Parameter.with("fields", fieldsValue));
}
return parameters;
}
}