Example usage for twitter4j StatusUpdate setLocation

List of usage examples for twitter4j StatusUpdate setLocation

Introduction

In this page you can find the example usage for twitter4j StatusUpdate setLocation.

Prototype

public void setLocation(GeoLocation location) 

Source Link

Usage

From source file:br.com.porcelli.hornetq.integration.twitter.outgoing.impl.OutgoingTwitterHandler.java

License:Apache License

@Override
public HandleStatus handle(final MessageReference ref) {
    System.out.println("Entrando no handle()");
    synchronized (this) {
        ref.handled();/* w  w w .  j  ava  2 s.c o m*/
        final ServerMessage message = ref.getMessage();
        try {
            String text2publish;
            if (message.containsProperty(TwitterConstants.KEY_TEXT)) {
                text2publish = message.getStringProperty(TwitterConstants.KEY_TEXT);
            } else {
                text2publish = message.getBodyBuffer().readString();
            }

            if (text2publish == null || text2publish.trim().length() <= 0) {
                log.error(ERROR_MESSAGE_NOT_FILLED);
                throw new Exception(ERROR_MESSAGE_NOT_FILLED);
            } else if (text2publish.length() > 140) {
                log.warn(ERROR_MESSAGE_SIZE_EXCEED);
                text2publish = text2publish.substring(0, 139);
            }

            MessageType type = MessageType.TWEET;
            try {
                if (message.containsProperty(TwitterConstants.KEY_MSG_TYPE)) {
                    type = MessageType
                            .valueOf(message.getStringProperty(TwitterConstants.KEY_MSG_TYPE).toUpperCase());
                }
            } catch (Exception e) {
                log.warn(ERROR_INVALID_MESSAGE_TYPE);
            }

            if (type == MessageType.DM) {
                if (!message.containsProperty(TwitterConstants.KEY_TO_USER_ID)
                        && !message.containsProperty(TwitterConstants.KEY_TO_USER_SCREEN_NAME)) {
                    log.error(ERROR_DM_DESTINY_NOT_FOUND);
                    throw new Exception(ERROR_DM_DESTINY_NOT_FOUND);
                }

                DirectMessage sentMessage = null;
                if (message.containsProperty(TwitterConstants.KEY_TO_USER_ID)) {
                    int userId = -1;
                    try {
                        userId = message.getIntProperty(TwitterConstants.KEY_TO_USER_ID);
                    } catch (PropertyConversionException e) {
                        userId = Integer.valueOf(message.getStringProperty(TwitterConstants.KEY_TO_USER_ID));
                    }
                    sentMessage = twitter.sendDirectMessage(userId, text2publish);
                    dmSent.incrementAndGet();
                    totalSent.incrementAndGet();
                } else if (message.containsProperty(TwitterConstants.KEY_TO_USER_SCREEN_NAME)) {
                    String userScreenName = message.getStringProperty(TwitterConstants.KEY_TO_USER_SCREEN_NAME);
                    sentMessage = twitter.sendDirectMessage(userScreenName, text2publish);
                    dmSent.incrementAndGet();
                    totalSent.incrementAndGet();
                }
                if (sentMessage != null && sentQueue != null) {
                    final ServerMessage msg = buildMessage(sentQueue.getName().toString(), sentMessage);
                    msg.setAddress(sentQueue.getName());
                    msg.setDurable(true);
                    postOffice.route(msg, false);
                }
            } else {
                final StatusUpdate status = new StatusUpdate(text2publish);

                if (message.containsProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID)) {
                    long reply2StatusId = 0L;
                    try {
                        reply2StatusId = message.getLongProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID);
                    } catch (PropertyConversionException e) {
                        reply2StatusId = Long
                                .valueOf(message.getStringProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID));
                    }
                    status.setInReplyToStatusId(reply2StatusId);
                }

                if (message.containsProperty(TwitterConstants.KEY_GEO_LATITUDE)
                        && message.containsProperty(TwitterConstants.KEY_GEO_LONGITUDE)) {
                    double geolat = 0.0D;
                    double geolong = 0.0D;
                    try {
                        geolat = message.getDoubleProperty(TwitterConstants.KEY_GEO_LATITUDE);
                    } catch (PropertyConversionException e) {
                        geolat = Double.valueOf(message.getStringProperty(TwitterConstants.KEY_GEO_LATITUDE));
                    }
                    try {
                        geolong = message.getDoubleProperty(TwitterConstants.KEY_GEO_LONGITUDE);
                    } catch (PropertyConversionException e) {
                        geolong = Double.valueOf(message.getStringProperty(TwitterConstants.KEY_GEO_LONGITUDE));
                    }

                    status.setLocation(new GeoLocation(geolat, geolong));
                }

                if (message.containsProperty(TwitterConstants.KEY_PLACE_ID)) {
                    status.setPlaceId(message.getStringProperty(TwitterConstants.KEY_PLACE_ID));
                }

                if (message.containsProperty(TwitterConstants.KEY_DISPLAY_COODINATES)) {
                    boolean displayCoordinated = false;
                    try {
                        displayCoordinated = message
                                .getBooleanProperty(TwitterConstants.KEY_DISPLAY_COODINATES);
                    } catch (PropertyConversionException e) {
                        displayCoordinated = Boolean
                                .valueOf(message.getStringProperty(TwitterConstants.KEY_DISPLAY_COODINATES));
                    }
                    status.setDisplayCoordinates(displayCoordinated);
                }
                Status sentMessage = twitter.updateStatus(status);
                tweetSent.incrementAndGet();
                totalSent.incrementAndGet();
                if (sentMessage != null && sentQueue != null) {
                    final ServerMessage msg = buildMessage(sentQueue.getName().toString(), sentMessage);
                    msg.setAddress(sentQueue.getName());
                    msg.setDurable(true);
                    postOffice.route(msg, false);
                }
            }
        } catch (Exception e) {
            mbean.notifyException(e);
            log.error("Error sending message.", e);
            if (errorQueue != null) {
                final ServerMessage msg = message.copy();
                msg.setAddress(errorQueue.getName());
                msg.setDurable(true);
                try {
                    postOffice.route(msg, false);
                } catch (Exception e1) {
                    mbean.notifyException(e1);
                }
            }
        }
        try {
            queue.acknowledge(ref);
        } catch (Exception e) {
            mbean.notifyException(e);
            log.error("Error acknowledging message.", e);
        }
        return HandleStatus.HANDLED;
    }
}

From source file:com.daiv.android.twitter.utils.api_helper.TwitLongerHelper.java

License:Apache License

/**
 * posts the status onto Twitlonger, it then posts the shortened status (with link) to the user's twitter and updates the status on twitlonger
 * to include the posted status's id./*  ww  w .j av  a  2  s.  co m*/
 *
 * @return id of the status that was posted to twitter
 */
public long createPost() {
    TwitLongerStatus status = postToTwitLonger();
    long statusId;
    try {
        Status postedStatus;
        StatusUpdate update = new StatusUpdate(status.getText());
        if (replyToStatusId != 0) {
            update.setInReplyToStatusId(replyToStatusId);
        }

        if (location != null) {
            update.setLocation(location);
        }

        postedStatus = twitter.updateStatus(update);

        statusId = postedStatus.getId();
        updateTwitlonger(status, statusId);
    } catch (Exception e) {
        e.printStackTrace();
        statusId = 0;
    }

    // if zero, then it failed
    return statusId;
}

From source file:com.daiv.android.twitter.utils.api_helper.TwitPicHelper.java

License:Apache License

/**
 * posts the status onto Twitlonger, it then posts the shortened status (with link) to the user's twitter and updates the status on twitlonger
 * to include the posted status's id.//from   w ww .  j  a  v  a 2 s  . com
 *
 * @return id of the status that was posted to twitter
 */
public long createPost() {
    TwitPicStatus status = uploadToTwitPic();
    Log.v("Test_twitpic", "past upload");
    long statusId;
    try {
        Status postedStatus;
        StatusUpdate update = new StatusUpdate(status.getText());

        if (replyToStatusId != 0) {
            update.setInReplyToStatusId(replyToStatusId);
        }
        if (location != null) {
            update.setLocation(location);
        }

        postedStatus = twitter.updateStatus(update);

        statusId = postedStatus.getId();
    } catch (Exception e) {
        e.printStackTrace();
        statusId = 0;
    }

    // if zero, then it failed
    return statusId;
}

From source file:com.github.moko256.twitlatte.model.impl.twitter.PostTweetModelImpl.java

License:Apache License

@Override
public Completable postTweet() {
    return Completable.create(subscriber -> {
        try {/*from  w w  w  . j av  a2s. c  o  m*/
            StatusUpdate statusUpdate = new StatusUpdate(tweetText);
            if (uriList.size() > 0) {
                long ids[] = new long[uriList.size()];
                for (int i = 0; i < uriList.size(); i++) {
                    Uri uri = uriList.get(i);
                    InputStream image = contentResolver.openInputStream(uri);
                    ids[i] = twitter.uploadMedia(uri.getLastPathSegment(), image).getMediaId();
                }
                statusUpdate.setMediaIds(ids);
                statusUpdate.setPossiblySensitive(possiblySensitive);
            }
            if (isReply()) {
                statusUpdate.setInReplyToStatusId(inReplyToStatusId);
            }
            if (location != null) {
                statusUpdate.setLocation(location);
            }
            twitter.updateStatus(statusUpdate);
            subscriber.onComplete();
        } catch (FileNotFoundException | TwitterException e) {
            subscriber.tryOnError(e);
        }
    });
}

From source file:com.javielinux.api.loaders.UploadStatusLoader.java

License:Apache License

private boolean updateText(String text, long tweet_id, boolean useGeo) {
    StatusUpdate statusUpdate = new StatusUpdate(text);
    if (useGeo) {
        Location loc = LocationUtils.getLastLocation(getContext());
        GeoLocation gl = new GeoLocation(loc.getLatitude(), loc.getLongitude());
        statusUpdate.setLocation(gl);
    }//ww w. j a  va 2  s .c om
    if (tweet_id > 0)
        statusUpdate.inReplyToStatusId(tweet_id);
    try {
        twitter.updateStatus(statusUpdate);
    } catch (TwitterException e) {
        e.printStackTrace();
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.javielinux.api.loaders.UploadTwitlongerLoader.java

License:Apache License

@Override
public BaseResponse loadInBackground() {

    //TODO: Comprobar el valor devuelto con el valor esperado (error - ready) y el parmetro user_geolocation
    try {//from ww w . j a va 2  s .c o m
        Log.d(Utils.TAG, "Enviando a twitlonger: " + tweet_text);

        String textTwitLonger = "";

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.twitlonger.com/api_post");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("application", "tweettopics"));
            nameValuePairs.add(new BasicNameValuePair("api_key", "f7y8lgz31srR46sr"));
            nameValuePairs.add(new BasicNameValuePair("username", twitter.getScreenName()));
            nameValuePairs.add(new BasicNameValuePair("message", tweet_text));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            HttpResponse httpResponse = httpclient.execute(httppost);

            String xml = EntityUtils.toString(httpResponse.getEntity());

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser x = factory.newPullParser();

            x.setInput(new StringReader(xml));

            String error = "";

            int eventType = x.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    if (x.getName().equals("error")) {
                        error = x.nextText();
                    }
                    if (x.getName().equals("content")) {
                        textTwitLonger = x.nextText();
                        Log.d(Utils.TAG, "Enviando a twitter: " + textTwitLonger);
                    }
                }
                eventType = x.next();
            }

            if (!error.equals("")) {
                Log.d(Utils.TAG, "Error: " + error);
                ErrorResponse response = new ErrorResponse();
                response.setError(error);
                return response;
            }
        } catch (Exception e) {
            e.printStackTrace();
            ErrorResponse response = new ErrorResponse();
            response.setError(e, e.getMessage());
            return response;
        }

        UploadTwitlongerResponse response = new UploadTwitlongerResponse();

        if (!textTwitLonger.equals("")) {
            StatusUpdate statusUpdate = new StatusUpdate(textTwitLonger);
            if (use_geolocation) {
                Location loc = LocationUtils.getLastLocation(getContext());
                GeoLocation gl = new GeoLocation(loc.getLatitude(), loc.getLongitude());
                statusUpdate.setLocation(gl);
            }
            if (tweet_id > 0)
                statusUpdate.inReplyToStatusId(tweet_id);
            twitter.updateStatus(statusUpdate);

            response.setReady(true);
        } else {
            response.setReady(false);
        }

        return response;
    } catch (Exception e) {
        e.printStackTrace();
        ErrorResponse response = new ErrorResponse();
        response.setError(e, e.getMessage());
        return response;
    }
}

From source file:com.javielinux.task.UploadStatusAsyncTask.java

License:Apache License

private boolean updateText(String text, long tweet_id, boolean useGeo) {
    StatusUpdate su = new StatusUpdate(text);
    if (useGeo) {
        Location loc = LocationUtils.getLastLocation(mContext);
        GeoLocation gl = new GeoLocation(loc.getLatitude(), loc.getLongitude());
        su.setLocation(gl);
    }//from w w w.  j ava  2s . co m
    if (tweet_id > 0)
        su.inReplyToStatusId(tweet_id);
    try {
        twitter.updateStatus(su);
    } catch (TwitterException e) {
        e.printStackTrace();
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.javielinux.task.UploadTwitlongerAsyncTask.java

License:Apache License

@Override
protected Boolean doInBackground(String... args) {
    try {//from   w  w  w .j  a  va2  s.c  o  m
        String text = args[0];
        Log.d(Utils.TAG, "Enviando a twitlonger: " + text);
        long tweet_id = Long.parseLong(args[1]);
        boolean useGeo = args[2].equals("1") ? true : false;

        String textTwitLonger = "";

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.twitlonger.com/api_post");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("application", "tweettopics"));
            nameValuePairs.add(new BasicNameValuePair("api_key", "f7y8lgz31srR46sr"));
            nameValuePairs.add(new BasicNameValuePair("username", twitter.getScreenName()));

            //byte[] utf8Bytes = text.getBytes("UTF8");
            //String textutf8 = new String(utf8Bytes, "UTF8");
            nameValuePairs.add(new BasicNameValuePair("message", text));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            HttpResponse httpResponse = httpclient.execute(httppost);

            String xml = EntityUtils.toString(httpResponse.getEntity());
            try {

                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(true);
                XmlPullParser x = factory.newPullParser();

                x.setInput(new StringReader(xml));

                String error = "";

                int eventType = x.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    if (eventType == XmlPullParser.START_TAG) {
                        if (x.getName().equals("error")) {
                            error = x.nextText();
                        }
                        if (x.getName().equals("content")) {
                            textTwitLonger = x.nextText();
                            Log.d(Utils.TAG, "Enviando a twitter: " + textTwitLonger);
                        }
                    }
                    eventType = x.next();
                }

                if (!error.equals("")) {
                    Log.d(Utils.TAG, "Error: " + error);
                    return true;
                }

            } catch (Exception e) {
                e.printStackTrace();
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }

        if (!textTwitLonger.equals("")) {
            StatusUpdate su = new StatusUpdate(textTwitLonger);
            if (useGeo) {
                Location loc = LocationUtils.getLastLocation(mContext);
                GeoLocation gl = new GeoLocation(loc.getLatitude(), loc.getLongitude());
                su.setLocation(gl);
            }
            if (tweet_id > 0)
                su.inReplyToStatusId(tweet_id);
            twitter.updateStatus(su);
        } else {
            return true;
        }

        //} catch (TwitterException e) {
        //   e.printStackTrace();
        //   return true;
    } catch (Exception e) {
        e.printStackTrace();
        return true;
    }
    return false;
}

From source file:com.klinker.android.twitter.utils.api_helper.TwitPicHelper.java

License:Apache License

/**
 * posts the status onto Twitlonger, it then posts the shortened status (with link) to the user's twitter and updates the status on twitlonger
 * to include the posted status's id.//from   www .  j av a  2 s.c o m
 *
 * @return id of the status that was posted to twitter
 */
public long createPost() {
    TwitPicStatus status = uploadToTwitPic();
    Log.v("talon_twitpic", "past upload");
    long statusId;
    try {
        Status postedStatus;
        StatusUpdate update = new StatusUpdate(status.getText());

        if (replyToStatusId != 0) {
            update.setInReplyToStatusId(replyToStatusId);
        }
        if (location != null) {
            update.setLocation(location);
        }

        postedStatus = twitter.updateStatus(update);

        statusId = postedStatus.getId();
    } catch (Exception e) {
        e.printStackTrace();
        statusId = 0;
    }

    // if zero, then it failed
    return statusId;
}

From source file:com.vti.managers.TwitterManager.java

License:Apache License

/**
 * publish status to account vti_robot on twitter
 * // w  w  w.  ja  v  a  2s  .c  o  m
 * @param tweet
 */
public void tweet(final String tweet, GeoLocation loc) {
    try {
        StatusUpdate status = new StatusUpdate(tweet);
        status.setLocation(loc);
        twitter.updateStatus(status);
    } catch (TwitterException e) {
        Log.d(TAG, Log.stack2string(e));
    }
}