Search in sources :

Example 86 with Twitter

use of twitter4j.Twitter in project camel by apache.

the class SearchProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    long myLastId = lastId;
    // KEYWORDS
    // keywords from header take precedence
    String keywords = exchange.getIn().getHeader(TwitterConstants.TWITTER_KEYWORDS, String.class);
    if (keywords == null) {
        keywords = endpoint.getProperties().getKeywords();
    }
    if (keywords == null) {
        throw new CamelExchangeException("No keywords to use for query", exchange);
    }
    Query query = new Query(keywords);
    // filter of older tweets
    if (endpoint.getProperties().isFilterOld() && myLastId != 0) {
        query.setSinceId(myLastId);
    }
    // since id
    Long sinceId = exchange.getIn().getHeader(TwitterConstants.TWITTER_SINCEID, Long.class);
    if (sinceId == null) {
        sinceId = endpoint.getProperties().getSinceId();
    }
    if (ObjectHelper.isNotEmpty(sinceId)) {
        query.setSinceId(sinceId);
    }
    // max id
    Long maxId = exchange.getIn().getHeader(TwitterConstants.TWITTER_MAXID, Long.class);
    if (ObjectHelper.isNotEmpty(maxId)) {
        query.setMaxId(maxId);
    }
    // language
    String lang = exchange.getIn().getHeader(TwitterConstants.TWITTER_SEARCH_LANGUAGE, String.class);
    if (lang == null) {
        lang = endpoint.getProperties().getLang();
    }
    if (ObjectHelper.isNotEmpty(lang)) {
        query.setLang(lang);
    }
    // number of elements per page
    Integer count = exchange.getIn().getHeader(TwitterConstants.TWITTER_COUNT, Integer.class);
    if (count == null) {
        count = endpoint.getProperties().getCount();
    }
    if (ObjectHelper.isNotEmpty(count)) {
        query.setCount(count);
    }
    // number of pages
    Integer numberOfPages = exchange.getIn().getHeader(TwitterConstants.TWITTER_NUMBER_OF_PAGES, Integer.class);
    if (numberOfPages == null) {
        numberOfPages = endpoint.getProperties().getNumberOfPages();
    }
    Twitter twitter = endpoint.getProperties().getTwitter();
    log.debug("Searching twitter with keywords: {}", keywords);
    QueryResult results = twitter.search(query);
    List<Status> list = results.getTweets();
    for (int i = 1; i < numberOfPages; i++) {
        if (!results.hasNext()) {
            break;
        }
        log.debug("Fetching page");
        results = twitter.search(results.nextQuery());
        list.addAll(results.getTweets());
    }
    if (endpoint.getProperties().isFilterOld()) {
        for (Status t : list) {
            long newId = t.getId();
            if (newId > myLastId) {
                myLastId = newId;
            }
        }
    }
    exchange.getIn().setBody(list);
    // update the lastId after finished the processing
    if (myLastId > lastId) {
        lastId = myLastId;
    }
}
Also used : Status(twitter4j.Status) CamelExchangeException(org.apache.camel.CamelExchangeException) QueryResult(twitter4j.QueryResult) Query(twitter4j.Query) Twitter(twitter4j.Twitter) TwitterEndpoint(org.apache.camel.component.twitter.TwitterEndpoint)

Example 87 with Twitter

use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.

the class TimeLineCursorAdapter method getCounts.

public void getCounts(final ViewHolder holder, final long tweetId) {
    Thread getCount = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Twitter twitter = getTwitter();
                final Status status;
                if (holder.retweeter.getVisibility() != View.GONE) {
                    status = twitter.showStatus(holder.tweetId).getRetweetedStatus();
                } else {
                    status = twitter.showStatus(tweetId);
                }
                if (status != null && holder.tweetId == tweetId) {
                    ((Activity) context).runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            holder.favCount.setText(" " + status.getFavoriteCount());
                            holder.retweetCount.setText(" " + status.getRetweetCount());
                            if (status.isFavorited()) {
                                TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { R.attr.favoritedButton });
                                int resource = a.getResourceId(0, 0);
                                a.recycle();
                                if (!settings.addonTheme) {
                                    holder.favorite.setColorFilter(context.getResources().getColor(R.color.app_color));
                                } else {
                                    holder.favorite.setColorFilter(settings.accentInt);
                                }
                                holder.favorite.setImageDrawable(context.getResources().getDrawable(resource));
                                holder.isFavorited = true;
                            } else {
                                TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { R.attr.notFavoritedButton });
                                int resource = a.getResourceId(0, 0);
                                a.recycle();
                                holder.favorite.setImageDrawable(context.getResources().getDrawable(resource));
                                holder.isFavorited = false;
                                holder.favorite.clearColorFilter();
                            }
                            if (status.isRetweetedByMe()) {
                                if (!settings.addonTheme) {
                                    holder.retweet.setColorFilter(context.getResources().getColor(R.color.app_color));
                                } else {
                                    holder.retweet.setColorFilter(settings.accentInt);
                                }
                            } else {
                                holder.retweet.clearColorFilter();
                            }
                        }
                    });
                }
            } catch (Exception e) {
            }
        }
    });
    getCount.setPriority(7);
    getCount.start();
}
Also used : Status(twitter4j.Status) TypedArray(android.content.res.TypedArray) Twitter(twitter4j.Twitter) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TwitterException(twitter4j.TwitterException) IOException(java.io.IOException)

Example 88 with Twitter

use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.

the class SavedSearchArrayAdapter method bindView.

@Override
public void bindView(final View view, Context mContext, final String trend) {
    final ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(trend);
    holder.text.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent search = new Intent(context, SearchedTrendsActivity.class);
            search.setAction(Intent.ACTION_SEARCH);
            search.putExtra(SearchManager.QUERY, trend);
            context.startActivity(search);
        }
    });
    holder.text.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View view) {
            final String search = trend;
            new AlertDialog.Builder(context).setTitle(context.getResources().getString(R.string.delete_saved_search)).setMessage(context.getResources().getString(R.string.cache_dialog_summary)).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(context, context.getString(R.string.deleting_search), Toast.LENGTH_SHORT).show();
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            long id = -1;
                            Twitter twitter = Utils.getTwitter(context, AppSettings.getInstance(context));
                            try {
                                ResponseList<SavedSearch> searches = twitter.savedSearches().getSavedSearches();
                                for (int i = 0; i < searches.size(); i++) {
                                    String name = searches.get(i).getName();
                                    Log.v("talon_saved_searches", name);
                                    if (name.equals(search)) {
                                        id = searches.get(i).getId();
                                        Log.v("talon_saved_searches", "id to delete: " + id);
                                    }
                                }
                                if (id != -1) {
                                    twitter.destroySavedSearch(id);
                                    ((Activity) context).runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {
                                            Toast.makeText(context, context.getString(R.string.success), Toast.LENGTH_SHORT).show();
                                            SavedSearchesActivity.getSearches();
                                        }
                                    });
                                } else {
                                    ((Activity) context).runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {
                                            Toast.makeText(context, context.getString(R.string.error), Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            } catch (Exception e) {
                                ((Activity) context).runOnUiThread(new Runnable() {

                                    @Override
                                    public void run() {
                                        Toast.makeText(context, context.getString(R.string.error), Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        }
                    }).start();
                }
            }).setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            }).create().show();
            return false;
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Twitter(twitter4j.Twitter) SearchedTrendsActivity(com.klinker.android.twitter.activities.drawer_activities.discover.trends.SearchedTrendsActivity) SavedSearchesActivity(com.klinker.android.twitter.activities.drawer_activities.SavedSearchesActivity) Activity(android.app.Activity) Intent(android.content.Intent) View(android.view.View) SearchedTrendsActivity(com.klinker.android.twitter.activities.drawer_activities.discover.trends.SearchedTrendsActivity) ResponseList(twitter4j.ResponseList)

Example 89 with Twitter

use of twitter4j.Twitter in project Talon-for-Twitter by klinker24.

the class FavoriterUtils method getFavoriters.

public List<User> getFavoriters(Context context, long tweetId) {
    List<User> users = new ArrayList<User>();
    Twitter twitter = Utils.getTwitter(context);
    try {
        Status stat = twitter.showStatus(tweetId);
        if (stat.isRetweet()) {
            tweetId = stat.getRetweetedStatus().getId();
        }
        long[] ids = getFavoritersIds(tweetId);
        users = twitter.lookupUsers(ids);
    } catch (TwitterException e) {
        e.printStackTrace();
    }
    return users;
}
Also used : Status(twitter4j.Status) User(twitter4j.User) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) TwitterException(twitter4j.TwitterException)

Example 90 with Twitter

use of twitter4j.Twitter in project twitter-2-weibo by rjyo.

the class AuthServlet method doGet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpServletRouter r = new HttpServletRouter(request);
    r.setPattern("/:type");
    response.setContentType("text/plain");
    PrintWriter writer = response.getWriter();
    String serverPath = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
    HttpSession session = request.getSession();
    if (r.is(":type", "weibo")) {
        try {
            Oauth oauth = new Oauth();
            String redirectUrl = oauth.authorize("code");
            response.setStatus(302);
            response.setHeader("Location", redirectUrl);
            log.info("Redirecting Weibo...");
        } catch (WeiboException e) {
            log.error(e);
        }
    } else if (r.is(":type", "twitter")) {
        log.info("hello world~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        try {
            TwitterFactory factory = new TwitterFactory();
            Twitter t = factory.getInstance();
            twitter4j.auth.RequestToken requestToken = t.getOAuthRequestToken(serverPath + "/callback/twitter");
            response.setStatus(302);
            log.info(requestToken.getAuthenticationURL());
            response.setHeader("Location", requestToken.getAuthenticationURL());
            session.setAttribute(Keys.SESSION_REQUEST_TOKEN, requestToken);
            log.info("Redirecting Twitter...");
        } catch (TwitterException e) {
            log.error(e);
        }
        writer.close();
    } else {
        response.setStatus(200);
        writer.println("Wrong parameter, not working!");
        writer.close();
    }
}
Also used : Oauth(weibo4j.Oauth) WeiboException(weibo4j.model.WeiboException) HttpSession(javax.servlet.http.HttpSession) HttpServletRouter(h2weibo.HttpServletRouter) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) TwitterException(twitter4j.TwitterException) PrintWriter(java.io.PrintWriter)

Aggregations

Twitter (twitter4j.Twitter)127 TwitterException (twitter4j.TwitterException)76 TwitterFactory (twitter4j.TwitterFactory)60 Status (twitter4j.Status)44 Activity (android.app.Activity)35 QueryResult (twitter4j.QueryResult)17 TimelineArrayAdapter (com.klinker.android.twitter.adapters.TimelineArrayAdapter)16 Intent (android.content.Intent)13 ArrayList (java.util.ArrayList)13 User (twitter4j.User)12 Query (twitter4j.Query)11 IDs (twitter4j.IDs)8 LinearLayout (android.widget.LinearLayout)7 IOException (java.io.IOException)7 Context (android.content.Context)6 DrawerActivity (com.klinker.android.twitter.activities.drawer_activities.DrawerActivity)6 LoginActivity (com.klinker.android.twitter.activities.setup.LoginActivity)6 AppSettings (com.klinker.android.twitter.settings.AppSettings)5 GeoLocation (twitter4j.GeoLocation)5 Paging (twitter4j.Paging)5