essential-actions
Clone or download
Modified Files
package com.stream_pi.twitteraction;
package com.stream_pi.twitteraction;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Property;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.actionproperty.property.Type;
import com.stream_pi.action_api.normalaction.NormalAction;
import com.stream_pi.action_api.normalaction.NormalAction;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlert;
import com.stream_pi.util.alert.StreamPiAlertListener;
import com.stream_pi.util.alert.StreamPiAlertListener;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.alert.StreamPiAlertType;
import com.stream_pi.util.uihelper.HBoxInputBox;
import com.stream_pi.util.uihelper.HBoxInputBox;
import com.stream_pi.util.version.Version;
import com.stream_pi.util.version.Version;
import javafx.application.Platform;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.concurrent.Task;
import javafx.scene.control.*;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBox;
import twitter4j.Status;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.conf.ConfigurationBuilder;
import java.net.URI;
import java.net.URI;
import java.util.Random;
import java.util.Random;
public class TwitterAction extends NormalAction {
public class TwitterAction extends NormalAction {
Button loginAsNewUserButton;
Button loginAsNewUserButton, logoutButton;
public TwitterAction()
public TwitterAction()
{
{
setName("Tweet");
setName("Tweet");
setCategory("Twitter");
setCategory("Twitter");
setAuthor("rnayabed");
setAuthor("rnayabed");
setServerButtonGraphic("fab-twitter");
setServerButtonGraphic("fab-twitter");
setHelpLink("https://github.com/stream-pi/essentialactions");
setHelpLink("https://github.com/stream-pi/essentialactions");
setVersion(new Version(1,0,0));
setVersion(new Version(1,0,0));
loginAsNewUserButton = new Button("Login as new user");
loginAsNewUserButton = new Button("Login as new user");
loginAsNewUserButton.setOnAction(event-> new Thread(new Task<Void>() {
loginAsNewUserButton.setOnAction(event-> new Thread(new Task<Void>() {
@Override
@Override
protected Void call()
protected Void call()
{
{
try {
try {
Platform.runLater(()->loginAsNewUserButton.setDisable(true));
Platform.runLater(()->{
loginAsNewUserButton.setDisable(true);
logoutButton.setDisable(true);
});
loginAsNewUser();
loginAsNewUser();
} catch (Exception e) {
} catch (Exception e) {
Platform.runLater(()-> {
Platform.runLater(()-> {
loginAsNewUserButton.setDisable(false);
Platform.runLater(()->{
loginAsNewUserButton.setDisable(false);
logoutButton.setDisable(false);
});
new StreamPiAlert(e.getMessage(), StreamPiAlertType.ERROR).show();
new StreamPiAlert(e.getMessage(), StreamPiAlertType.ERROR).show();
});
});
e.printStackTrace();
e.printStackTrace();
}
}
return null;
return null;
}
}
}).start());
}).start());
logoutButton = new Button("Login as new user");
logoutButton.setOnAction(event-> new Thread(new Task<Void>() {
@Override
protected Void call()
{
try {
Platform.runLater(()->{
loginAsNewUserButton.setDisable(false);
logoutButton.setDisable(false);
});
logout();
} catch (Exception e) {
Platform.runLater(()-> {
Platform.runLater(()->{
loginAsNewUserButton.setDisable(false);
logoutButton.setDisable(false);
});
new StreamPiAlert(e.getMessage(), StreamPiAlertType.ERROR).show();
});
e.printStackTrace();
}
return null;
}
}).start());
setButtonBar(loginAsNewUserButton);
setButtonBar(loginAsNewUserButton, logoutButton);
}
}
@Override
@Override
public void initProperties() throws Exception
public void initProperties() throws Exception
{
{
Property oAuthConsumerKey = new Property("consumer_key", Type.STRING);
Property oAuthConsumerKey = new Property("consumer_key", Type.STRING);
oAuthConsumerKey.setDisplayName("API Key");
oAuthConsumerKey.setDisplayName("API Key");
Property oAuthConsumerKeySecret = new Property("consumer_key_secret", Type.STRING);
Property oAuthConsumerKeySecret = new Property("consumer_key_secret", Type.STRING);
oAuthConsumerKeySecret.setDisplayName("API Key Secret");
oAuthConsumerKeySecret.setDisplayName("API Key Secret");
Property oAuthAccessToken = new Property("access_token", Type.STRING);
Property oAuthAccessToken = new Property("access_token", Type.STRING);
oAuthAccessToken.setDisplayName("Access Token");
oAuthAccessToken.setDisplayName("Access Token");
oAuthAccessToken.setVisible(false);
oAuthAccessToken.setVisible(false);
Property oAuthAccessTokenSecret = new Property("access_token_secret", Type.STRING);
Property oAuthAccessTokenSecret = new Property("access_token_secret", Type.STRING);
oAuthAccessTokenSecret.setDisplayName("Access Token Secret");
oAuthAccessTokenSecret.setDisplayName("Access Token Secret");
oAuthAccessTokenSecret.setVisible(false);
oAuthAccessTokenSecret.setVisible(false);
addServerProperties(
addServerProperties(
oAuthConsumerKey,
oAuthConsumerKey,
oAuthConsumerKeySecret,
oAuthConsumerKeySecret,
oAuthAccessToken,
oAuthAccessToken,
oAuthAccessTokenSecret
oAuthAccessTokenSecret
);
);
Property toBeTweeted = new Property("tweet", Type.STRING);
Property toBeTweeted = new Property("tweet", Type.STRING);
toBeTweeted.setDisplayName("Tweet");
toBeTweeted.setDisplayName("Tweet");
toBeTweeted.setDefaultValueStr("Hello From Stream-Pi!");
toBeTweeted.setDefaultValueStr("Hello From Stream-Pi!");
toBeTweeted.setCanBeBlank(false);
toBeTweeted.setCanBeBlank(false);
addClientProperties(
addClientProperties(
toBeTweeted
toBeTweeted
);
);
}
}
public void loginAsNewUser() throws Exception
public void loginAsNewUser() throws Exception
{
logout();
getAuthToken();
}
public void logout() throws Exception
{
{
setNewTwitterConfig(
setNewTwitterConfig(
getServerProperties().getSingleProperty("consumer_key").getStringValue(),
getServerProperties().getSingleProperty("consumer_key").getStringValue(),
getServerProperties().getSingleProperty("consumer_key_secret").getStringValue(),
getServerProperties().getSingleProperty("consumer_key_secret").getStringValue(),
null,null
null,null
);
);
getAuthToken();
}
}
public void getAuthToken() throws Exception {
public void getAuthToken() throws Exception {
RequestToken requestToken = tf.getInstance().getOAuthRequestToken();
RequestToken requestToken = tf.getInstance().getOAuthRequestToken();
String cancel = "Cancel";
String cancel = "Cancel";
String login = "Log In";
String login = "Log In";
TextArea authURLTextArea = new TextArea(requestToken.getAuthorizationURL());
TextArea authURLTextArea = new TextArea(requestToken.getAuthorizationURL());
authURLTextArea.setEditable(false);
authURLTextArea.setEditable(false);
TextField pinTextField = new TextField();
TextField pinTextField = new TextField();
HBox textBox = new HBoxInputBox("PIN", pinTextField);
HBox textBox = new HBoxInputBox("PIN", pinTextField);
Platform.runLater(()->{
Platform.runLater(()->{
try
try
{
{
StreamPiAlert alert = new StreamPiAlert("Authorise App", StreamPiAlertType.INFORMATION, cancel, login);
StreamPiAlert alert = new StreamPiAlert("Authorise App", StreamPiAlertType.INFORMATION, cancel, login);
VBox vBox = new VBox(new Label("Go the following Link below, authorise app and then enter the PIN Below"), authURLTextArea, textBox);
VBox vBox = new VBox(new Label("Go the following Link below, authorise app and then enter the PIN Below"), authURLTextArea, textBox);
vBox.setSpacing(5.0);
vBox.setSpacing(5.0);
alert.setAlertContent(vBox);
alert.setAlertContent(vBox);
alert.show();
alert.show();
alert.setOnClicked(new StreamPiAlertListener(){
alert.setOnClicked(new StreamPiAlertListener(){
@Override
@Override
public void onClick(String buttonClicked) {
public void onClick(String buttonClicked) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
try
try
{
{
if(buttonClicked.equals(login))
if(buttonClicked.equals(login))
{
{
AccessToken accessToken = tf.getInstance().getOAuthAccessToken(requestToken, pinTextField.getText());
AccessToken accessToken = tf.getInstance().getOAuthAccessToken(requestToken, pinTextField.getText());
getServerProperties().getSingleProperty("access_token").setStringValue(accessToken.getToken());
getServerProperties().getSingleProperty("access_token").setStringValue(accessToken.getToken());
getServerProperties().getSingleProperty("access_token_secret").setStringValue(accessToken.getTokenSecret());
getServerProperties().getSingleProperty("access_token_secret").setStringValue(accessToken.getTokenSecret());
saveServerProperties();
saveServerProperties();
initAction();
initAction();
new StreamPiAlert("Success", "Login Successful!", StreamPiAlertType.INFORMATION).show();
new StreamPiAlert("Success", "Login Successful!", StreamPiAlertType.INFORMATION).show();
}
}
else
else
{
{
initAction();
initAction();
}
}
}
}
catch (Exception e)
catch (Exception e)
{
{
new StreamPiAlert(e.getMessage(), StreamPiAlertType.ERROR).show();
new StreamPiAlert(e.getMessage(), StreamPiAlertType.ERROR).show();
e.printStackTrace();
e.printStackTrace();
}
}
}
}
});
});
}
}
catch (Exception e)
catch (Exception e)
{
{
new StreamPiAlert(e.getMessage(), StreamPiAlertType.ERROR).show();
new StreamPiAlert(e.getMessage(), StreamPiAlertType.ERROR).show();
e.printStackTrace();
e.printStackTrace();
}
}
finally
finally
{
{
loginAsNewUserButton.setDisable(false);
loginAsNewUserButton.setDisable(false);
}
}
});
});
}
}
TwitterFactory tf;
TwitterFactory tf;
@Override
@Override
public void initAction() throws Exception {
public void initAction() throws Exception {
//System.setProperty("twitter4j.http.useSSL", "true");
//System.setProperty("twitter4j.http.useSSL", "true");
setNewTwitterConfig(
setNewTwitterConfig(
getServerProperties().getSingleProperty("consumer_key").getStringValue(),
getServerProperties().getSingleProperty("consumer_key").getStringValue(),
getServerProperties().getSingleProperty("consumer_key_secret").getStringValue(),
getServerProperties().getSingleProperty("consumer_key_secret").getStringValue(),
getServerProperties().getSingleProperty("access_token").getStringValue(),
getServerProperties().getSingleProperty("access_token").getStringValue(),
getServerProperties().getSingleProperty("access_token_secret").getStringValue()
getServerProperties().getSingleProperty("access_token_secret").getStringValue()
);
);
}
}
public void setNewTwitterConfig(String consumerKey, String consumerKeySecret, String accessToken, String accessTokenSecret)
public void setNewTwitterConfig(String consumerKey, String consumerKeySecret, String accessToken, String accessTokenSecret)
{
{
ConfigurationBuilder cb = new ConfigurationBuilder();
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
cb.setDebugEnabled(true)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerKeySecret)
.setOAuthConsumerSecret(consumerKeySecret)
.setOAuthAccessToken(accessToken)
.setOAuthAccessToken(accessToken)
.setOAuthAccessTokenSecret(accessTokenSecret);
.setOAuthAccessTokenSecret(accessTokenSecret);
tf = new TwitterFactory(cb.build());
tf = new TwitterFactory(cb.build());
}
}
@Override
@Override
public void onActionClicked() throws Exception {
public void onActionClicked() throws Exception {
Twitter twitter = tf.getInstance();
Twitter twitter = tf.getInstance();
twitter.updateStatus(getClientProperties().getSingleProperty("tweet").getStringValue());
twitter.updateStatus(getClientProperties().getSingleProperty("tweet").getStringValue());
}
}
/*public String addRandomBlank(String value)
/*public String addRandomBlank(String value)
{
{
return value+("⠀".repeat(new Random().nextInt(100)));
return value+("⠀".repeat(new Random().nextInt(100)));
}*/
}*/
@Override
@Override
public void onShutDown() throws Exception {
public void onShutDown() throws Exception {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
}
}
}
}