Guest User

Untitled

a guest
Apr 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'twitter'
  3. require 'erb'
  4. require 'trollop'
  5.  
  6. options = Trollop.options do
  7. opt :tag, "Hashtag to use as a filter",
  8. :type => :string, :required => true
  9.  
  10. opt :from, "Restrict to tweets from a specific user",
  11. :type => :string
  12.  
  13. opt :template, "ERB template used to render the stream",
  14. :type => :string, :required => true
  15.  
  16. opt :output, "Output to this file",
  17. :type => :string
  18. end
  19.  
  20. class Tweetstream
  21. def initialize(hashtag, from = nil)
  22. @hashtag, @from = sanitize_hashtag(hashtag), from
  23. end
  24.  
  25. def render(template_path)
  26. perform_search!
  27. template = File.read(template_path)
  28. ERB.new(template).result(binding)
  29. end
  30.  
  31. private
  32.  
  33. def sanitize_hashtag(hashtag)
  34. "##{hashtag}".squeeze('#')
  35. end
  36.  
  37. def perform_search!
  38. query = Twitter::Search.new(@hashtag)
  39. query = query.from(@from) if @from
  40. @tweets = query.map { |tweet| tweet }
  41. end
  42. end
  43.  
  44. stream = Tweetstream.new(options[:tag], options[:from])
  45. output = stream.render(options[:template])
  46.  
  47. if options[:output]
  48. File.open(options[:output], 'w') { |io| io.write(output) }
  49. else
  50. puts output
  51. end
Add Comment
Please, Sign In to add comment