Search in sources :

Example 1 with InstagramUploadVideoJobRequest

use of org.brunocvcunha.instagram4j.requests.internal.InstagramUploadVideoJobRequest in project instagram4j by brunocvcunha.

the class InstagramUploadVideoRequest method execute.

@Override
public StatusResult execute() throws ClientProtocolException, IOException {
    HttpPost post = createHttpRequest();
    String uploadId = String.valueOf(System.currentTimeMillis());
    post.setEntity(createMultipartEntity(uploadId));
    try (CloseableHttpResponse response = api.getClient().execute(post)) {
        api.setLastResponse(response);
        int resultCode = response.getStatusLine().getStatusCode();
        String content = EntityUtils.toString(response.getEntity());
        log.info("First phase result " + resultCode + ": " + content);
        post.releaseConnection();
        InstagramUploadVideoResult firstPhaseResult = parseJson(content, InstagramUploadVideoResult.class);
        if (!firstPhaseResult.getStatus().equalsIgnoreCase("ok")) {
            throw new RuntimeException("Error happened in video upload session start: " + firstPhaseResult.getMessage());
        }
        String uploadUrl = firstPhaseResult.getVideo_upload_urls().get(3).get("url").toString();
        String uploadJob = firstPhaseResult.getVideo_upload_urls().get(3).get("job").toString();
        StatusResult uploadJobResult = api.sendRequest(new InstagramUploadVideoJobRequest(uploadId, uploadUrl, uploadJob, videoFile));
        log.info("Upload result: " + uploadJobResult);
        if (!uploadJobResult.getStatus().equalsIgnoreCase("ok")) {
            throw new RuntimeException("Error happened in video upload submit job: " + uploadJobResult.getMessage());
        }
        StatusResult thumbnailResult = configureThumbnail(uploadId);
        if (!thumbnailResult.getStatus().equalsIgnoreCase("ok")) {
            throw new IllegalArgumentException("Failed to configure thumbnail: " + thumbnailResult.getMessage());
        }
        return api.sendRequest(new InstagramExposeRequest());
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InstagramUploadVideoResult(org.brunocvcunha.instagram4j.requests.payload.InstagramUploadVideoResult) StatusResult(org.brunocvcunha.instagram4j.requests.payload.StatusResult) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InstagramExposeRequest(org.brunocvcunha.instagram4j.requests.internal.InstagramExposeRequest) InstagramUploadVideoJobRequest(org.brunocvcunha.instagram4j.requests.internal.InstagramUploadVideoJobRequest)

Aggregations

CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 InstagramExposeRequest (org.brunocvcunha.instagram4j.requests.internal.InstagramExposeRequest)1 InstagramUploadVideoJobRequest (org.brunocvcunha.instagram4j.requests.internal.InstagramUploadVideoJobRequest)1 InstagramUploadVideoResult (org.brunocvcunha.instagram4j.requests.payload.InstagramUploadVideoResult)1 StatusResult (org.brunocvcunha.instagram4j.requests.payload.StatusResult)1