Search code examples
ruby-on-railstwitterpagination

How do you retrieve up to 3200 statuses in rails?


Background Building a rails app that gets another user's statuses to store into my database Using gem 'twitter'

Using the module methods, I can call Twitter.user_timeline("username", count: 3200)but it will retrieve at most 200 statuses.

Questions Does there exist a method or code using the gem where I can exceed this limit? or how can i page through my statuses beyond 200?

I have researched that you can reach up to 3200 statuses. I am not looking to exceed it but at least reach the limit.


Solution

  • the following is the code I used along with comments, using max_id as a paramater, running it through the loop then saving it to the database. Hope this is helpful for someone

    // get initial resposne

    //this will return 20 results

    x = Twitter.user_timeline("username")
    

    //save each result the database

    for i in(0..x.length-1)
        tweet = Tweet.create
        tweet.info = x[i].whatever_info
        tweet.save
    end
    

    //now get the last tweet_id and pass it into max_id as part of the request

    last_id = x.last.id  
    prev_id = 0
    

    //now you want to pass the last_id into a new request and loop it until the last_id is equal to the previous_id

    while prev_id != last_id do
    a = Twitter.user_timeline("username", max_count: 200, max_id: last_id)
        for i in(0..x.length-1)
            tweet = Favoritetweet.create
            tweet.info = a[i].whatever_info
            tweet.save
        end
    
        prev_id = last_id
        last_id = x.last.id
    end