Example usage for twitter4j Twitter getRetweets

List of usage examples for twitter4j Twitter getRetweets

Introduction

In this page you can find the example usage for twitter4j Twitter getRetweets.

Prototype

ResponseList<Status> getRetweets(long statusId) throws TwitterException;

Source Link

Document

Returns up to 100 of the first retweets of a given tweet.

Usage

From source file:TwitterRetrieval.java

License:Apache License

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    PrintWriter out;//from w ww . j av  a2s .co  m
    try {

        // img stuff not req'd for source code html showing
        // all links relative
        // XXX
        // making these absolute till we work out the
        // addition of a PathInfo issue

        ConfigurationBuilder cb = new ConfigurationBuilder();
        System.setProperty("twitter4j.http.httpClient", "twitter4j.internal.http.HttpClientImpl");
        cb.setOAuthConsumerKey("56NAE9lQHSOZIGXRktd5Qw")
                .setOAuthConsumerSecret("zJjJrUUs1ubwKjtPOyYzrwBJzpwq7ud8Aryq1VhYH2E")
                .setOAuthAccessTokenURL("https://api.twitter.com/oauth/access_token")
                .setOAuthRequestTokenURL("https://api.twitter.com/oauth/request_token")
                .setOAuthAuthorizationURL("https://api.twitter.com/oauth/authorize")
                .setOAuthAccessToken("234742739-I1l0VGTTjRUbZrfH1jvKnTVFU9ZEvkxxUDpvsAJ2")
                .setOAuthAccessTokenSecret("jLe3imI3JiPgmHCatt6SqYgRAcX5q8s6z38oUrqMc");

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        Query query = new Query(request.getParameter("q"));

        String tags = request.getParameter("tags");

        String zone = request.getParameter("zone");

        String users = request.getParameter("users");

        Map<String, LatLon> latslongs = new HashMap();

        String[] tagsArray = null;
        if (tags != null) {
            tagsArray = tags.split(",");
        }

        String[] userArray = null;

        if (users != null) {
            userArray = users.split(",");
            int count = userArray.length;
            usuarios = new String[count];
            latitud = new String[count];
            longitud = new String[count];
            for (int i = 0; i < userArray.length; i++) {
                temp = userArray[i];
                if (temp != null) {
                    int hit1 = temp.indexOf("[");
                    int hit2 = temp.indexOf(";");
                    int hit3 = temp.indexOf("]");
                    latslongs.put(temp.substring(0, hit1),
                            new LatLon(temp.substring(hit1 + 1, hit2), temp.substring(hit2 + 1, hit3)));
                    /*
                     * usuarios[i] = temp.substring(0, hit1); latitud[i] =
                     * temp.substring(hit1 + 1, hit2); longitud[i] =
                     * temp.substring(hit2 + 1, hit3);
                     */
                }
            }
        }

        QueryResult result;

        result = twitter.search(query);

        List<Post> postList = new ArrayList();
        List<PostType> postsList = new ArrayList();

        Post solrPost;
        PostType post;
        //List<LinkType> links = new ArrayList();
        List<ActionType> actions;
        ArrayList<User> toUsers = new ArrayList();

        ArrayList<LinkType> links;
        int d;

        InputStream stream = getServletContext().getResourceAsStream("/WEB-INF/servlet.properties");
        Properties props = null;
        if (props == null) {
            props = new Properties();
            props.load(stream);
        }
        ZoneDao zoneDao = new ZoneDao(props.getProperty("db_host"),
                Integer.valueOf(props.getProperty("db_port")), props.getProperty("db_name"));
        PlaceDao placeDao = new PlaceDao(props.getProperty("db_host"),
                Integer.valueOf(props.getProperty("db_port")), props.getProperty("db_name"));
        Place place = null;
        org.zonales.tagsAndZones.objects.Zone zoneObj = zoneDao
                .retrieveByExtendedString(Utils.normalizeZone(zone));

        for (Tweet tweet : (List<Tweet>) result.getTweets()) {
            d = MAX_TITLE_LENGTH;
            actions = new ArrayList();
            try {
                actions.add(new ActionType("retweets", twitter.getRetweets(tweet.getId()).size()));
                actions.add(new ActionType("replies",
                        twitter.getRelatedResults(tweet.getId()).getTweetsWithReply().size()));
            } catch (TwitterException ex) {
                Logger.getLogger(TwitterRetrieval.class.getName()).log(Level.SEVERE,
                        "Error intentando obtener retweets o replies: {0}", new Object[] { ex });
            }

            solrPost = new Post();
            solrPost.setZone(new Zone(String.valueOf(zoneObj.getId()), zoneObj.getName(),
                    zoneObj.getType().getName(), zoneObj.getExtendedString()));
            solrPost.setSource("Twitter");

            solrPost.setId(String.valueOf(tweet.getId()));

            if (request.getParameter(tweet.getFromUser() + "Place") != null) {
                place = placeDao.retrieveByExtendedString(request.getParameter(tweet.getFromUser() + "Place"));
            } else {
                place = null;
            }
            User usersolr = new User(String.valueOf(tweet.getFromUserId()), tweet.getFromUser(),
                    "http://twitter.com/#!/" + tweet.getFromUser(), tweet.getSource(),
                    place != null
                            ? new org.zonales.entities.Place(String.valueOf(place.getId()), place.getName(),
                                    place.getType().getName())
                            : null);

            if (users != null) {
                /*
                 * for (int i = 0; i < usuarios.length; i++) { if
                 * (tweet.getFromUser().equals(usuarios[i])) {
                 * usersolr.setLatitude(Double.parseDouble(latitud[i]));
                 * usersolr.setLongitude(Double.parseDouble(longitud[i])); }
                }
                 */
                //usersolr.setLatitude(Double.parseDouble(latslongs.get(tweet.getFromUser()).latitud));
                //usersolr.setLongitude(Double.parseDouble(latslongs.get(tweet.getFromUser()).longitud));
            }

            solrPost.setFromUser(usersolr);
            if (tweet.getToUser() != null) {
                toUsers.add(new User(String.valueOf(tweet.getToUserId()), tweet.getToUser(), null,
                        tweet.getSource(), null));
                solrPost.setToUsers(toUsers);
            }
            if (tweet.getText().length() > d) {
                while (d > 0 && tweet.getText().charAt(d - 1) != ' ') {
                    d--;
                }
            } else {
                d = tweet.getText().length() - 1;
            }
            solrPost.setTitle(tweet.getText().substring(0, d)
                    + (tweet.getText().length() > MAX_TITLE_LENGTH ? "..." : ""));
            solrPost.setText(tweet.getText());
            //post.setLinks(new LinksType(links));
            solrPost.setActions((ArrayList<ActionType>) actions);
            solrPost.setCreated(tweet.getCreatedAt().getTime());
            solrPost.setModified(tweet.getCreatedAt().getTime());
            solrPost.setRelevance(
                    actions.size() == 2 ? actions.get(0).getCant() * 3 + actions.get(1).getCant() : 0);
            solrPost.setPostLatitude(
                    tweet.getGeoLocation() != null ? tweet.getGeoLocation().getLatitude() : null);
            solrPost.setPostLongitude(
                    tweet.getGeoLocation() != null ? tweet.getGeoLocation().getLongitude() : null);

            links = new ArrayList<LinkType>();
            links.add(new LinkType("avatar", tweet.getProfileImageUrl()));
            if (tweet.getText() != null && getLinks(tweet.getText()) != null) {
                links.addAll(getLinks(tweet.getText()));
            }

            if (solrPost.getLinks() == null) {
                solrPost.setLinks(new ArrayList<LinkType>());
            }
            solrPost.setLinks(links);

            if (tagsArray != null && tagsArray.length > 0) {
                solrPost.setTags(new ArrayList<String>(Arrays.asList(tagsArray)));
            }

            solrPost.setExtendedString(WordUtils.capitalize((solrPost.getFromUser().getPlace() != null
                    ? solrPost.getFromUser().getPlace().getName() + ", "
                    : "") + solrPost.getZone().getExtendedString().replace("_", " ")));
            postList.add(solrPost);

            post = new PostType();
            post.setZone(new Zone(String.valueOf(zoneObj.getId()), zoneObj.getName(),
                    zoneObj.getType().getName(), zoneObj.getExtendedString()));
            post.setSource("Twitter");

            post.setId(String.valueOf(tweet.getId()));
            User user = new User(String.valueOf(tweet.getFromUserId()), tweet.getFromUser(),
                    "http://twitter.com/#!/" + tweet.getFromUser(), tweet.getSource(),
                    place != null
                            ? new org.zonales.entities.Place(String.valueOf(place.getId()), place.getName(),
                                    place.getType().getName())
                            : null);

            if (users != null) {
                /*
                 * for (int i = 0; i < usuarios.length; i++) { if
                 * (tweet.getFromUser().equals(usuarios[i])) {
                 * user.setLatitude(Double.parseDouble(latitud[i]));
                 * user.setLongitude(Double.parseDouble(longitud[i])); }
                }
                 */
                //user.setLatitude(Double.parseDouble(latslongs.get(tweet.getFromUser()).latitud));
                //user.setLongitude(Double.parseDouble(latslongs.get(tweet.getFromUser()).longitud));
            }

            post.setFromUser(user);

            if (tweet.getToUser() != null) {
                toUsers.add(new User(String.valueOf(tweet.getToUserId()), tweet.getToUser(), null,
                        tweet.getSource(), null));
                post.setToUsers(new ToUsersType(toUsers));
            }

            post.setTitle(tweet.getText().substring(0, d)
                    + (tweet.getText().length() > MAX_TITLE_LENGTH ? "..." : ""));
            post.setText(tweet.getText());
            //post.setLinks(new LinksType(links));
            post.setActions(new ActionsType(actions));
            post.setCreated(String.valueOf(tweet.getCreatedAt().getTime()));
            post.setModified(String.valueOf(tweet.getCreatedAt().getTime()));
            post.setRelevance(
                    actions.size() == 2 ? actions.get(0).getCant() * 3 + actions.get(1).getCant() : 0);
            post.setPostLatitude(tweet.getGeoLocation() != null ? tweet.getGeoLocation().getLatitude() : null);
            post.setPostLongitude(
                    tweet.getGeoLocation() != null ? tweet.getGeoLocation().getLongitude() : null);

            links = new ArrayList<LinkType>();
            links.add(new LinkType("avatar", tweet.getProfileImageUrl()));

            post.setLinks(new LinksType(getLinks(tweet.getText())));

            if (tagsArray != null && tagsArray.length > 0) {
                post.setTags(new TagsType(Arrays.asList(tagsArray)));
            }

            postsList.add(post);

        }
        PostsType posts = new PostsType(postsList);
        Gson gson = new Gson();
        if ("xml".equalsIgnoreCase(request.getParameter("format"))) {
            response.setContentType("application/xml");
            out = response.getWriter();
            try {
                for (PostType postIt : posts.getPost()) {
                    postIt.setVerbatim(gson.toJson(postIt));
                }
                Twitter2XML(posts, out);
            } catch (Exception ex) {
                Logger.getLogger(TwitterRetrieval.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            response.setContentType("text/javascript");
            out = response.getWriter();
            out.println("{post: " + gson.toJson(postList) + "}");
        }

    } catch (TwitterException ex) {
        Logger.getLogger(TwitterRetrieval.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.daiv.android.twitter.adapters.TimelineArrayAdapter.java

License:Apache License

public void addExpansion(final ViewHolder holder, String screenname, String users, final String[] otherLinks,
        final String webpage, final long id) {

    holder.retweet.setVisibility(View.VISIBLE);
    holder.retweetCount.setVisibility(View.VISIBLE);
    holder.favCount.setVisibility(View.VISIBLE);
    holder.favorite.setVisibility(View.VISIBLE);

    //holder.reply.setVisibility(View.GONE);
    try {/* w  ww  . ja  v a2  s  .  c om*/
        holder.replyButton.setVisibility(View.GONE);
    } catch (Exception e) {

    }

    holder.screenName = screenname;

    // used to find the other names on a tweet... could be optimized i guess, but only run when button is pressed
    final String text = holder.tweet.getText().toString();
    String extraNames = "";

    if (text.contains("@")) {
        for (String s : users.split("  ")) {
            if (!s.equals(settings.myScreenName) && !extraNames.contains(s) && !s.equals(screenname)) {
                extraNames += "@" + s + " ";
            }
        }
    }

    try {
        if (holder.retweeter.getVisibility() == View.VISIBLE && !extraNames.contains(holder.retweeterName)) {
            extraNames += "@" + holder.retweeterName + " ";
        }
    } catch (NullPointerException e) {

    }

    if (!screenname.equals(settings.myScreenName)) {
        holder.reply.setText("@" + screenname + " " + extraNames);
    } else {
        holder.reply.setText(extraNames);
    }

    holder.reply.setSelection(holder.reply.getText().length());

    if (holder.favCount.getText().toString().length() <= 2) {
        holder.favCount.setText(" -");
        holder.retweetCount.setText(" -");
    }

    //ExpansionAnimation expandAni = new ExpansionAnimation(holder.expandArea, 450);
    holder.expandArea.setVisibility(View.VISIBLE);//startAnimation(expandAni);

    getCounts(holder, id);

    holder.favorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (holder.isFavorited || !settings.crossAccActions) {
                new FavoriteStatus(holder, holder.tweetId, FavoriteStatus.TYPE_ACC_ONE).execute();
            } else if (settings.crossAccActions) {
                // dialog for favoriting
                String[] options = new String[3];

                options[0] = "@" + settings.myScreenName;
                options[1] = "@" + settings.secondScreenName;
                options[2] = context.getString(R.string.both_accounts);

                new AlertDialog.Builder(context).setItems(options, new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int item) {
                        new FavoriteStatus(holder, holder.tweetId, item + 1).execute();
                    }
                }).create().show();
            }
        }
    });

    holder.retweet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!settings.crossAccActions) {
                new RetweetStatus(holder, holder.tweetId, FavoriteStatus.TYPE_ACC_ONE).execute();
            } else {
                // dialog for favoriting
                String[] options = new String[3];

                options[0] = "@" + settings.myScreenName;
                options[1] = "@" + settings.secondScreenName;
                options[2] = context.getString(R.string.both_accounts);

                new AlertDialog.Builder(context).setItems(options, new DialogInterface.OnClickListener() {
                    public void onClick(final DialogInterface dialog, final int item) {
                        new RetweetStatus(holder, holder.tweetId, item + 1).execute();
                    }
                }).create().show();
            }
        }
    });

    holder.retweet.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            new AlertDialog.Builder(context).setTitle(context.getResources().getString(R.string.remove_retweet))
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            new RemoveRetweet(holder.tweetId).execute();
                        }
                    }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).create().show();
            return false;
        }

        class RemoveRetweet extends AsyncTask<String, Void, Boolean> {

            private long tweetId;

            public RemoveRetweet(long tweetId) {
                this.tweetId = tweetId;
            }

            protected void onPreExecute() {
                holder.retweet.clearColorFilter();

                Toast.makeText(context, context.getResources().getString(R.string.removing_retweet),
                        Toast.LENGTH_SHORT).show();
            }

            protected Boolean doInBackground(String... urls) {
                try {
                    Twitter twitter = Utils.getTwitter(context, settings);
                    ResponseList<twitter4j.Status> retweets = twitter.getRetweets(tweetId);
                    for (twitter4j.Status retweet : retweets) {
                        if (retweet.getUser().getId() == settings.myId)
                            twitter.destroyStatus(retweet.getId());
                    }
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }

            protected void onPostExecute(Boolean deleted) {
                try {
                    if (deleted) {
                        Toast.makeText(context, context.getResources().getString(R.string.success),
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, context.getResources().getString(R.string.error),
                                Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    // user has gone away from the window
                }
            }
        }
    });

    holder.reply.requestFocus();
    removeKeyboard(holder);
    holder.reply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent compose = new Intent(context, ComposeActivity.class);
            String string = holder.reply.getText().toString();
            try {
                compose.putExtra("user", string.substring(0, string.length() - 1));
            } catch (Exception e) {

            }
            compose.putExtra("id", holder.tweetId);
            compose.putExtra("reply_to_text", "@" + holder.screenName + ": " + text);
            context.startActivity(compose);

            removeExpansionWithAnimation(holder);
        }
    });

    if (holder.replyButton != null) {
        holder.replyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ReplyToStatus(holder, holder.tweetId).execute();
            }
        });
    }

    final String name = screenname;

    try {
        holder.shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                intent.setType("text/plain");
                String text = holder.tweet.getText().toString();

                text = TweetLinkUtils.removeColorHtml(text, settings);
                text = restoreLinks(text);

                switch (settings.quoteStyle) {
                case AppSettings.QUOTE_STYLE_TWITTER:
                    text = " " + "https://twitter.com/" + name + "/status/" + id;
                    break;
                case AppSettings.QUOTE_STYLE_Test:
                    text = "\"@" + name + ": " + text + "\" ";
                    break;
                case AppSettings.QUOTE_STYLE_RT:
                    text = " RT @" + name + ": " + text;
                    break;
                }
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                intent.putExtra(Intent.EXTRA_TEXT, text);
                context.startActivity(
                        Intent.createChooser(intent, context.getResources().getString(R.string.menu_share)));
            }

            public String restoreLinks(String text) {
                String full = text;

                String[] split = text.split("\\s");
                String[] otherLink = new String[otherLinks.length];

                for (int i = 0; i < otherLinks.length; i++) {
                    otherLink[i] = "" + otherLinks[i];
                }

                for (String s : otherLink) {
                    Log.v("Test_links", ":" + s + ":");
                }

                boolean changed = false;
                int otherIndex = 0;

                if (otherLink.length > 0) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];

                        //if (Patterns.WEB_URL.matcher(s).find()) { // we know the link is cut off
                        if (Patterns.WEB_URL.matcher(s).find()) { // we know the link is cut off
                            String f = s.replace("...", "").replace("http", "");

                            f = stripTrailingPeriods(f);

                            try {
                                if (otherIndex < otherLinks.length) {
                                    if (otherLink[otherIndex].substring(otherLink[otherIndex].length() - 1,
                                            otherLink[otherIndex].length()).equals("/")) {
                                        otherLink[otherIndex] = otherLink[otherIndex].substring(0,
                                                otherLink[otherIndex].length() - 1);
                                    }
                                    f = otherLink[otherIndex].replace("http://", "").replace("https://", "")
                                            .replace("www.", "");
                                    otherLink[otherIndex] = "";
                                    otherIndex++;

                                    changed = true;
                                }
                            } catch (Exception e) {

                            }

                            if (changed) {
                                split[i] = f;
                            } else {
                                split[i] = s;
                            }
                        } else {
                            split[i] = s;
                        }

                    }
                }

                if (!webpage.equals("")) {
                    for (int i = split.length - 1; i >= 0; i--) {
                        String s = split[i];
                        if (Patterns.WEB_URL.matcher(s).find()) {
                            String replace = otherLinks[otherLinks.length - 1];
                            if (replace.replace(" ", "").equals("")) {
                                replace = webpage;
                            }
                            split[i] = replace;
                            changed = true;
                            break;
                        }
                    }
                }

                if (changed) {
                    full = "";
                    for (String p : split) {
                        full += p + " ";
                    }

                    full = full.substring(0, full.length() - 1);
                }

                return full;
            }

            private String stripTrailingPeriods(String url) {
                try {
                    if (url.substring(url.length() - 1, url.length()).equals(".")) {
                        return stripTrailingPeriods(url.substring(0, url.length() - 1));
                    } else {
                        return url;
                    }
                } catch (Exception e) {
                    return url;
                }
            }
        });

        holder.quoteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ComposeActivity.class);
                intent.setType("text/plain");
                String text = holder.tweet.getText().toString();

                text = TweetLinkUtils.removeColorHtml(text, settings);
                text = restoreLinks(text);

                switch (settings.quoteStyle) {
                case AppSettings.QUOTE_STYLE_TWITTER:
                    text = " " + "https://twitter.com/" + name + "/status/" + id;
                    break;
                case AppSettings.QUOTE_STYLE_Test:
                    text = "\"@" + name + ": " + text + "\" ";
                    break;
                case AppSettings.QUOTE_STYLE_RT:
                    text = " RT @" + name + ": " + text;
                    break;
                }
                intent.putExtra("user", text);
                intent.putExtra("id", id);

                context.startActivity(intent);
            }

            public String restoreLinks(String text) {
                String full = text;

                String[] split = text.split("\\s");
                String[] otherLink = new String[otherLinks.length];

                for (int i = 0; i < otherLinks.length; i++) {
                    otherLink[i] = "" + otherLinks[i];
                }

                boolean changed = false;

                if (otherLink.length > 0) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];

                        //if (Patterns.WEB_URL.matcher(s).find()) { // we know the link is cut off
                        if (s.contains("...")) { // we know the link is cut off
                            String f = s.replace("...", "").replace("http", "");

                            Log.v("Test_links", ":" + s + ":");

                            for (int x = 0; x < otherLink.length; x++) {
                                if (otherLink[x].toLowerCase().contains(f.toLowerCase())) {
                                    changed = true;
                                    // for some reason it wouldn't match the last "/" on a url and it was stopping it from opening
                                    try {
                                        if (otherLink[x]
                                                .substring(otherLink[x].length() - 1, otherLink[x].length())
                                                .equals("/")) {
                                            otherLink[x] = otherLink[x].substring(0, otherLink[x].length() - 1);
                                        }
                                        f = otherLink[x].replace("http://", "").replace("https://", "")
                                                .replace("www.", "");
                                        otherLink[x] = "";
                                    } catch (Exception e) {

                                    }
                                    break;
                                }
                            }

                            if (changed) {
                                split[i] = f;
                            } else {
                                split[i] = s;
                            }
                        } else {
                            split[i] = s;
                        }

                    }
                }

                if (!webpage.equals("")) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];
                        s = s.replace("...", "");

                        if (Patterns.WEB_URL.matcher(s).find()
                                && (s.startsWith("t.co/") || s.contains("twitter.com/"))) { // we know the link is cut off
                            String replace = otherLinks[otherLinks.length - 1];
                            if (replace.replace(" ", "").equals("")) {
                                replace = webpage;
                            }
                            split[i] = replace;
                            changed = true;
                        }
                    }
                }

                if (changed) {
                    full = "";
                    for (String p : split) {
                        full += p + " ";
                    }

                    full = full.substring(0, full.length() - 1);
                }

                return full;
            }
        });
    } catch (Exception e) {
        // theme made before these were implemented
    }
}

From source file:com.klinker.android.twitter.adapters.TimelineArrayAdapter.java

License:Apache License

public void addExpansion(final ViewHolder holder, String screenname, String users, final String[] otherLinks,
        final String webpage, final long id) {

    holder.retweet.setVisibility(View.VISIBLE);
    holder.retweetCount.setVisibility(View.VISIBLE);
    holder.favCount.setVisibility(View.VISIBLE);
    holder.favorite.setVisibility(View.VISIBLE);

    //holder.reply.setVisibility(View.GONE);
    try {//from   w  w w .  j  av a  2s .co  m
        holder.replyButton.setVisibility(View.GONE);
    } catch (Exception e) {

    }

    holder.screenName = screenname;

    // used to find the other names on a tweet... could be optimized i guess, but only run when button is pressed
    final String text = holder.tweet.getText().toString();
    String extraNames = "";

    if (text.contains("@")) {
        for (String s : users.split("  ")) {
            if (!s.equals(settings.myScreenName) && !extraNames.contains(s) && !s.equals(screenname)) {
                extraNames += "@" + s + " ";
            }
        }
    }

    try {
        if (holder.retweeter.getVisibility() == View.VISIBLE && !extraNames.contains(holder.retweeterName)) {
            extraNames += "@" + holder.retweeterName + " ";
        }
    } catch (NullPointerException e) {

    }

    if (!screenname.equals(settings.myScreenName)) {
        holder.reply.setText("@" + screenname + " " + extraNames);
    } else {
        holder.reply.setText(extraNames);
    }

    holder.reply.setSelection(holder.reply.getText().length());

    if (holder.favCount.getText().toString().length() <= 2) {
        holder.favCount.setText(" -");
        holder.retweetCount.setText(" -");
    }

    //ExpansionAnimation expandAni = new ExpansionAnimation(holder.expandArea, 450);
    holder.expandArea.setVisibility(View.VISIBLE);//startAnimation(expandAni);

    getCounts(holder, id);

    holder.favorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new FavoriteStatus(holder, holder.tweetId).execute();
        }
    });

    holder.retweet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new RetweetStatus(holder, holder.tweetId).execute();
        }
    });

    holder.retweet.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            new AlertDialog.Builder(context).setTitle(context.getResources().getString(R.string.remove_retweet))
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            new RemoveRetweet(holder.tweetId).execute();
                        }
                    }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).create().show();
            return false;
        }

        class RemoveRetweet extends AsyncTask<String, Void, Boolean> {

            private long tweetId;

            public RemoveRetweet(long tweetId) {
                this.tweetId = tweetId;
            }

            protected void onPreExecute() {
                holder.retweet.clearColorFilter();

                Toast.makeText(context, context.getResources().getString(R.string.removing_retweet),
                        Toast.LENGTH_SHORT).show();
            }

            protected Boolean doInBackground(String... urls) {
                try {
                    Twitter twitter = Utils.getTwitter(context, settings);
                    ResponseList<twitter4j.Status> retweets = twitter.getRetweets(tweetId);
                    for (twitter4j.Status retweet : retweets) {
                        if (retweet.getUser().getId() == settings.myId)
                            twitter.destroyStatus(retweet.getId());
                    }
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }

            protected void onPostExecute(Boolean deleted) {
                try {
                    if (deleted) {
                        Toast.makeText(context, context.getResources().getString(R.string.success),
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, context.getResources().getString(R.string.error),
                                Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    // user has gone away from the window
                }
            }
        }
    });

    holder.reply.requestFocus();
    removeKeyboard(holder);
    holder.reply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent compose = new Intent(context, ComposeActivity.class);
            String string = holder.reply.getText().toString();
            try {
                compose.putExtra("user", string.substring(0, string.length() - 1));
            } catch (Exception e) {

            }
            compose.putExtra("id", holder.tweetId);
            compose.putExtra("reply_to_text", "@" + holder.screenName + ": " + text);
            context.startActivity(compose);

            removeExpansionWithAnimation(holder);
        }
    });

    if (holder.replyButton != null) {
        holder.replyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ReplyToStatus(holder, holder.tweetId).execute();
            }
        });
    }

    final String name = screenname;

    try {
        holder.shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                intent.setType("text/plain");
                String text = holder.tweet.getText().toString();

                text = TweetLinkUtils.removeColorHtml(text, settings);
                text = restoreLinks(text);

                if (!settings.preferRT) {
                    text = "\"@" + name + ": " + text + "\" ";
                } else {
                    text = " RT @" + name + ": " + text;
                }
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                intent.putExtra(Intent.EXTRA_TEXT, text);
                context.startActivity(
                        Intent.createChooser(intent, context.getResources().getString(R.string.menu_share)));
            }

            public String restoreLinks(String text) {
                String full = text;

                String[] split = text.split("\\s");
                String[] otherLink = new String[otherLinks.length];

                for (int i = 0; i < otherLinks.length; i++) {
                    otherLink[i] = "" + otherLinks[i];
                }

                boolean changed = false;

                if (otherLink.length > 0) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];

                        //if (Patterns.WEB_URL.matcher(s).find()) { // we know the link is cut off
                        if (s.contains("...")) { // we know the link is cut off
                            String f = s.replace("...", "").replace("http", "");

                            Log.v("talon_links", ":" + s + ":");

                            for (int x = 0; x < otherLink.length; x++) {
                                if (otherLink[x].toLowerCase().contains(f.toLowerCase())) {
                                    changed = true;
                                    // for some reason it wouldn't match the last "/" on a url and it was stopping it from opening
                                    try {
                                        if (otherLink[x]
                                                .substring(otherLink[x].length() - 1, otherLink[x].length())
                                                .equals("/")) {
                                            otherLink[x] = otherLink[x].substring(0, otherLink[x].length() - 1);
                                        }
                                        f = otherLink[x].replace("http://", "").replace("https://", "")
                                                .replace("www.", "");
                                        otherLink[x] = "";
                                    } catch (Exception e) {

                                    }
                                    break;
                                }
                            }

                            if (changed) {
                                split[i] = f;
                            } else {
                                split[i] = s;
                            }
                        } else {
                            split[i] = s;
                        }

                    }
                }

                if (!webpage.equals("")) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];

                        if (s.contains("...")) {
                            s = s.replace("...", "");

                            if (Patterns.WEB_URL.matcher(s).find()
                                    && (s.startsWith("t.co/") || s.contains("twitter.com/"))) { // we know the link is cut off
                                String replace = otherLinks[otherLinks.length - 1];
                                if (replace.replace(" ", "").equals("")) {
                                    replace = webpage;
                                }
                                split[i] = replace;
                                changed = true;
                            }
                        }
                    }
                }

                if (changed) {
                    full = "";
                    for (String p : split) {
                        full += p + " ";
                    }

                    full = full.substring(0, full.length() - 1);
                }

                return full;
            }
        });

        holder.quoteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ComposeActivity.class);
                intent.setType("text/plain");
                String text = holder.tweet.getText().toString();

                text = TweetLinkUtils.removeColorHtml(text, settings);
                text = restoreLinks(text);

                if (!settings.preferRT) {
                    text = "\"@" + name + ": " + text + "\" ";
                } else {
                    text = " RT @" + name + ": " + text;
                }
                intent.putExtra("user", text);
                intent.putExtra("id", id);

                context.startActivity(intent);
            }

            public String restoreLinks(String text) {
                String full = text;

                String[] split = text.split("\\s");
                String[] otherLink = new String[otherLinks.length];

                for (int i = 0; i < otherLinks.length; i++) {
                    otherLink[i] = "" + otherLinks[i];
                }

                boolean changed = false;

                if (otherLink.length > 0) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];

                        //if (Patterns.WEB_URL.matcher(s).find()) { // we know the link is cut off
                        if (s.contains("...")) { // we know the link is cut off
                            String f = s.replace("...", "").replace("http", "");

                            Log.v("talon_links", ":" + s + ":");

                            for (int x = 0; x < otherLink.length; x++) {
                                if (otherLink[x].toLowerCase().contains(f.toLowerCase())) {
                                    changed = true;
                                    // for some reason it wouldn't match the last "/" on a url and it was stopping it from opening
                                    try {
                                        if (otherLink[x]
                                                .substring(otherLink[x].length() - 1, otherLink[x].length())
                                                .equals("/")) {
                                            otherLink[x] = otherLink[x].substring(0, otherLink[x].length() - 1);
                                        }
                                        f = otherLink[x].replace("http://", "").replace("https://", "")
                                                .replace("www.", "");
                                        otherLink[x] = "";
                                    } catch (Exception e) {

                                    }
                                    break;
                                }
                            }

                            if (changed) {
                                split[i] = f;
                            } else {
                                split[i] = s;
                            }
                        } else {
                            split[i] = s;
                        }

                    }
                }

                if (!webpage.equals("")) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];
                        s = s.replace("...", "");

                        if (Patterns.WEB_URL.matcher(s).find()
                                && (s.startsWith("t.co/") || s.contains("twitter.com/"))) { // we know the link is cut off
                            String replace = otherLinks[otherLinks.length - 1];
                            if (replace.replace(" ", "").equals("")) {
                                replace = webpage;
                            }
                            split[i] = replace;
                            changed = true;
                        }
                    }
                }

                if (changed) {
                    full = "";
                    for (String p : split) {
                        full += p + " ";
                    }

                    full = full.substring(0, full.length() - 1);
                }

                return full;
            }
        });
    } catch (Exception e) {
        // theme made before these were implemented
    }
}

From source file:com.klinker.android.twitter.adapters.TimeLineCursorAdapter.java

License:Apache License

public void addExpansion(final ViewHolder holder, String screenname, String users, final String[] otherLinks,
        final String webpage, final long tweetId, String[] hashtags) {
    if (isDM) {/*from  ww w .j  av a  2s. c  o m*/
        holder.retweet.setVisibility(View.GONE);
        holder.retweetCount.setVisibility(View.GONE);
        holder.favCount.setVisibility(View.GONE);
        holder.favorite.setVisibility(View.GONE);
    } else {
        holder.retweet.setVisibility(View.VISIBLE);
        holder.retweetCount.setVisibility(View.VISIBLE);
        holder.favCount.setVisibility(View.VISIBLE);
        holder.favorite.setVisibility(View.VISIBLE);
    }

    try {
        holder.replyButton.setVisibility(View.GONE);
    } catch (Exception e) {

    }
    try {
        holder.charRemaining.setVisibility(View.GONE);
    } catch (Exception e) {

    }

    holder.screenName = screenname;

    // used to find the other names on a tweet... could be optimized i guess, but only run when button is pressed
    if (!isDM) {
        String text = holder.tweet.getText().toString();
        String extraNames = "";

        if (text.contains("@")) {
            for (String s : users.split("  ")) {
                if (!s.equals(settings.myScreenName) && !extraNames.contains(s) && !s.equals(screenname)) {
                    extraNames += "@" + s + " ";
                }
            }
        }

        try {
            if (holder.retweeter.getVisibility() == View.VISIBLE
                    && !extraNames.contains(holder.retweeterName)) {
                extraNames += "@" + holder.retweeterName + " ";
            }
        } catch (NullPointerException e) {

        }

        if (!screenname.equals(settings.myScreenName)) {
            holder.reply.setText("@" + screenname + " " + extraNames);
        } else {
            holder.reply.setText(extraNames);
        }

        if (settings.autoInsertHashtags && hashtags != null) {
            for (String s : hashtags) {
                if (!s.equals("")) {
                    holder.reply.append("#" + s + " ");
                }
            }
        }
    }

    holder.reply.setSelection(holder.reply.getText().length());

    if (holder.favCount.getText().toString().length() <= 2) {
        holder.favCount.setText(" -");
        holder.retweetCount.setText(" -");
    }

    //ExpansionAnimation expandAni = new ExpansionAnimation(holder.expandArea, 450);
    holder.expandArea.setVisibility(View.VISIBLE);//startAnimation(expandAni);

    if (holder.favCount.getText().toString().equals(" -")) {
        getCounts(holder, tweetId);
    }

    holder.favorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new FavoriteStatus(holder, holder.tweetId).execute();
        }
    });

    holder.retweet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new RetweetStatus(holder, holder.tweetId).execute();
        }
    });

    holder.retweet.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            new AlertDialog.Builder(context).setTitle(context.getResources().getString(R.string.remove_retweet))
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            new RemoveRetweet(holder.tweetId).execute();
                        }
                    }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    }).create().show();
            return false;
        }

        class RemoveRetweet extends AsyncTask<String, Void, Boolean> {

            private long tweetId;

            public RemoveRetweet(long tweetId) {
                this.tweetId = tweetId;
            }

            protected void onPreExecute() {
                holder.retweet.clearColorFilter();

                Toast.makeText(context, context.getResources().getString(R.string.removing_retweet),
                        Toast.LENGTH_SHORT).show();
            }

            protected Boolean doInBackground(String... urls) {
                try {
                    Twitter twitter = Utils.getTwitter(context, settings);
                    ResponseList<twitter4j.Status> retweets = twitter.getRetweets(tweetId);
                    for (twitter4j.Status retweet : retweets) {
                        if (retweet.getUser().getId() == settings.myId)
                            twitter.destroyStatus(retweet.getId());
                    }
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            }

            protected void onPostExecute(Boolean deleted) {
                try {
                    if (deleted) {
                        Toast.makeText(context, context.getResources().getString(R.string.success),
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, context.getResources().getString(R.string.error),
                                Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    // user has gone away from the window
                }
            }
        }
    });

    holder.reply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent compose = new Intent(context, ComposeActivity.class);
            String string = holder.reply.getText().toString();
            try {
                compose.putExtra("user", string.substring(0, string.length() - 1));
            } catch (Exception e) {
                e.printStackTrace();
            }
            compose.putExtra("id", holder.tweetId);
            compose.putExtra("reply_to_text",
                    "@" + holder.screenName + ": " + holder.tweet.getText().toString());

            if (isHomeTimeline) {
                sharedPrefs.edit().putLong("current_position_" + settings.currentAccount, holder.tweetId)
                        .commit();
            }

            context.startActivity(compose);

            removeExpansionWithAnimation(holder);
        }
    });

    holder.reply.requestFocus();
    removeKeyboard(holder);

    // this isn't going to run anymore, but just in case i put it back i guess
    if (holder.replyButton != null) {
        holder.replyButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ReplyToStatus(holder, holder.tweetId).execute();
            }
        });

        holder.charRemaining.setText(140 - holder.reply.getText().length() + "");

        holder.reply.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                hasKeyboard = b;
            }
        });

        holder.reply.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                holder.charRemaining.setText(140 - holder.reply.getText().length() + "");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    final String name = screenname;

    try {
        holder.shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                intent.setType("text/plain");
                String text = holder.tweet.getText().toString();

                text = restoreLinks(text);

                text = "@" + name + ": " + text;

                Log.v("talon_sharing", "text: " + text);

                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                intent.putExtra(Intent.EXTRA_TEXT, text);

                if (isHomeTimeline) {
                    sharedPrefs.edit().putLong("current_position_" + settings.currentAccount, holder.tweetId)
                            .commit();
                }

                context.startActivity(
                        Intent.createChooser(intent, context.getResources().getString(R.string.menu_share)));
            }

            public String restoreLinks(String text) {
                String full = text;

                String[] split = text.split("\\s");
                String[] otherLink = new String[otherLinks.length];

                for (int i = 0; i < otherLinks.length; i++) {
                    otherLink[i] = "" + otherLinks[i];
                }

                boolean changed = false;

                if (otherLink.length > 0) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];

                        if (Patterns.WEB_URL.matcher(s).find()) { // we know the link is cut off
                            String f = s.replace("...", "").replace("http", "");

                            for (int x = 0; x < otherLink.length; x++) {
                                if (otherLink[x].contains(f)) {
                                    changed = true;
                                    // for some reason it wouldn't match the last "/" on a url and it was stopping it from opening
                                    if (otherLink[x].substring(otherLink[x].length() - 1, otherLink[x].length())
                                            .equals("/")) {
                                        otherLink[x] = otherLink[x].substring(0, otherLink[x].length() - 1);
                                    }
                                    f = otherLink[x];
                                    otherLink[x] = "";
                                    break;
                                }
                            }

                            if (changed) {
                                split[i] = f;
                            } else {
                                split[i] = s;
                            }
                        } else {
                            split[i] = s;
                        }

                    }
                }

                if (!webpage.equals("")) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];
                        if (s.contains("...")) {
                            s = s.replace("...", "");

                            if (Patterns.WEB_URL.matcher(s).find()
                                    && (s.startsWith("t.co/") || s.contains("twitter.com/"))) { // we know the link is cut off
                                String replace = otherLinks[otherLinks.length - 1];
                                if (replace.replace(" ", "").equals("")) {
                                    replace = webpage;
                                }
                                split[i] = replace;
                                changed = true;
                            }
                        }
                    }
                }

                if (changed) {
                    full = "";
                    for (String p : split) {
                        full += p + " ";
                    }

                    full = full.substring(0, full.length() - 1);
                }

                return full;
            }
        });

        holder.quoteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ComposeActivity.class);
                intent.setType("text/plain");
                String text = holder.tweet.getText().toString();

                text = TweetLinkUtils.removeColorHtml(text, settings);
                text = restoreLinks(text);

                if (!settings.preferRT) {
                    text = "\"@" + name + ": " + text + "\" ";
                } else {
                    text = " RT @" + name + ": " + text;
                }
                intent.putExtra("user", text);
                intent.putExtra("id", tweetId);

                if (isHomeTimeline) {
                    sharedPrefs.edit().putLong("current_position_" + settings.currentAccount, holder.tweetId)
                            .commit();
                }

                context.startActivity(intent);
            }

            public String restoreLinks(String text) {
                String full = text;

                String[] split = text.split("\\s");
                String[] otherLink = new String[otherLinks.length];

                for (int i = 0; i < otherLinks.length; i++) {
                    otherLink[i] = "" + otherLinks[i];
                }

                boolean changed = false;

                if (otherLink.length > 0) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];

                        if (Patterns.WEB_URL.matcher(s).find()) { // we know the link is cut off
                            String f = s.replace("...", "").replace("http", "");

                            for (int x = 0; x < otherLink.length; x++) {
                                if (otherLink[x].contains(f)) {
                                    changed = true;
                                    // for some reason it wouldn't match the last "/" on a url and it was stopping it from opening
                                    if (otherLink[x].substring(otherLink[x].length() - 1, otherLink[x].length())
                                            .equals("/")) {
                                        otherLink[x] = otherLink[x].substring(0, otherLink[x].length() - 1);
                                    }
                                    f = otherLink[x];
                                    otherLink[x] = "";
                                    break;
                                }
                            }

                            if (changed) {
                                split[i] = f;
                            } else {
                                split[i] = s;
                            }
                        } else {
                            split[i] = s;
                        }

                    }
                }

                if (!webpage.equals("")) {
                    for (int i = 0; i < split.length; i++) {
                        String s = split[i];
                        s = s.replace("...", "");

                        if (Patterns.WEB_URL.matcher(s).find()
                                && (s.startsWith("t.co/") || s.contains("twitter.com/"))) { // we know the link is cut off
                            String replace = otherLinks[otherLinks.length - 1];
                            if (replace.replace(" ", "").equals("")) {
                                replace = webpage;
                            }
                            split[i] = replace;
                            changed = true;
                        }
                    }
                }

                if (changed) {
                    full = "";
                    for (String p : split) {
                        full += p + " ";
                    }

                    full = full.substring(0, full.length() - 1);
                }

                return full;
            }
        });
    } catch (Exception e) {
        // theme made before these were implemented
    }
    if (settings.addonTheme) {
        try {
            Resources resourceAddon = context.getPackageManager()
                    .getResourcesForApplication(settings.addonThemePackage);
            int back = resourceAddon.getIdentifier("reply_entry_background", "drawable",
                    settings.addonThemePackage);
            holder.reply.setBackgroundDrawable(resourceAddon.getDrawable(back));
        } catch (Exception e) {
            // theme does not include a reply entry box
        }
    }
}

From source file:com.klinker.android.twitter.ui.tweet_viewer.ViewRetweeters.java

License:Apache License

public void onRefreshStarted() {
    new Thread(new Runnable() {
        @Override/*from   www. j ava 2s . c  om*/
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, settings);

                Status stat = twitter.showStatus(tweetId);
                if (stat.isRetweet()) {
                    tweetId = stat.getRetweetedStatus().getId();
                }

                // can get 100 retweeters is all
                ResponseList<twitter4j.Status> lists = twitter.getRetweets(tweetId);

                users.clear();

                for (Status status : lists) {
                    users.add(status.getUser());
                }

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new PeopleArrayAdapter(context, users);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);

                        spinner.setVisibility(View.GONE);

                        if (users.size() == 0) {
                            noContent.setVisibility(View.VISIBLE);
                        }

                        mPullToRefreshLayout.setRefreshing(false);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();

            } catch (OutOfMemoryError e) {
                e.printStackTrace();

            }
        }
    }).start();
}

From source file:jp.wda.azeda.core.daemons.impl.RetweetsCrawlerImpl.java

License:Apache License

/**
 *
 * @param twitter/*from   w w  w  . j a v  a  2 s. c  o m*/
 * @param twitterID
 * @return
 * @throws TwitterException
 */
private ResponseList<Status> getRetweets(Twitter twitter, long twitterID) throws TwitterException {
    if (getRetweetsTimer == 0) {
        getRetweetsTimer = System.currentTimeMillis();
    }
    long waiting = 15 * 60 * 1000L - (System.currentTimeMillis() - getRetweetsTimer);
    if (++getRetweetsCounter >= 15) {
        if (waiting > 0) {
            log.info("?????getRetweets" + (waiting / 1000)
                    + "?????");
            try {
                sleep(waiting);
            } catch (InterruptedException exx) {
                log.error("", exx);
            }
        }
        getRetweetsTimer = System.currentTimeMillis();
        getRetweetsCounter = 0;
    }

    try {
        return twitter.getRetweets(twitterID);
    } catch (TwitterException ex) {
        log.error("getRetweets?twitter?????15?????", ex);
        try {
            sleep(15 * 60 * 1000L);
        } catch (InterruptedException exx) {
            log.error("", exx);
        }
        return twitter.getRetweets(twitterID);
    }
}

From source file:twitter4j.examples.tweets.GetRetweets.java

License:Apache License

/**
 * Usage: java twitter4j.examples.tweets.GetRetweets [status id]
 *
 * @param args message/*  ww w  . j  ava 2  s . c om*/
 */
public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.tweets.GetRetweets [status id]");
        System.exit(-1);
    }
    System.out.println("Showing up to 100 of the first retweets of the status id - [" + args[0] + "].");
    try {
        Twitter twitter = new TwitterFactory().getInstance();
        List<Status> statuses = twitter.getRetweets(Long.parseLong(args[0]));
        for (Status status : statuses) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }
        System.out.println("done.");
        System.exit(0);
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get retweets: " + te.getMessage());
        System.exit(-1);
    }
}