Search in sources :

Example 6 with QueryResult

use of twitter4j.QueryResult in project aries by apache.

the class TwitterQuery method start.

/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
public void start(BundleContext context) throws Exception {
    Twitter twitter = new Twitter();
    Query query = new Query("from:theasf");
    try {
        QueryResult result = twitter.search(query);
        List<Tweet> tweets = result.getTweets();
        System.out.println("hits:" + tweets.size());
        for (Tweet tweet : tweets) {
            System.out.println(tweet.getFromUser() + ":" + StringEscapeUtils.unescapeXml(tweet.getText()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : QueryResult(twitter4j.QueryResult) Query(twitter4j.Query) Tweet(twitter4j.Tweet) Twitter(twitter4j.Twitter)

Example 7 with QueryResult

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

the class ConversationFragment method getDiscussion.

public void getDiscussion(final ListView listView, final long tweetId, final LinearLayout progressBar, final HoloTextView none, final Status status) {
    Log.v("talon_replies", "getting discussion");
    if (replies.size() == 0) {
        replies.add(status);
    }
    Thread getReplies = new Thread(new Runnable() {

        @Override
        public void run() {
            if (!isRunning) {
                return;
            }
            ArrayList<twitter4j.Status> all = null;
            Twitter twitter = Utils.getTwitter(context, settings);
            try {
                Log.v("talon_replies", "looking for discussion");
                long id = status.getId();
                String screenname = status.getUser().getScreenName();
                query = new Query("@" + screenname + " since_id:" + id);
                Log.v("talon_replies", "query string: " + query.getQuery());
                try {
                    query.setCount(100);
                } catch (Throwable e) {
                    // enlarge buffer error?
                    query.setCount(30);
                }
                QueryResult result = twitter.search(query);
                Log.v("talon_replies", "result: " + result.getTweets().size());
                all = new ArrayList<twitter4j.Status>();
                do {
                    Log.v("talon_replies", "do loop repetition");
                    if (!isRunning) {
                        return;
                    }
                    List<Status> tweets = result.getTweets();
                    for (twitter4j.Status tweet : tweets) {
                        if (tweet.getInReplyToStatusId() == id) {
                            all.add(tweet);
                            Log.v("talon_replies", tweet.getText());
                        }
                    }
                    if (all.size() > 0) {
                        for (int i = all.size() - 1; i >= 0; i--) {
                            Log.v("talon_replies", "inserting into arraylist:" + all.get(i).getText());
                            replies.add(all.get(i));
                        }
                        all.clear();
                        ((Activity) context).runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                progressBar.setVisibility(View.GONE);
                                try {
                                    if (replies.size() > 0) {
                                        if (adapter == null || adapter.getCount() == 0) {
                                            adapter = new TimelineArrayAdapter(context, replies);
                                            listView.setAdapter(adapter);
                                            listView.setVisibility(View.VISIBLE);
                                        } else {
                                            Log.v("talon_replies", "notifying adapter change");
                                            adapter.notifyDataSetChanged();
                                        }
                                    } else {
                                        none.setVisibility(View.VISIBLE);
                                    }
                                } catch (Exception e) {
                                    // none and it got the null object
                                    e.printStackTrace();
                                    listView.setVisibility(View.GONE);
                                    none.setVisibility(View.VISIBLE);
                                }
                            }
                        });
                    }
                    try {
                        Thread.sleep(250);
                    } catch (Exception e) {
                    // since we are changing the arraylist for the adapter in the background, we need to make sure it
                    // gets updated before continuing
                    }
                    query = result.nextQuery();
                    if (query != null)
                        result = twitter.search(query);
                } while (query != null);
            } catch (Exception e) {
                e.printStackTrace();
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            }
            if (replies.size() < 2) {
                // nothing to show, so tell them that
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        progressBar.setVisibility(View.GONE);
                        listView.setVisibility(View.GONE);
                        none.setVisibility(View.VISIBLE);
                    }
                });
            }
        }
    });
    getReplies.setPriority(8);
    getReplies.start();
}
Also used : Status(twitter4j.Status) Query(twitter4j.Query) ArrayList(java.util.ArrayList) Twitter(twitter4j.Twitter) TwitterException(twitter4j.TwitterException) QueryResult(twitter4j.QueryResult) ArrayList(java.util.ArrayList) List(java.util.List) TimelineArrayAdapter(com.klinker.android.twitter.adapters.TimelineArrayAdapter)

Example 8 with QueryResult

use of twitter4j.QueryResult in project twicalico by moko256.

the class MastodonTwitterImpl method search.

@Override
public QueryResult search(Query query) {
    Pageable<com.sys1yagi.mastodon4j.api.entity.Status> pageable = null;
    try {
        pageable = new Public(client).getFederatedTag(query.getQuery(), MTRangeConverter.convert(query)).execute();
    } catch (Mastodon4jRequestException e) {
        e.printStackTrace();
    }
    long previous;
    long next;
    if (pageable == null)
        return null;
    if (pageable.getLink() != null) {
        next = pageable.getLink().getMaxId();
        previous = pageable.getLink().getSinceId();
    } else {
        next = -1;
        previous = -1;
    }
    List<com.sys1yagi.mastodon4j.api.entity.Status> part = pageable.getPart();
    Pageable<com.sys1yagi.mastodon4j.api.entity.Status> finalPageable = pageable;
    return new QueryResult() {

        @Override
        public long getSinceId() {
            return previous;
        }

        @Override
        public long getMaxId() {
            return next;
        }

        @Override
        public String getRefreshURL() {
            return null;
        }

        @Override
        public int getCount() {
            return part.size();
        }

        @Override
        public double getCompletedIn() {
            return 0;
        }

        @Override
        public String getQuery() {
            return query.getQuery();
        }

        @Override
        public List<Status> getTweets() {
            return MTResponseList.convert(finalPageable);
        }

        @Override
        public Query nextQuery() {
            return new Query(query.getQuery()).sinceId(next);
        }

        @Override
        public boolean hasNext() {
            return next != -1;
        }

        @Override
        public RateLimitStatus getRateLimitStatus() {
            return null;
        }

        @Override
        public int getAccessLevel() {
            return 0;
        }
    };
}
Also used : Status(twitter4j.Status) RateLimitStatus(twitter4j.RateLimitStatus) QueryResult(twitter4j.QueryResult) Mastodon4jRequestException(com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException) GeoQuery(twitter4j.GeoQuery) Query(twitter4j.Query) Public(com.sys1yagi.mastodon4j.api.method.Public)

Example 9 with QueryResult

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

the class NearbyTweets method getTweets.

public void getTweets() {
    canRefresh = false;
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Twitter twitter = Utils.getTwitter(context, DrawerActivity.settings);
                boolean manualLoc = sharedPrefs.getBoolean("manually_config_location", false);
                int i = 0;
                while (!connected && i < 5 && !manualLoc) {
                    try {
                        Thread.sleep(1500);
                    } catch (Exception e) {
                    }
                    i++;
                }
                double latitude = -1;
                double longitude = -1;
                if (manualLoc) {
                    // need to query yahoos api for the location...
                    double[] loc = getLocationFromYahoo(sharedPrefs.getInt("woeid", 2379574));
                    latitude = loc[0];
                    longitude = loc[1];
                } else {
                    // set it from the location client
                    Location location = mLastLocation;
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                }
                query = new Query();
                query.setGeoCode(new GeoLocation(latitude, longitude), 10, Query.MILES);
                QueryResult result = twitter.search(query);
                if (result.hasNext()) {
                    hasMore = true;
                    query = result.nextQuery();
                } else {
                    hasMore = false;
                }
                for (Status s : result.getTweets()) {
                    statuses.add(s);
                }
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        adapter = new TimelineArrayAdapter(context, statuses);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);
                        LinearLayout spinner = (LinearLayout) layout.findViewById(R.id.list_progress);
                        spinner.setVisibility(View.GONE);
                    }
                });
            } catch (Throwable e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Toast.makeText(context, getString(R.string.error), Toast.LENGTH_SHORT).show();
                        } catch (IllegalStateException e) {
                        // not attached to activity
                        }
                    }
                });
            }
            canRefresh = true;
        }
    }).start();
}
Also used : Status(twitter4j.Status) Query(twitter4j.Query) Twitter(twitter4j.Twitter) DrawerActivity(com.klinker.android.twitter.activities.drawer_activities.DrawerActivity) Activity(android.app.Activity) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) QueryResult(twitter4j.QueryResult) TimelineArrayAdapter(com.klinker.android.twitter.adapters.TimelineArrayAdapter) GeoLocation(twitter4j.GeoLocation) LinearLayout(android.widget.LinearLayout) GeoLocation(twitter4j.GeoLocation) Location(android.location.Location)

Example 10 with QueryResult

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

the class SearchedTrendsActivity method onRefreshStarted.

public void onRefreshStarted() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            final long topId;
            if (tweets.size() > 0) {
                topId = tweets.get(0).getId();
            } else {
                topId = 0;
            }
            try {
                Twitter twitter = Utils.getTwitter(context, settings);
                query = new Query(searchQuery);
                QueryResult result = twitter.search(query);
                tweets.clear();
                for (twitter4j.Status status : result.getTweets()) {
                    tweets.add(status);
                }
                if (result.hasNext()) {
                    query = result.nextQuery();
                    hasMore = true;
                } else {
                    hasMore = false;
                }
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        int top = 0;
                        for (int i = 0; i < tweets.size(); i++) {
                            if (tweets.get(i).getId() == topId) {
                                top = i;
                                break;
                            }
                        }
                        adapter = new TimelineArrayAdapter(context, tweets);
                        listView.setAdapter(adapter);
                        listView.setVisibility(View.VISIBLE);
                        listView.setSelection(top);
                        spinner.setVisibility(View.GONE);
                        mPullToRefreshLayout.setRefreshing(false);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                ((Activity) context).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        spinner.setVisibility(View.GONE);
                        mPullToRefreshLayout.setRefreshing(false);
                    }
                });
            }
        }
    }).start();
}
Also used : QueryResult(twitter4j.QueryResult) Query(twitter4j.Query) Status(twitter4j.Status) Twitter(twitter4j.Twitter) ComposeActivity(com.klinker.android.twitter.activities.compose.ComposeActivity) SettingsActivity(com.klinker.android.twitter.settings.SettingsActivity) Activity(android.app.Activity) TimelineArrayAdapter(com.klinker.android.twitter.adapters.TimelineArrayAdapter) TwitterException(twitter4j.TwitterException)

Aggregations

QueryResult (twitter4j.QueryResult)18 Twitter (twitter4j.Twitter)17 Status (twitter4j.Status)16 Activity (android.app.Activity)12 Query (twitter4j.Query)12 TimelineArrayAdapter (com.klinker.android.twitter.adapters.TimelineArrayAdapter)8 TwitterException (twitter4j.TwitterException)5 ComposeActivity (com.klinker.android.twitter.activities.compose.ComposeActivity)3 SettingsActivity (com.klinker.android.twitter.settings.SettingsActivity)3 GeoLocation (twitter4j.GeoLocation)3 View (android.view.View)2 AbsListView (android.widget.AbsListView)2 DrawerActivity (com.klinker.android.twitter.activities.drawer_activities.DrawerActivity)2 ArrayList (java.util.ArrayList)2 TwitterEndpoint (org.apache.camel.component.twitter.TwitterEndpoint)2 AsyncListView (org.lucasr.smoothie.AsyncListView)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 Location (android.location.Location)1 LinearLayout (android.widget.LinearLayout)1 Mastodon4jRequestException (com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException)1