Java tutorial
////////////////////////////////////////////////////////////////////////////////////////// // Copyright (c) 2016, Joo Vitor Verona Biazibetti - All Rights Reserved / // / // Licensed under the GNU General Public License v3; / // you may not use this file except in compliance with the License. / // / // You may obtain a copy of the License at / // http://www.gnu.org/licenses/gpl.html / // / // 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. / // / // Written by Joo Vitor Verona Biazibetti <joaaoverona@gmail.com>, March 2016 / // https://www.github.com/BloodShura / ////////////////////////////////////////////////////////////////////////////////////////// package br.shura.team.mpsbot.runtime; import br.shura.team.mpsbot.model.ApiKeys; import br.shura.team.mpsbot.model.Bot; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.TwitterStream; import twitter4j.TwitterStreamFactory; import twitter4j.auth.AccessToken; import twitter4j.auth.OAuthAuthorization; import twitter4j.auth.RequestToken; import twitter4j.conf.Configuration; import twitter4j.conf.ConfigurationBuilder; import twitter4j.media.ImageUpload; import twitter4j.media.ImageUploadFactory; import java.io.Serializable; import java.net.MalformedURLException; import java.net.URL; import java.util.function.Function; /** * ConnectedBot.java * * @author <a href="https://www.github.com/BloodShura">BloodShura</a> (Joo Vitor Verona Biazibetti) * @contact joaaoverona@gmail.com * @date 17/03/2016 - 23:22 * @since GAMMA - 0x3 */ public class ConnectedBot implements Serializable { private AccessToken accessToken; private final Bot bot; private Twitter handler; private transient TwitterStream streamer; private transient ImageUpload uploader; public ConnectedBot(Bot bot) { this.bot = bot; } public ConnectedBot(Bot bot, Twitter handler, AccessToken accessToken) { this.accessToken = accessToken; this.bot = bot; this.handler = handler; } public void connect() { ApiKeys keys = getBot().getApiKeys(); if (!isConnected()) { Configuration configuration = new ConfigurationBuilder().setOAuthAccessToken(keys.getAccessToken()) .setOAuthAccessTokenSecret(keys.getAccessTokenSecret()) .setOAuthConsumerKey(keys.getConsumerKey()).setOAuthConsumerSecret(keys.getConsumerSecret()) .build(); OAuthAuthorization auth = new OAuthAuthorization(configuration); TwitterStreamFactory streamFactory = new TwitterStreamFactory(configuration); ImageUploadFactory uploadFactory = new ImageUploadFactory(configuration); this.streamer = streamFactory.getInstance(auth); this.uploader = uploadFactory.getInstance(auth); } } public AccessToken getAccessToken() { return accessToken; } public Bot getBot() { return bot; } public Twitter getHandler() { return handler; } public TwitterStream getStreamer() { return streamer; } public ImageUpload getUploader() { return uploader; } public boolean isConnected() { return streamer != null && uploader != null; } public boolean isPrepared() { return accessToken != null && handler != null; } public void prepare(Function<URL, String> pinEvaluator) throws IllegalStateException, MalformedURLException, TwitterException { ApiKeys keys = getBot().getApiKeys(); if (!isPrepared()) { Twitter handler = TwitterFactory.getSingleton(); handler.setOAuthConsumer(keys.getConsumerKey(), keys.getConsumerSecret()); RequestToken requestToken = handler.getOAuthRequestToken(); URL url = new URL(requestToken.getAuthorizationURL()); while (accessToken == null) { String pin = pinEvaluator.apply(url); try { if (pin != null && !pin.isEmpty()) { this.accessToken = handler.getOAuthAccessToken(requestToken, pin); this.handler = handler; } else { return; } } catch (TwitterException exception) { System.err.println("Could not evaluate OAuth access token:"); System.err.println(exception); } } } getHandler().verifyCredentials(); } }