Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'twitter'
- require 'erb'
- require 'trollop'
- options = Trollop.options do
- opt :tag, "Hashtag to use as a filter",
- :type => :string, :required => true
- opt :from, "Restrict to tweets from a specific user",
- :type => :string
- opt :template, "ERB template used to render the stream",
- :type => :string, :required => true
- opt :output, "Output to this file",
- :type => :string
- end
- class Tweetstream
- def initialize(hashtag, from = nil)
- @hashtag, @from = sanitize_hashtag(hashtag), from
- end
- def render(template_path)
- perform_search!
- template = File.read(template_path)
- ERB.new(template).result(binding)
- end
- private
- def sanitize_hashtag(hashtag)
- "##{hashtag}".squeeze('#')
- end
- def perform_search!
- query = Twitter::Search.new(@hashtag)
- query = query.from(@from) if @from
- @tweets = query.map { |tweet| tweet }
- end
- end
- stream = Tweetstream.new(options[:tag], options[:from])
- output = stream.render(options[:template])
- if options[:output]
- File.open(options[:output], 'w') { |io| io.write(output) }
- else
- puts output
- end
Add Comment
Please, Sign In to add comment