Search in sources :

Example 1 with StatusUpdate

use of twitter4j.StatusUpdate in project intellij-community by JetBrains.

the class StudyTwitterUtils method updateStatus.

/**
   * Post on twitter media and text from panel
   * @param panel shown to user and used to provide data to post 
   */
public static void updateStatus(StudyTwitterUtils.TwitterDialogPanel panel, Twitter twitter) throws IOException, TwitterException {
    StatusUpdate update = new StatusUpdate(panel.getMessage());
    InputStream e = panel.getMediaSource();
    if (e != null) {
        File imageFile = FileUtil.createTempFile("twitter_media", panel.getMediaExtension());
        FileUtil.copy(e, new FileOutputStream(imageFile));
        update.media(imageFile);
    }
    twitter.updateStatus(update);
    BrowserUtil.browse("https://twitter.com/");
}
Also used : StatusUpdate(twitter4j.StatusUpdate) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 2 with StatusUpdate

use of twitter4j.StatusUpdate in project camel by apache.

the class UserProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    // update user's status
    Object in = exchange.getIn().getBody();
    Status response;
    if (in instanceof StatusUpdate) {
        response = updateStatus((StatusUpdate) in);
    } else {
        String s = exchange.getIn().getMandatoryBody(String.class);
        response = updateStatus(s);
    }
    /*
         * Support the InOut exchange pattern in order to provide access to
         * the unique identifier for the published tweet which is returned in the response
         * by the Twitter REST API: https://dev.twitter.com/docs/api/1/post/statuses/update
         */
    if (exchange.getPattern().isOutCapable()) {
        // here we just copy the header of in message to the out message
        exchange.getOut().copyFrom(exchange.getIn());
        exchange.getOut().setBody(response);
    }
}
Also used : Status(twitter4j.Status) StatusUpdate(twitter4j.StatusUpdate)

Example 3 with StatusUpdate

use of twitter4j.StatusUpdate in project twicalico by moko256.

the class PostTweetModelImpl method postTweet.

@Override
public Single<Status> postTweet() {
    return Single.create(subscriber -> {
        try {
            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);
            }
            subscriber.onSuccess(twitter.updateStatus(statusUpdate));
        } catch (FileNotFoundException | TwitterException e) {
            subscriber.onError(e);
        }
    });
}
Also used : StatusUpdate(twitter4j.StatusUpdate) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Uri(android.net.Uri) TwitterException(twitter4j.TwitterException)

Example 4 with StatusUpdate

use of twitter4j.StatusUpdate in project SilicaGel by Siketyan.

the class NotificationService method onNotificationPosted.

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Log.d(LOG_TAG, "[Notification] " + sbn.getPackageName());
    String player = getPlayer(sbn.getPackageName());
    if (player == null)
        return;
    try {
        final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        if (!pref.getBoolean("monitor_notifications", true))
            return;
        final Bundle extras = sbn.getNotification().extras;
        String title = "";
        String artist = "";
        String album = "";
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        try {
            title = extras.getCharSequence(Notification.EXTRA_TITLE).toString();
            artist = extras.getCharSequence(Notification.EXTRA_TEXT).toString();
            album = extras.getCharSequence(Notification.EXTRA_SUB_TEXT).toString();
        } catch (NullPointerException e) {
            Log.d(LOG_TAG, "[Error] Empty title, artist or album was provided.");
        }
        if (title == null || title.isEmpty())
            return;
        Log.d(LOG_TAG, "[Playing] " + title + " - " + artist + " (" + album + ") on " + player);
        String tweetText = pref.getString("template", "").replaceAll("%title%", title).replaceAll("%artist%", artist).replaceAll("%album%", album).replaceAll("%player%", player);
        if (tweetText.equals(previous))
            return;
        previous = tweetText;
        tweetText = tweetText.replaceAll("%y%", String.format("%4d", year)).replaceAll("%m%", String.format("%2d", month)).replaceAll("%d%", String.format("%2d", day)).replaceAll("%h%", String.format("%02d", hour)).replaceAll("%i%", String.format("%02d", minute)).replaceAll("%s%", String.format("%02d", second));
        Log.d(LOG_TAG, "[Tweeting] " + tweetText);
        AsyncTask<String, Void, Boolean> task = new AsyncTask<String, Void, Boolean>() {

            @Override
            protected Boolean doInBackground(String... params) {
                try {
                    Twitter twitter = TwitterUtil.getTwitterInstance(getInstance());
                    ByteArrayInputStream bs = null;
                    if (pref.getBoolean("with_cover", false)) {
                        try {
                            Bitmap thumb = (Bitmap) extras.get(Notification.EXTRA_LARGE_ICON);
                            if (thumb == null)
                                thumb = (Bitmap) extras.get(Notification.EXTRA_LARGE_ICON_BIG);
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            thumb.compress(Bitmap.CompressFormat.PNG, 0, bos);
                            byte[] bitmap = bos.toByteArray();
                            bs = new ByteArrayInputStream(bitmap);
                        } catch (Exception e) {
                            notifyException(NotificationService.this, e);
                        }
                    }
                    if (bs != null) {
                        twitter.updateStatus(new StatusUpdate(params[0]).media("cover.png", bs));
                    } else {
                        twitter.updateStatus(params[0]);
                    }
                    Log.d(LOG_TAG, "[Tweeted] " + params[0]);
                    return true;
                } catch (Exception e) {
                    notifyException(NotificationService.this, e);
                    e.printStackTrace();
                    Log.d(LOG_TAG, "[Error] Failed to tweet.");
                    return false;
                }
            }

            @Override
            protected void onPostExecute(Boolean b) {
                if (pref.getBoolean("notify_tweeted", true)) {
                    Toast.makeText(NotificationService.this, R.string.tweeted, Toast.LENGTH_SHORT).show();
                }
            }
        };
        task.execute(tweetText);
    } catch (Exception e) {
        notifyException(this, e);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) Calendar(java.util.Calendar) AsyncTask(android.os.AsyncTask) Twitter(twitter4j.Twitter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StatusUpdate(twitter4j.StatusUpdate) Bitmap(android.graphics.Bitmap) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 5 with StatusUpdate

use of twitter4j.StatusUpdate in project jbpm-work-items by kiegroup.

the class UpdateStatusWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    String statusMessage = (String) workItem.getParameter("StatusUpdate");
    // media is optional
    Document statusMedia = null;
    if (workItem.getParameter("Media") != null) {
        statusMedia = (Document) workItem.getParameter("Media");
    }
    // debug is optional (default to false)
    boolean debugOption = false;
    if (workItem.getParameter("DebugEnabled") != null) {
        debugOption = Boolean.parseBoolean((String) workItem.getParameter("DebugEnabled"));
    }
    if (StringUtils.isNotEmpty(statusMessage)) {
        try {
            Twitter twitter = auth.getTwitterService(this.consumerKey, this.consumerSecret, this.accessKey, this.accessSecret, debugOption);
            statusUpdate = new StatusUpdate(statusMessage);
            if (statusMedia != null) {
                statusUpdate.setMedia(FilenameUtils.getBaseName(statusMedia.getName()) + "." + FilenameUtils.getExtension(statusMedia.getName()), new ByteArrayInputStream(statusMedia.getContent()));
            }
            twitter.updateStatus(statusUpdate);
            workItemManager.completeWorkItem(workItem.getId(), null);
        } catch (Exception e) {
            handleException(e);
        }
    } else {
        logger.error("Missing status message.");
        throw new IllegalArgumentException("Missing status message.");
    }
}
Also used : StatusUpdate(twitter4j.StatusUpdate) ByteArrayInputStream(java.io.ByteArrayInputStream) Twitter(twitter4j.Twitter) Document(org.jbpm.document.Document)

Aggregations

StatusUpdate (twitter4j.StatusUpdate)12 Status (twitter4j.Status)4 InputStream (java.io.InputStream)3 Twitter (twitter4j.Twitter)3 SharedPreferences (android.content.SharedPreferences)2 Bitmap (android.graphics.Bitmap)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)2 TestWorkItemManager (org.jbpm.process.workitem.core.TestWorkItemManager)2 AbstractBaseTest (org.jbpm.test.AbstractBaseTest)2 Test (org.junit.Test)2 TwitterException (twitter4j.TwitterException)2 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 AsyncTask (android.os.AsyncTask)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1