Guest User

Untitled

a guest
Apr 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #
  3. # usage: tweets_to_csv.rb <search term> <output filename>
  4. #
  5. #
  6. # This is the 'dumb' approach
  7. # needs reworking if you want something that handles duplicates and whatnot
  8.  
  9. require 'rubygems'
  10. require 'csv'
  11. require 'twitter_search'
  12.  
  13. query = ARGV[0]
  14. output_filename = ARGV[1]
  15. outfile = File.open(output_filename, 'wb')
  16.  
  17. # setup my twitter search client
  18. client = TwitterSearch::Client.new('tweet_to_csv')
  19.  
  20. CSV::Writer.generate(outfile) do |csv|
  21. # csv headers
  22. csv << ['id', 'created_at', 'from_user', 'to_user', 'body']
  23.  
  24. # start at page 1
  25. page = 1
  26. tweets = client.query(:q => query, :rpp => '100', :page => page)
  27. # while there are tweets to be had
  28. while tweets.count > 0
  29. # increment page
  30. page += 1
  31. # print tweets
  32. tweets.each do |tweet|
  33. # print line of csv
  34. csv << [tweet.id, tweet.created_at, tweet.from_user, tweet.to_user, tweet.text]
  35. end
  36. # been getting strange timeout issues
  37. # i think twitter is rate limiting
  38. # sleeping for a couple seconds seems to make it not do that
  39. sleep(3)
  40. # fetch the next page
  41. tweets = client.query(:q => query, :rpp => '100', :page => page)
  42. end
  43. end
  44.  
  45. outfile.close
Add Comment
Please, Sign In to add comment