Skip to content

Commit

Permalink
remove Apache dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Hanson <[email protected]>
  • Loading branch information
computergeek1507 committed Sep 3, 2021
1 parent fdcba0c commit cef6026
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
12 changes: 0 additions & 12 deletions bundles/org.openhab.binding.twitter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,5 @@
<version>4.0.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.UnhandledException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.twitter.internal.action.TwitterActions;
Expand Down Expand Up @@ -66,8 +62,8 @@ public class TwitterHandler extends BaseThingHandler {

private final Logger logger = LoggerFactory.getLogger(TwitterHandler.class);

private @Nullable TwitterConfig config;
@SuppressWarnings("unused")
private TwitterConfig config = new TwitterConfig();

private @Nullable ScheduledFuture<?> refreshTask;

private static final int CHARACTER_LIMIT = 280;
Expand Down Expand Up @@ -107,7 +103,7 @@ public void initialize() {
} else {
updateStatus(ThingStatus.OFFLINE);
}
} catch (UnhandledException e) {
} catch (TwitterException e) {
updateStatus(ThingStatus.OFFLINE);
}
}
Expand Down Expand Up @@ -159,7 +155,7 @@ private boolean sendTweet(final String tweetTxt, final @Nullable File fileToAtta
return false;
}
// abbreviate the Tweet to meet the 280 character limit ...
String abbreviatedTweetTxt = StringUtils.abbreviate(tweetTxt, CHARACTER_LIMIT);
String abbreviatedTweetTxt = abbreviateString(tweetTxt, CHARACTER_LIMIT);
try {
// send the Tweet
StatusUpdate status = new StatusUpdate(abbreviatedTweetTxt);
Expand Down Expand Up @@ -212,12 +208,12 @@ public boolean sendTweet(String tweetTxt, String tweetPicture) {
// prepare the image attachment
File fileToAttach = null;
boolean deleteTemporaryFile = false;
if (StringUtils.startsWith(tweetPicture, "http://") || StringUtils.startsWith(tweetPicture, "https://")) {
if (tweetPicture.startsWith("http://") || tweetPicture.startsWith("https://")) {
try {
// we have a remote url and need to download the remote file to a temporary location
Path tDir = Files.createTempDirectory("TempDirectory");
String path = tDir + File.separator + "openhab-twitter-remote_attached_file" + "."
+ FilenameUtils.getExtension(tweetPicture);
+ getExtension(tweetPicture);

// URL url = new URL(tweetPicture);
fileToAttach = new File(path);
Expand Down Expand Up @@ -253,7 +249,13 @@ public boolean sendTweet(String tweetTxt, String tweetPicture) {
boolean result = sendTweet(tweetTxt, fileToAttach);
// delete temp file (if needed)
if (deleteTemporaryFile) {
FileUtils.deleteQuietly(fileToAttach);
if (fileToAttach != null) {
try {
fileToAttach.delete();
} catch (final Exception ignored) {
return false;
}
}
}
return result;
}
Expand All @@ -277,7 +279,7 @@ public boolean sendDirectMessage(String recipientId, String messageTxt) {

try {
// abbreviate the Tweet to meet the allowed character limit ...
String abbreviatedMessageTxt = StringUtils.abbreviate(messageTxt, CHARACTER_LIMIT);
String abbreviatedMessageTxt = abbreviateString(messageTxt, CHARACTER_LIMIT);
// send the direct message
DirectMessage message = client.sendDirectMessage(recipientId, abbreviatedMessageTxt);
logger.debug("Successfully sent direct message '{}' to @'{}'", message.getText(), message.getRecipientId());
Expand Down Expand Up @@ -319,4 +321,19 @@ private twitter4j.Twitter createClient() {
client.setOAuthAccessToken(new AccessToken(config.accessToken, config.accessTokenSecret));
return client;
}

public static String abbreviateString(String input, int maxLength) {
if (input.length() <= maxLength) {
return input;
} else {
return input.substring(0, maxLength);
}
}

public static String getExtension(String filename) {
if(filename.contains(".")) {
return filename.substring(filename.lastIndexOf(".") + 1));
}
return new String();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<label>Access Token Secret</label>
<context>password</context>
</parameter>
<parameter name="refresh" type="integer" required="false">
<parameter name="refresh" type="integer" required="false" unit="min" min="1">
<label>Refresh Time</label>
<description>Refresh Time for This Account in Mins</description>
<default>30</default>
Expand Down

0 comments on commit cef6026

Please sign in to comment.