bin/rabbiter in rabbit-0.9.2 vs bin/rabbiter in rabbit-0.9.3

- old
+ new

@@ -1,96 +1,97 @@ #!/usr/bin/env ruby # -*- ruby -*- -require 'rubygems' -require 'password' -require 'tweetstream' -require 'drb' -require "optparse" -require "ostruct" +require 'drb/drb' -class Rabbiter - def initialize(options) - @options = options - @rabbit = DRb::DRbObject.new_with_uri(@options.rabbit_uri) - @tweet = nil - end +require 'rabbit/console' +require 'rabbit/twitter' - def start - @tweet = TweetStream::Daemon.new(@options.username, - @options.password, - "rabbiter") - @tweet.track(@options.keyword) do |status| - begin - comment = "#{status.user.screen_name}: #{clean_text(status.text)}" - @rabbit.append_comment(comment) - rescue DRb::DRbConnError - puts "Rabbiter: #{$!.class}: #{$!.message}" - end +def parse(args=ARGV, logger=nil) + Rabbit::Console.parse!(args, logger) do |opts, options| + options.rabbit_uri = "druby://localhost:10101" + options.filters = [] + options.user_languages = [] + + opts.separator "" + + opts.on("--rabbit-uri=URI", + "Rabbit's dRuby URI", + "(#{options.rabbit_uri})") do |uri| + options.rabbit_uri = uri end - end - def stop - if @tweet - @tweet.stop + opts.on("--filter=FILTER", + "Filter word for search Twitter.", + "To use multiple filters, use this option multiple.") do |filter| + options.filters << filter end - end - private - def clean_text(text) - remove_ustream_link(remove_hash_tag(text)) - end + opts.on("--user-language=LANGUAGE", + "Filter by user language.", + "(e.g.: ja, en)", + "To use multiple language, use this option multiple.") do |language| + options.user_languages << language + end - def remove_hash_tag(text) - text.gsub(/\##{Regexp.escape(@options.keyword)}/, "") - end + opts.separator "Deprecated" - def remove_ustream_link(text) - text.gsub(/\(.* live at http:\/\/ustre\.am\/.*\)/, "") + opts.on("--keyword=KEYWORD", + "Deprecated. Use --filter instead.") do |keyword| + options.filters << keyword + end end end -options = OpenStruct.new -options.keyword = 'ruby' -options.rabbit_uri = 'druby://localhost:10101' -options.username = ENV['USER'] || ENV['USERNAME'] -options.password = '' +def target?(status, options) + return true if options.user_languages.empty? + options.user_languages.include?(status["user"]["lang"]) +end -parser = OptionParser.new do |opts| - opts.on("--keyword=KEYWROD", - "Keyword for search Twitter", - "(#{options.keyword})") do |keyword| - options.keyword = keyword +def clean_text(text, filters) + remove_ustream_link(remove_hash_tag(text, filters)) +end + +def remove_hash_tag(text, filters) + hash_tag_regexps = filters.collect do |filter| + if filter.start_with?("#") + Regexp.escape(filter) + else + Regexp.escape("\##{filter}") + end end + text.gsub(Regexp.union(*hash_tag_regexps), "") +end - opts.on("--rabbit-uri=URI", - "Rabbit's dRuby URI", - "(#{options.rabbit_uri})") do |uri| - options.rabbit_uri = uri +def remove_ustream_link(text) + text.gsub(/\(.* live at http:\/\/ustre\.am\/.*\)/, "") +end + +def main + options, logger = parse + if options.filters.empty? + logger.error("must specify one or more filters by --filter") + exit(false) end - opts.on("--username=USERNAME", - "Username for Twitter", - "(#{options.username})") do |username| - options.username = username + rabbit = DRbObject.new_with_uri(options.rabbit_uri) + twitter = Rabbit::Twitter.new(logger) + twitter.start(*options.filters) do |status| + next unless target?(status, options) + begin + text = clean_text(status['text'], options.filters) + comment = "@#{status['user']['screen_name']}: #{text}" + rabbit.append_comment(comment) + rescue DRb::DRbConnError + logger.error("Rabbiter: #{$!.class}: #{$!.message}") + end end - opts.on("--password=PASSWORD", - "User password for Twitter", - "(#{options.password})") do |password| - options.password = password + main_loop = GLib::MainLoop.new + trap("INT") do + twitter.close + main_loop.quit end + main_loop.run end -parser.parse!(ARGV) - -if options.password.empty? - options.password = Password.get("Password for #{options.username}: ") -end - -rabbiter = Rabbiter.new(options) - -Signal.trap("INT") do - rabbiter.stop -end - -rabbiter.start +main