使用Ruby和Twitter可以收集用户的所有时间表吗?

时间:2015-05-23 20:21:44

标签: ruby twitter scrape timeline tweets

我试图检索用户的时间表。 api说你可以获得(最多)3,200条推文。我似乎只知道如何使用此代码获得20:

def gather_tweets_from(user)
  tweets = []
  file = File.open("tweets_from.txt","w")
  client.user_timeline(user).each { |tweet| 
    file.puts tweet.text
  }
end
gather_tweets_from(user)

请帮帮我, 谢谢:))

2 个答案:

答案 0 :(得分:1)

user_timeline函数允许您指定某些选项。您正在寻找的是

if (Meteor.isServer) {

    Meteor.startup(function () {

    });

    Meteor.methods({

        getApiResult: function() {

           try {
            var pipeline = [{$match: {"ACTIVE": 1}}, {"$group": {"_id": "$VARIENTS.NAME"}}, {
                "$project": {
                    "_id": 0,
                    "TEMPLATE_NAME": "$_id"
                }
            }];

            console.log("display pipeline");
            console.log(pipeline);
            var result = nodeDB.aggregate(pipeline);

            console.log("display result", result);

            for (i = 0; i < result.length; i++) {
                var Temp_Name = result[i];
                console.log("temp name is ", Temp_Name);
                //productDB.insert({ Temp_Name: $(". Temp_Name").val()});
                return result;

            }

        }catch (_error) {
            return false;
        }


      }

    });

}

http://www.rubydoc.info/gems/twitter/Twitter/REST/Timelines:user_timeline

答案 1 :(得分:0)

您可以通过设置max_id参数http://www.rubydoc.info/gems/twitter/Twitter/REST/Timelines

来实现
#set max_id to max value
mnid = 999999999999999999
#set number of tweets you want to get 
max_no_tweets = 3500
count = 0
while(count < max_no_tweets)
    #each loop gets 200 tweet
    client.user_timeline('elonmusk', :count => 200, :max_id => mnid).each do |tweet|
        puts(tweet.text) if tweet.is_a?(Twitter::Tweet)
        #to get the next 200 tweet
        mnid = [mnid,tweet.id].min
        count+=1
    end
end