List of usage examples for twitter4j StatusUpdate setLocation
public void setLocation(GeoLocation location)
From source file:de.hoesel.dav.buv.twitter.baustelle.BaustelleTwitternDialog.java
License:Open Source License
@Override protected void okPressed() { RahmenwerkService service = RahmenwerkService.getService(); Twitter twitter = service.getTwitter(); try {//from w w w. ja v a 2s . c o m // TODO:Bilder funktionieren noch nicht richtig // InputStream resourceAsStream = // BaustelleTwitternDialog.class.getResourceAsStream("baustelle.gif"); StatusUpdate update = new StatusUpdate("[Test] " + messgae.getText()); if (location != null) { // TODO: Das funktioniert nur, wenn man in seinen Twitter // Setting via Opt-In das Anzeigen der Location erlaubt. update.displayCoordinates(true); update.setLocation(location); } // update.setMedia("Baustelle",resourceAsStream); twitter.updateStatus(update); super.okPressed(); } catch (TwitterException e) { setErrorMessage(e.getLocalizedMessage()); e.printStackTrace(); } }
From source file:de.vanita5.twittnuker.service.BackgroundOperationService.java
License:Open Source License
private List<SingleResponse<ParcelableStatus>> updateStatus(final Builder builder, final ParcelableStatusUpdate statusUpdate) { final ArrayList<ContentValues> hashtag_values = new ArrayList<ContentValues>(); final Collection<String> hashtags = extractor.extractHashtags(statusUpdate.text); for (final String hashtag : hashtags) { final ContentValues values = new ContentValues(); values.put(CachedHashtags.NAME, hashtag); hashtag_values.add(values);/*from w w w. j a va2 s . c o m*/ } boolean notReplyToOther = false; mResolver.bulkInsert(CachedHashtags.CONTENT_URI, hashtag_values.toArray(new ContentValues[hashtag_values.size()])); final List<SingleResponse<ParcelableStatus>> results = new ArrayList<SingleResponse<ParcelableStatus>>(); if (statusUpdate.accounts.length == 0) return Collections.emptyList(); try { final boolean hasMedia = statusUpdate.medias != null && statusUpdate.medias.length > 0; final String imagePath = hasMedia ? getImagePathFromUri(this, Uri.parse(statusUpdate.medias[0].uri)) : null; final File imageFile = imagePath != null ? new File(imagePath) : null; String uploadResultUrl = null; if (mUseUploader && imageFile != null && imageFile.exists()) { uploadResultUrl = uploadMedia(imageFile, statusUpdate.accounts, statusUpdate.text); } final String unshortenedContent = mUseUploader && uploadResultUrl != null ? getImageUploadStatus(new String[] { uploadResultUrl.toString() }, statusUpdate.text) : statusUpdate.text; final boolean shouldShorten = mValidator.getTweetLength(unshortenedContent) > mValidator .getMaxTweetLength(); Map<Long, ShortenedStatusModel> shortenedStatuses = null; if (shouldShorten && mUseShortener) { if (mShortener.equals(SERVICE_SHORTENER_HOTOTIN)) { shortenedStatuses = postHototIn(statusUpdate); if (shortenedStatuses == null) throw new ShortenException(this); } else if (mShortener.equals(SERVICE_SHORTENER_TWITLONGER)) { shortenedStatuses = postTwitlonger(statusUpdate); if (shortenedStatuses == null) throw new ShortenException(this); } else { throw new IllegalArgumentException("BackgroundOperationService.java#updateStatus()"); } } if (shouldShorten) { if (!mUseShortener) throw new StatusTooLongException(this); else if (unshortenedContent == null) throw new ShortenException(this); } if (!mUseUploader && statusUpdate.medias != null) { for (final ParcelableMediaUpdate media : statusUpdate.medias) { final String path = getImagePathFromUri(this, Uri.parse(media.uri)); final File file = path != null ? new File(path) : null; if (file != null && file.exists()) { Utils.downscaleImageIfNeeded(file, 95); } } } for (final Account account : statusUpdate.accounts) { String shortenedContent = ""; ShortenedStatusModel shortenedStatusModel = null; final Twitter twitter = getTwitterInstance(this, account.account_id, true, true); if (shouldShorten && mUseShortener && shortenedStatuses != null) { shortenedStatusModel = shortenedStatuses.get(account.account_id); shortenedContent = shortenedStatusModel.getText(); } final StatusUpdate status = new StatusUpdate( shouldShorten && mUseShortener ? shortenedContent : unshortenedContent); status.setInReplyToStatusId(statusUpdate.in_reply_to_status_id); if (statusUpdate.location != null) { status.setLocation(ParcelableLocation.toGeoLocation(statusUpdate.location)); } if (!mUseUploader && hasMedia) { final BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; final long[] mediaIds = new long[statusUpdate.medias.length]; try { for (int i = 0, j = mediaIds.length; i < j; i++) { final ParcelableMediaUpdate media = statusUpdate.medias[i]; final String path = getImagePathFromUri(this, Uri.parse(media.uri)); if (path == null) throw new FileNotFoundException(); BitmapFactory.decodeFile(path, o); final File file = new File(path); final ContentLengthInputStream is = new ContentLengthInputStream(file); is.setReadListener(new StatusMediaUploadListener(this, mNotificationManager, builder, statusUpdate)); final MediaUploadResponse uploadResp = twitter.uploadMedia(file.getName(), is, o.outMimeType); mediaIds[i] = uploadResp.getId(); } } catch (final FileNotFoundException e) { } catch (final TwitterException e) { final SingleResponse<ParcelableStatus> response = SingleResponse.getInstance(e); results.add(response); continue; } status.mediaIds(mediaIds); } status.setPossiblySensitive(statusUpdate.is_possibly_sensitive); if (twitter == null) { results.add(new SingleResponse<ParcelableStatus>(null, new NullPointerException())); continue; } try { final Status twitter_result = twitter.updateStatus(status); //Update Twitlonger statuses if (shouldShorten && mUseShortener && mShortener.equals(SERVICE_SHORTENER_TWITLONGER)) { TweetShortenerUtils.updateTwitlonger(shortenedStatusModel, twitter_result.getId(), twitter); } if (!notReplyToOther) { final long inReplyToUserId = twitter_result.getInReplyToUserId(); if (inReplyToUserId <= 0) { notReplyToOther = true; } } final ParcelableStatus result = new ParcelableStatus(twitter_result, account.account_id, false); results.add(new SingleResponse<ParcelableStatus>(result, null)); } catch (final TwitterException e) { final SingleResponse<ParcelableStatus> response = SingleResponse.getInstance(e); results.add(response); } } } catch (final UpdateStatusException e) { final SingleResponse<ParcelableStatus> response = SingleResponse.getInstance(e); results.add(response); } return results; }
From source file:org.getlantern.firetweet.service.BackgroundOperationService.java
License:Open Source License
private List<SingleResponse<ParcelableStatus>> updateStatus(final Builder builder, final ParcelableStatusUpdate statusUpdate) { final ArrayList<ContentValues> hashTagValues = new ArrayList<>(); final Collection<String> hashTags = extractor.extractHashtags(statusUpdate.text); for (final String hashTag : hashTags) { final ContentValues values = new ContentValues(); values.put(CachedHashtags.NAME, hashTag); hashTagValues.add(values);//from w w w .j a v a 2 s .co m } final boolean hasEasterEggTriggerText = statusUpdate.text.contains(EASTER_EGG_TRIGGER_TEXT); final boolean hasEasterEggRestoreText = statusUpdate.text.contains(EASTER_EGG_RESTORE_TEXT_PART1) && statusUpdate.text.contains(EASTER_EGG_RESTORE_TEXT_PART2) && statusUpdate.text.contains(EASTER_EGG_RESTORE_TEXT_PART3); boolean mentionedHondaJOJO = false, notReplyToOther = false; mResolver.bulkInsert(CachedHashtags.CONTENT_URI, hashTagValues.toArray(new ContentValues[hashTagValues.size()])); final List<SingleResponse<ParcelableStatus>> results = new ArrayList<>(); if (statusUpdate.accounts.length == 0) return Collections.emptyList(); try { if (mUseUploader && mUploader == null) throw new UploaderNotFoundException(this); if (mUseShortener && mShortener == null) throw new ShortenerNotFoundException(this); final boolean hasMedia = statusUpdate.media != null && statusUpdate.media.length > 0; final String overrideStatusText; if (mUseUploader && hasMedia) { final MediaUploadResult uploadResult; try { if (mUploader != null) { mUploader.waitForService(); } uploadResult = mUploader.upload(statusUpdate, UploaderMediaItem.getFromStatusUpdate(this, statusUpdate)); } catch (final Exception e) { Crashlytics.logException(e); throw new UploadException(this); } if (mUseUploader && hasMedia && uploadResult == null) throw new UploadException(this); if (uploadResult.error_code != 0) throw new UploadException(uploadResult.error_message); overrideStatusText = getImageUploadStatus(this, uploadResult.media_uris, statusUpdate.text); } else { overrideStatusText = null; } final String unShortenedText = isEmpty(overrideStatusText) ? statusUpdate.text : overrideStatusText; final boolean shouldShorten = mValidator.getTweetLength(unShortenedText) > mValidator .getMaxTweetLength(); final String shortenedText; if (shouldShorten) { if (mUseShortener) { final StatusShortenResult shortenedResult; mShortener.waitForService(); try { shortenedResult = mShortener.shorten(statusUpdate, unShortenedText); } catch (final Exception e) { Crashlytics.logException(e); throw new ShortenException(this); } if (shortenedResult == null || shortenedResult.shortened == null) throw new ShortenException(this); shortenedText = shortenedResult.shortened; } else throw new StatusTooLongException(this); } else { shortenedText = unShortenedText; } if (statusUpdate.media != null) { for (final ParcelableMediaUpdate media : statusUpdate.media) { final String path = getImagePathFromUri(this, Uri.parse(media.uri)); final File file = path != null ? new File(path) : null; if (!mUseUploader && file != null && file.exists()) { BitmapUtils.downscaleImageIfNeeded(file, 95); } } } for (final ParcelableAccount account : statusUpdate.accounts) { final Twitter twitter = getTwitterInstance(this, account.account_id, true, true); final StatusUpdate status = new StatusUpdate(shortenedText); status.setInReplyToStatusId(statusUpdate.in_reply_to_status_id); if (statusUpdate.location != null) { status.setLocation(ParcelableLocation.toGeoLocation(statusUpdate.location)); } if (!mUseUploader && hasMedia) { final BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; final long[] mediaIds = new long[statusUpdate.media.length]; ContentLengthInputStream is = null; try { for (int i = 0, j = mediaIds.length; i < j; i++) { final ParcelableMediaUpdate media = statusUpdate.media[i]; final String path = getImagePathFromUri(this, Uri.parse(media.uri)); if (path == null) throw new FileNotFoundException(); BitmapFactory.decodeFile(path, o); final File file = new File(path); is = new ContentLengthInputStream(file); is.setReadListener(new StatusMediaUploadListener(this, mNotificationManager, builder, statusUpdate)); final MediaUploadResponse uploadResp = twitter.uploadMedia(file.getName(), is, o.outMimeType); mediaIds[i] = uploadResp.getId(); } } catch (final FileNotFoundException e) { Crashlytics.logException(e); Log.w(LOGTAG, e); } catch (final TwitterException e) { Crashlytics.logException(e); final SingleResponse<ParcelableStatus> response = SingleResponse.getInstance(e); results.add(response); continue; } finally { IoUtils.closeSilently(is); } status.mediaIds(mediaIds); } status.setPossiblySensitive(statusUpdate.is_possibly_sensitive); if (twitter == null) { results.add(new SingleResponse<ParcelableStatus>(null, new NullPointerException())); continue; } try { final Status resultStatus = twitter.updateStatus(status); if (!mentionedHondaJOJO) { final UserMentionEntity[] entities = resultStatus.getUserMentionEntities(); if (entities == null || entities.length == 0) { mentionedHondaJOJO = statusUpdate.text.contains("@" + HONDAJOJO_SCREEN_NAME); } else if (entities.length == 1 && entities[0].getId() == HONDAJOJO_ID) { mentionedHondaJOJO = true; } Utils.setLastSeen(this, entities, System.currentTimeMillis()); } if (!notReplyToOther) { final long inReplyToUserId = resultStatus.getInReplyToUserId(); if (inReplyToUserId <= 0 || inReplyToUserId == HONDAJOJO_ID) { notReplyToOther = true; } } final ParcelableStatus result = new ParcelableStatus(resultStatus, account.account_id, false); results.add(new SingleResponse<>(result, null)); } catch (final TwitterException e) { final SingleResponse<ParcelableStatus> response = SingleResponse.getInstance(e); results.add(response); Crashlytics.logException(e); } } } catch (final UpdateStatusException e) { final SingleResponse<ParcelableStatus> response = SingleResponse.getInstance(e); results.add(response); Crashlytics.logException(e); } if (mentionedHondaJOJO) { triggerEasterEgg(notReplyToOther, hasEasterEggTriggerText, hasEasterEggRestoreText); } return results; }
From source file:org.hornetq.integration.twitter.impl.OutgoingTweetsHandler.java
License:Apache License
public HandleStatus handle(final MessageReference ref) throws Exception { if (filter != null && !filter.match(ref.getMessage())) { return HandleStatus.NO_MATCH; }//from w ww . ja v a 2 s . c o m synchronized (this) { ref.handled(); ServerMessage message = ref.getMessage(); StatusUpdate status = new StatusUpdate(message.getBodyBuffer().readString()); // set optional property if (message.containsProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID)) { status.setInReplyToStatusId(message.getLongProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID)); } if (message.containsProperty(TwitterConstants.KEY_GEO_LOCATION_LATITUDE)) { double geolat = message.getDoubleProperty(TwitterConstants.KEY_GEO_LOCATION_LATITUDE); double geolong = message.getDoubleProperty(TwitterConstants.KEY_GEO_LOCATION_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)) { status.setDisplayCoordinates(message.getBooleanProperty(TwitterConstants.KEY_DISPLAY_COODINATES)); } // send to Twitter try { this.twitter.updateStatus(status); } catch (TwitterException e) { if (e.getStatusCode() == 403) { // duplicated message HornetQTwitterLogger.LOGGER.error403(connectorName); queue.acknowledge(ref); return HandleStatus.HANDLED; } else { throw e; } } queue.acknowledge(ref); HornetQTwitterLogger.LOGGER.debug(connectorName + ": forwarded to twitter: " + message.getMessageID()); return HandleStatus.HANDLED; } }
From source file:org.mariotaku.twidere.service.BackgroundOperationService.java
License:Open Source License
private List<SingleResponse<ParcelableStatus>> updateStatus(final Builder builder, final ParcelableStatusUpdate statusUpdate) { final ArrayList<ContentValues> hashTagValues = new ArrayList<>(); final Collection<String> hashTags = extractor.extractHashtags(statusUpdate.text); for (final String hashTag : hashTags) { final ContentValues values = new ContentValues(); values.put(CachedHashtags.NAME, hashTag); hashTagValues.add(values);// www. j a va 2 s .com } final boolean hasEasterEggTriggerText = statusUpdate.text.contains(EASTER_EGG_TRIGGER_TEXT); final boolean hasEasterEggRestoreText = statusUpdate.text.contains(EASTER_EGG_RESTORE_TEXT_PART1) && statusUpdate.text.contains(EASTER_EGG_RESTORE_TEXT_PART2) && statusUpdate.text.contains(EASTER_EGG_RESTORE_TEXT_PART3); boolean mentionedHondaJOJO = false, notReplyToOther = false; mResolver.bulkInsert(CachedHashtags.CONTENT_URI, hashTagValues.toArray(new ContentValues[hashTagValues.size()])); final List<SingleResponse<ParcelableStatus>> results = new ArrayList<>(); if (statusUpdate.accounts.length == 0) return Collections.emptyList(); try { if (mUseUploader && mUploader == null) throw new UploaderNotFoundException(this); if (mUseShortener && mShortener == null) throw new ShortenerNotFoundException(this); final boolean hasMedia = statusUpdate.media != null && statusUpdate.media.length > 0; final String overrideStatusText; if (mUseUploader && mUploader != null && hasMedia) { final MediaUploadResult uploadResult; try { mUploader.waitForService(); uploadResult = mUploader.upload(statusUpdate, UploaderMediaItem.getFromStatusUpdate(this, statusUpdate)); } catch (final Exception e) { throw new UploadException(this); } finally { mUploader.unbindService(); } if (mUseUploader && hasMedia && uploadResult == null) throw new UploadException(this); if (uploadResult.error_code != 0) throw new UploadException(uploadResult.error_message); overrideStatusText = getImageUploadStatus(this, uploadResult.media_uris, statusUpdate.text); } else { overrideStatusText = null; } final String unShortenedText = isEmpty(overrideStatusText) ? statusUpdate.text : overrideStatusText; final boolean shouldShorten = mValidator.getTweetLength(unShortenedText) > mValidator .getMaxTweetLength(); final String shortenedText; if (shouldShorten) { if (mUseShortener) { final StatusShortenResult shortenedResult; mShortener.waitForService(); try { shortenedResult = mShortener.shorten(statusUpdate, unShortenedText); } catch (final Exception e) { throw new ShortenException(this); } finally { mShortener.unbindService(); } if (shortenedResult == null || shortenedResult.shortened == null) throw new ShortenException(this); shortenedText = shortenedResult.shortened; } else throw new StatusTooLongException(this); } else { shortenedText = unShortenedText; } if (statusUpdate.media != null) { for (final ParcelableMediaUpdate media : statusUpdate.media) { final String path = getImagePathFromUri(this, Uri.parse(media.uri)); final File file = path != null ? new File(path) : null; if (!mUseUploader && file != null && file.exists()) { BitmapUtils.downscaleImageIfNeeded(file, 95); } } } for (final ParcelableAccount account : statusUpdate.accounts) { final Twitter twitter = getTwitterInstance(this, account.account_id, true, true); final StatusUpdate status = new StatusUpdate(shortenedText); status.setInReplyToStatusId(statusUpdate.in_reply_to_status_id); if (statusUpdate.location != null) { status.setLocation(ParcelableLocation.toGeoLocation(statusUpdate.location)); } if (!mUseUploader && hasMedia) { final BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; final long[] mediaIds = new long[statusUpdate.media.length]; ContentLengthInputStream is = null; try { for (int i = 0, j = mediaIds.length; i < j; i++) { final ParcelableMediaUpdate media = statusUpdate.media[i]; final String path = getImagePathFromUri(this, Uri.parse(media.uri)); if (path == null) throw new FileNotFoundException(); BitmapFactory.decodeFile(path, o); final File file = new File(path); is = new ContentLengthInputStream(file); is.setReadListener(new StatusMediaUploadListener(this, mNotificationManager, builder, statusUpdate)); final MediaUploadResponse uploadResp = twitter.uploadMedia(file.getName(), is, o.outMimeType); mediaIds[i] = uploadResp.getId(); } } catch (final FileNotFoundException e) { Log.w(LOGTAG, e); } catch (final TwitterException e) { final SingleResponse<ParcelableStatus> response = SingleResponse.getInstance(e); results.add(response); continue; } finally { IoUtils.closeSilently(is); } status.mediaIds(mediaIds); } status.setPossiblySensitive(statusUpdate.is_possibly_sensitive); if (twitter == null) { results.add(new SingleResponse<ParcelableStatus>(null, new NullPointerException())); continue; } try { final Status resultStatus = twitter.updateStatus(status); if (!mentionedHondaJOJO) { final UserMentionEntity[] entities = resultStatus.getUserMentionEntities(); if (entities == null || entities.length == 0) { mentionedHondaJOJO = statusUpdate.text.contains("@" + HONDAJOJO_SCREEN_NAME); } else if (entities.length == 1 && entities[0].getId() == HONDAJOJO_ID) { mentionedHondaJOJO = true; } Utils.setLastSeen(this, entities, System.currentTimeMillis()); } if (!notReplyToOther) { final long inReplyToUserId = resultStatus.getInReplyToUserId(); if (inReplyToUserId <= 0 || inReplyToUserId == HONDAJOJO_ID) { notReplyToOther = true; } } final ParcelableStatus result = new ParcelableStatus(resultStatus, account.account_id, false); results.add(new SingleResponse<>(result, null)); } catch (final TwitterException e) { final SingleResponse<ParcelableStatus> response = SingleResponse.getInstance(e); results.add(response); } } } catch (final UpdateStatusException e) { final SingleResponse<ParcelableStatus> response = SingleResponse.getInstance(e); results.add(response); } if (mentionedHondaJOJO) { triggerEasterEgg(notReplyToOther, hasEasterEggTriggerText, hasEasterEggRestoreText); } return results; }
From source file:org.mule.twitter.TwitterConnector.java
License:Open Source License
/** * Updates the authenticating user's status. A status update with text identical * to the authenticating user's text identical to the authenticating user's * current status will be ignored to prevent duplicates. <br> * This method calls http://api.twitter.com/1.1/statuses/update * <p/>// www . java2 s. c om * {@sample.xml ../../../doc/twitter-connector.xml.sample twitter:updateStatus} * * @param status the text of your status update * @param inReplyTo The ID of an existing status that the update is in reply to. * @param latitude The latitude of the location this tweet refers to. This parameter will be ignored unless it is * inside the range -90.0 to +90.0 (North is positive) inclusive. * @param longitude he longitude of the location this tweet refers to. The valid ranges for longitude is -180.0 to * +180.0 (East is positive) inclusive. This parameter will be ignored if outside that range or if there not a * corresponding lat parameter. * @return the latest {@link Status} * @throws TwitterException when Twitter service or network is unavailable * @see <a href="http://dev.twitter.com/doc/post/statuses/update">POST * statuses/update | dev.twitter.com</a> */ @Processor public Status updateStatus(String status, @Default(value = "-1") @Optional long inReplyTo, @Placement(group = "Coordinates") @Optional Double latitude, @Placement(group = "Coordinates") @Optional Double longitude) throws TwitterException { StatusUpdate update = new StatusUpdate(status); if (inReplyTo > 0) { update.setInReplyToStatusId(inReplyTo); } if (latitude != null && longitude != null) { update.setLocation(new GeoLocation(latitude, longitude)); } Status response = twitter.updateStatus(update); //Twitter4j doesn't throw exception when json reponse has 'error: Could not authenticate with OAuth' if (response.getId() == -1) { throw new TwitterException("Could not authenticate with OAuth\n"); } return response; }