essential-actions

Clone or download

[Twitter Action] - Cleanup, Updated to work with new Action API

Modified Files

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>twitteraction</artifactId>
<artifactId>twitteraction</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<build>
<build>
<plugins>
<plugins>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<version>3.1.0</version>
<executions>
<executions>
<execution>
<execution>
<id>test-jar</id>
<id>test-jar</id>
<phase>package</phase>
<phase>package</phase>
<goals>
<goals>
<goal>test-jar</goal>
<goal>test-jar</goal>
</goals>
</goals>
</execution>
</execution>
</executions>
</executions>
</plugin>
</plugin>
<plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.8.1</version>
<configuration>
<configuration>
<release>11</release>
<release>11</release>
</configuration>
</configuration>
</plugin>
</plugin>
</plugins>
</plugins>
</build>
</build>
<properties>
<properties>
<ActionAPIVersion>1.0.0-SNAPSHOT</ActionAPIVersion>
<ActionAPIVersion>1.0.0-SNAPSHOT</ActionAPIVersion>
<UtilVersion>1.0.0-SNAPSHOT</UtilVersion>
<UtilVersion>1.0.0-SNAPSHOT</UtilVersion>
<JavaFXVersion>16-ea+5</JavaFXVersion>
<Twitter4JVersion>4.0.7</Twitter4JVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.target>11</maven.compiler.target>
<IkonliVersion>11.5.0</IkonliVersion>
<IkonliFA5PackVersion>11.5.0</IkonliFA5PackVersion>
</properties>
</properties>
<dependencies>
<dependencies>
<dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.7</version>
<version>${Twitter4JVersion}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${JavaFXVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>util</artifactId>
<artifactId>util</artifactId>
<version>${UtilVersion}</version>
<version>${UtilVersion}</version>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>com.stream-pi</groupId>
<groupId>com.stream-pi</groupId>
<artifactId>action-api</artifactId>
<artifactId>action-api</artifactId>
<version>${ActionAPIVersion}</version>
<version>${ActionAPIVersion}</version>
</dependency>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-fontawesome5-pack</artifactId>
<version>${IkonliFA5PackVersion}</version>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<version>${IkonliVersion}</version>
</dependency>
</dependencies>
</dependencies>
</project>
</project>
package com.stream_pi.twitteraction;
package com.stream_pi.twitteraction;
import com.stream_pi.action_api.actionproperty.property.ControlType;
import com.stream_pi.action_api.actionproperty.property.ControlType;
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.externalplugin.NormalAction;
import com.stream_pi.action_api.externalplugin.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.exception.MinorException;
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.concurrent.Task;
import javafx.concurrent.Task;
import javafx.scene.control.Button;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBox;
import twitter4j.Twitter;
import twitter4j.Twitter;
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;
public class TwitterAction extends NormalAction {
public class TwitterAction extends NormalAction
{
Button loginAsNewUserButton, logoutButton;
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,1));
setVersion(new Version(1,0,2));
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(()->{
Platform.runLater(()->{
loginAsNewUserButton.setDisable(true);
loginAsNewUserButton.setDisable(true);
logoutButton.setDisable(true);
logoutButton.setDisable(true);
});
});
loginAsNewUser();
loginAsNewUser();
} catch (Exception e) {
} catch (Exception e) {
Platform.runLater(()-> {
Platform.runLater(()-> {
Platform.runLater(()->{
Platform.runLater(()->{
loginAsNewUserButton.setDisable(false);
loginAsNewUserButton.setDisable(false);
logoutButton.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("Logout");
logoutButton = new Button("Logout");
logoutButton.setOnAction(event-> new Thread(new Task<Void>() {
logoutButton.setOnAction(event-> new Thread(new Task<Void>() {
@Override
@Override
protected Void call()
protected Void call()
{
{
try {
try {
Platform.runLater(()->{
Platform.runLater(()->{
loginAsNewUserButton.setDisable(false);
loginAsNewUserButton.setDisable(false);
logoutButton.setDisable(false);
logoutButton.setDisable(false);
});
});
logout();
logout();
} catch (Exception e) {
} catch (Exception e) {
Platform.runLater(()-> {
Platform.runLater(()-> {
Platform.runLater(()->{
Platform.runLater(()->{
loginAsNewUserButton.setDisable(false);
loginAsNewUserButton.setDisable(false);
logoutButton.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());
setServerSettingsButtonBar(loginAsNewUserButton, logoutButton);
setServerSettingsButtonBar(loginAsNewUserButton, logoutButton);
}
}
@Override
@Override
public void initProperties() throws Exception
public void initProperties() throws MinorException
{
{
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.setControlType(ControlType.TEXT_FIELD_MASKED);
oAuthConsumerKeySecret.setControlType(ControlType.TEXT_FIELD_MASKED);
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();
logout();
getAuthToken();
getAuthToken();
}
}
public void logout() throws Exception
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
);
);
}
}
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 MinorException
//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 MinorException
{
Twitter twitter = tf.getInstance();
Twitter twitter = tf.getInstance();
twitter.updateStatus(getClientProperties().getSingleProperty("tweet").getStringValue());
try
{
twitter.updateStatus(
getClientProperties().getSingleProperty("tweet").getStringValue()
);
} catch (TwitterException e)
{
e.printStackTrace();
throw new MinorException("Unable to update Twitter : "+e.getMessage());
}
}
}
/*public String addRandomBlank(String value)
/*
TOP SECRET - If you found this then congrats :D (you probably know why)
public String getRandomBlanks()
{
{
return value+("⠀".repeat(new Random().nextInt(100)));
return "⠀".repeat(new Random().nextInt(100));
}*/
}*/
@Override
public void onShutDown() throws Exception {
// TODO Auto-generated method stub
}
}
}
module com.stream_pi.twitteraction {
module com.stream_pi.twitteraction
{
requires com.stream_pi.action_api;
requires com.stream_pi.action_api;
requires com.stream_pi.util;
requires com.stream_pi.util;
requires javafx.controls;
requires javafx.controls;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.ikonli.fontawesome5;
requires org.kordamp.ikonli.fontawesome5;
requires org.twitter4j.core;
requires org.twitter4j.core;
requires java.desktop;
requires java.desktop;
provides com.stream_pi.action_api.externalplugin.ExternalPlugin with com.stream_pi.twitteraction.TwitterAction;
provides com.stream_pi.action_api.externalplugin.ExternalPlugin with com.stream_pi.twitteraction.TwitterAction;
}
}