cmu.edu.homework.mediaUpload.TwitterVideoUpload.java Source code

Java tutorial

Introduction

Here is the source code for cmu.edu.homework.mediaUpload.TwitterVideoUpload.java

Source

/*
 * Copyright 2007 Yusuke Yamamoto
 *
 * 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 cmu.edu.homework.mediaUpload;

import java.io.File;
import java.io.InputStream;

import twitter4j.StatusUpdate;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.OAuthAuthorization;
import twitter4j.conf.Configuration;

/**
 * @author Yusuke Yamamoto - yusuke at mac.com
 * @since Twitter4J 2.2.5
 */
class TwitterVideoUpload implements VideoUpload {
    private final Twitter twitter;

    public TwitterVideoUpload(Configuration conf, OAuthAuthorization oauth) {
        twitter = new TwitterFactory(conf).getInstance(oauth);
    }

    @Override
    public String upload(File video, String message) throws TwitterException {
        return twitter.updateStatus(new StatusUpdate(message).media(video)).getText();
    }

    @Override
    public String upload(File video) throws TwitterException {
        return twitter.updateStatus(new StatusUpdate("").media(video)).getText();
    }

    @Override
    public String upload(String videoFileName, InputStream videoBody) throws TwitterException {
        return twitter.updateStatus(new StatusUpdate("").media(videoFileName, videoBody)).getText();
    }

    @Override
    public String upload(String videoFileName, InputStream videoBody, String message) throws TwitterException {
        return twitter.updateStatus(new StatusUpdate(message).media(videoFileName, videoBody)).getText();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        TwitterVideoUpload that = (TwitterVideoUpload) o;

        if (twitter != null ? !twitter.equals(that.twitter) : that.twitter != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return twitter != null ? twitter.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "TwitterUpload{" + "twitter=" + twitter + '}';
    }
}