Pagination for search Tweets

Feb 3, 2015 at 9:14 PM
Hi, I have this code for searching tweets
    private async Task searchKeyword(string q)
    {
        string defaultImage;
        string lang = "en";
        string selectedLang = comboBox1.SelectedItem.ToString();

        ulong sinceID = 1;
        int Count = 10;
        int maxStatuses = Convert.ToInt32(numericUpDown3.Value);
        var statusList = new List<Status>();
        int lastStatusCount = 0;

        switch (selectedLang)
        {

            case "English":
                lang = "en";
                break;

            case "Romanian":
                lang = "ro";
                break;

            case "Italian":
                lang = "it";
                break;

            case "French":
                lang = "fr";
                break;

            default:
                lang = "en";
                break;

        }

        var auth = apiConnect();
        var twitterCtx = new TwitterContext(auth);

        // last tweet processed on previous query set

        var searchResponse =
            await
            (from search in twitterCtx.Search
             where search.Type == SearchType.Search &&
                   search.Query == q &&
                   search.Count == Count &&
                   search.SinceID == sinceID &&
                   search.SearchLanguage == lang &&
                   search.ResultType == ResultType.Recent
             select search)
            .SingleOrDefaultAsync();

        if (searchResponse != null && searchResponse.Statuses != null)
        {

            List<Status> newStatuses = searchResponse.Statuses;

            maxIDlim = newStatuses.Min(status => status.StatusID) - 1;

            statusList.AddRange(newStatuses);

            do
            {
                label19.Text = maxIDlim.ToString();

                searchResponse =
                    await
                    (from search in twitterCtx.Search
                     where search.Type == SearchType.Search &&
                           search.Query == q &&
                           search.Count == Count &&
                           search.SinceID == sinceID &&
                           search.MaxID == maxIDlim &&
                           search.SearchLanguage == lang &&
                           search.ResultType == ResultType.Recent
                     select search)
                    .SingleOrDefaultAsync();

                newStatuses = searchResponse.Statuses;

                maxIDlim = newStatuses.Min(status => status.StatusID) - 1;

                statusList.AddRange(newStatuses);

                lastStatusCount = newStatuses.Count;
                label18.Text = maxIDlim.ToString();
            } while (lastStatusCount != 0 && statusList.Count < maxStatuses);

            listView1.BeginUpdate();
            for (int i = 0; i < statusList.Count; i++)
            {
                Status search = statusList[i];

                if (search.User.DefaultProfileImage)
                {
                    defaultImage = "NO";

                }
                else
                {
                    defaultImage = "YES";
                }

                string[] row = { search.User.ScreenNameResponse, search.Text, defaultImage, search.StatusID.ToString() };
                var listViewItem = new ListViewItem(row);
                listView1.Items.Add(listViewItem);
            }
            listView1.EndUpdate();
        }
    }

When I press Search button, it returns first 20 results, but when I press again it returns same 20 results. How can I get next 20 results?
Coordinator
Feb 4, 2015 at 6:21 PM
Hi,

It looks like you're setting SinceID to 1 every time. After you iterate through queries, SinceID should be updated to the last status ID read so that you don't start reading the same tweets again. It looks like the structure of your algorithm is right. Maybe review Twitter's Working with Timelines to help understand what's happening with this technique. Since you're requesting tweets backwards, the presentation in your UI might be different.

@JoeMayo
Feb 5, 2015 at 12:50 PM
Edited Feb 5, 2015 at 1:05 PM
You said my algorithm is ok but ... for example:
int Count = 10;
and user select to get 12 results
int maxStatuses = Convert.ToInt32(numericUpDown3.Value); // users set 12
I will get 20 results because
} while (lastStatusCount != 0 && statusList.Count < maxStatuses);
and the algorithm can get only 2 results because Count is 10.


Or what I want is to get the number of : (numericUpDown3.Value results on every button press.