Skip to content

Commit

Permalink
Fixes based on review
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Hanson <scooter_seh@yahoo.com>
  • Loading branch information
computergeek1507 committed Jul 13, 2021
1 parent 82daa7a commit 81bef74
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
12 changes: 6 additions & 6 deletions bundles/org.openhab.binding.twitter/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Twitter Binding

The Twitter binding allows your home to Tweet, 280 characters at a time. It also supports direct messages and tweeting with media.
The Twitter binding allows your home to Tweet 280 characters at a time. It also supports direct messages and tweeting with media.

## Supported Things

```
Account - Twitter Account.
account - Twitter Account.
```

## Thing Configuration

The Twitter Account Thing requires you create a Twitter App in the Twitter Developer Page.
The Twitter Account Thing requires you to create a Twitter App in the Twitter Developer Page.

| Property | Default | Required | Description |
|-------------------|---------|:--------:|-----------------------------------|
Expand All @@ -24,10 +24,10 @@ The Twitter Account Thing requires you create a Twitter App in the Twitter Devel

## Channels

```
Last Tweet - Latest Tweet.
| channel | type | description |
|------------|--------|--------------------------------------------|
| lasttweet | String | This channel provides Latest Tweet message |

```

## Full Example

Expand Down
14 changes: 13 additions & 1 deletion bundles/org.openhab.binding.twitter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.2.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.twitter</artifactId>
Expand All @@ -25,5 +25,17 @@
<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 @@ -15,9 +15,11 @@
import static org.openhab.binding.twitter.internal.TwitterBindingConstants.CHANNEL_LASTTWEET;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ScheduledFuture;
Expand All @@ -31,6 +33,8 @@
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.twitter.internal.action.TwitterActions;
import org.openhab.binding.twitter.internal.config.TwitterConfig;
import org.openhab.core.io.net.http.HttpUtil;
import org.openhab.core.library.types.RawType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -92,9 +96,10 @@ public void initialize() {

try {
// create a New Twitter Client
client = createClient();
Twitter localClient = createClient();
// verify client is valid
if (client != null) {
if (localClient != null) {
client = localClient;
refresh();// Get latest status
isProperlyConfigured = true;
refreshTask = scheduler.scheduleWithFixedDelay(this::refresh, 0, config.refresh, TimeUnit.MINUTES);
Expand All @@ -107,6 +112,14 @@ public void initialize() {
}
}

@Override
public void dispose() {
ScheduledFuture<?> localRefreshTask = refreshTask;
if (localRefreshTask != null) {
localRefreshTask.cancel(true);
}
}

/**
* Internal method for Getting Twitter Status
*
Expand All @@ -125,7 +138,7 @@ private void refresh() {
logger.debug("No Statuses Found");
}
} catch (TwitterException e) {
logger.info("Error when trying to refresh Twitter Account: {}", e.getMessage());
logger.debug("Error when trying to refresh Twitter Account: {}", e.getMessage());
}
}

Expand Down Expand Up @@ -200,19 +213,31 @@ public boolean sendTweet(String tweetTxt, String tweetPicture) {
File fileToAttach = null;
boolean deleteTemporaryFile = false;
if (StringUtils.startsWith(tweetPicture, "http://") || StringUtils.startsWith(tweetPicture, "https://")) {
// we have a remote url and need to download the remote file to a temporary location
String tDir = System.getProperty("java.io.tmpdir");
String path = tDir + File.separator + "openhab-twitter-remote_attached_file" + "."
+ FilenameUtils.getExtension(tweetPicture);

try {
URL url = new URL(tweetPicture);
// 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);

// URL url = new URL(tweetPicture);
fileToAttach = new File(path);
deleteTemporaryFile = true;
FileUtils.copyURLToFile(url, fileToAttach);
} catch (MalformedURLException e) {
logger.warn("Can't read file from '{}'", tweetPicture, e);
} catch (IOException e) {
logger.warn("Can't save file from '{}' to '{}'", tweetPicture, path, e);

RawType tweet_picture = HttpUtil.downloadImage(tweetPicture);
if (tweet_picture != null) {
try (FileOutputStream fos = new FileOutputStream(path)) {
fos.write(tweet_picture.getBytes(), 0, tweet_picture.getBytes().length);
} catch (FileNotFoundException ex) {
logger.debug("Could not create {} in temp dir. {}", path, ex.getMessage());
} catch (IOException ex) {
logger.debug("Could not write {} to temp dir. {}", path, ex.getMessage());
}
} else {
logger.debug("Could not download tweet file from {}", tweetPicture);
}
} catch (IOException ex) {
logger.debug("Could not write {} to temp dir. {}", tweetPicture, ex.getMessage());
}
} else {
// we have a local file and can just use it directly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class TwitterActions implements ThingActions {

final TwitterHandler handler = this.handler;
if (handler == null) {
logger.info("Handler is null, cannot tweet.");
logger.debug("Handler is null, cannot tweet.");
return false;
} else {
return handler.sendTweet(text);
Expand All @@ -68,7 +68,7 @@ public class TwitterActions implements ThingActions {

final TwitterHandler handler = this.handler;
if (handler == null) {
logger.info("Handler is null, cannot tweet.");
logger.debug("Handler is null, cannot tweet.");
return false;
} else {
return handler.sendTweet(text, urlString);
Expand All @@ -90,7 +90,7 @@ public class TwitterActions implements ThingActions {

final TwitterHandler handler = this.handler;
if (handler == null) {
logger.info("Handler is null, cannot tweet.");
logger.debug("Handler is null, cannot tweet.");
return false;
} else {
return handler.sendDirectMessage(recipient, text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd">

<name>Twitter Binding</name>
<description>This is the binding for Twitter.</description>
<description>Supports adding Thing for getting the Last Tweet. Send Tweets and Pictures with Actions.</description>

</binding:binding>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<thing-type id="account">
<label>Twitter Account</label>
<description>Use for sending Tweets</description>
<description>Account uses for sending Tweets</description>

<channels>
<channel id="lasttweet" typeId="lasttweet"/>
Expand Down Expand Up @@ -39,7 +39,7 @@

<channel-type id="lasttweet">
<item-type>String</item-type>
<label>Twitter Last Tweet</label>
<label>Last Tweet</label>
<description>Users Last Tweet</description>
<state readOnly="true"/>
</channel-type>
Expand Down

0 comments on commit 81bef74

Please sign in to comment.