├── data └── .gitkeep ├── icon └── .gitkeep ├── .gitignore ├── Gemfile ├── make_booth.rb ├── lib ├── make_booth.rb └── make_booth │ ├── cli.rb │ ├── server.rb │ └── stream.rb ├── MIT-LICENSE └── README.md /data/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /icon/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Gemfile.lock 2 | data/* 3 | icon/* 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'http://rubygems.org' 2 | 3 | gem 'eventmachine' 4 | gem 'em-websocket' 5 | gem 'em-websocket-client' 6 | gem 'json' if RUBY_VERSION < '1.9' 7 | gem 'growl' 8 | gem 'thor' 9 | -------------------------------------------------------------------------------- /make_booth.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # -*- coding: utf-8 -*- 3 | 4 | root = File.dirname(__FILE__) 5 | $LOAD_PATH.unshift File.join(root, 'lib') 6 | ENV['BUNDLE_GEMFILE'] ||= File.join(root, 'Gemfile') 7 | 8 | require 'rubygems' 9 | require 'bundler' 10 | Bundler.require 11 | require 'json' unless defined? JSON 12 | require 'make_booth' 13 | 14 | MakeBooth::CLI.start ARGV 15 | -------------------------------------------------------------------------------- /lib/make_booth.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | module MakeBooth 4 | ROOT = File.join(File.dirname(__FILE__), '..') 5 | DATA_DIR = File.join(ROOT, 'data') 6 | ICON_DIR = File.join(ROOT, 'icon') 7 | DATA_PATH = File.join(DATA_DIR, 'data.json') 8 | 9 | autoload :CLI, 'make_booth/cli' 10 | autoload :Server, 'make_booth/server' 11 | autoload :Stream, 'make_booth/stream' 12 | end 13 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Kazuya Takeshima 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /lib/make_booth/cli.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | module MakeBooth 4 | class CLI < Thor 5 | desc 'stream', 'Start receive Make::Booth activity stream.' 6 | method_option :debug, :aliases => '-d', :type => :boolean, :default => false, 7 | :desc => 'Connect to emulated local server.' 8 | method_option :save, :aliases => '-s', :type => :boolean, :default => true, 9 | :desc => 'Save received json data. (No save if debug mode.)' 10 | method_option :growl, :aliases => '-g', :type => :boolean, :default => true, 11 | :desc => 'Notify received data.' 12 | method_option :log, :aliases => '-l', :type => :boolean, :default => true, 13 | :desc => 'Log received data.' 14 | def stream 15 | MakeBooth::Stream.start(symbolize_keys(options)) 16 | end 17 | 18 | desc 'server', 'Emulate Make::Booth activity stream server' 19 | method_option :interval, :aliases => '-i', :type => :numeric, :default => Server::INTERVAL, 20 | :desc => 'Interval between send the json data.' 21 | def server 22 | MakeBooth::Server.start(symbolize_keys(options)) 23 | end 24 | 25 | private 26 | def symbolize_keys(hash) 27 | other = {} 28 | hash.keys.each do |key| 29 | other[key.to_sym] = hash[key] 30 | end 31 | other 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/make_booth/server.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | module MakeBooth 4 | module Server 5 | HOST = 'localhost' 6 | PORT = 5678 7 | INTERVAL = 10 8 | 9 | @connections = [] 10 | @data = JSON.load(open(DATA_PATH)) rescue [] 11 | 12 | module_function 13 | 14 | def start(options = {}) 15 | interval = options.fetch :interval, INTERVAL 16 | 17 | EventMachine.run do 18 | EventMachine::WebSocket.start(:host => HOST, :port => PORT) do |con| 19 | con.onopen do 20 | $stdout.puts "MakeBooth#Server: connection opened #{con}" 21 | 22 | @connections.push(con) unless @connections.include?(con) 23 | end 24 | 25 | con.onclose do 26 | $stderr.puts "MakeBooth#Server: connection closed #{con}" 27 | 28 | @connections.delete(con) 29 | end 30 | end 31 | 32 | EventMachine.add_periodic_timer(interval) do 33 | $stdout.puts "MakeBooth#Server: send data to #{@connections.size} connections" 34 | 35 | datum = @data.shift 36 | @data << datum 37 | 38 | @connections.each do |con| 39 | con.send(datum.to_json) 40 | end 41 | end 42 | 43 | $stdout.puts "MakeBooth#Server: start web socket server on ws://#{HOST}:#{PORT}" 44 | end 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Make::Booth Stream 2 | 3 | Make::Booth のストリームをアレコレするアプリ 4 | 5 | ## 必要なもの 6 | 7 | Ruby 8 | Growl 9 | 10 | ### RubyGems 11 | 12 | eventmachine 13 | em-websocket 14 | em-websocket-client 15 | json (Ruby 1.9 未満の場合) 16 | growl 17 | thor 18 | 19 | ## インストール 20 | 21 | git clone git://github.com/mitukiii/make-booth-stream-for-ruby.git make-booth-stream 22 | cd make-booth-stream 23 | bundle install 24 | 25 | ## 使い方 26 | 27 | Make::Booth のストリームを受信し Growl 通知 28 | 29 | $ ./make_booth.rb stream 30 | 31 | Make::Booth のストリームをエミュレートする WebSocket サーバを起動 32 | 33 | $ ./make_booth.rb server 34 | 35 | ## ヘルプ 36 | 37 | ヘルプ 38 | 39 | $ ./make_booth.rb 40 | Tasks: 41 | make_booth.rb help [TASK] # Describe available tasks or one specific task 42 | make_booth.rb server # Emulate Make::Booth activity stream server 43 | make_booth.rb stream # Start receive Make::Booth activity stream. 44 | 45 | stream のヘルプ 46 | 47 | $ ./make_booth.rb help stream 48 | Usage: 49 | make_booth.rb stream 50 | 51 | Options: 52 | -d, [--debug] # Connect to emulated local server. 53 | -s, [--save] # Save received json data. (No save if debug mode.) 54 | # Default: true 55 | -g, [--growl] # Notify received data. 56 | # Default: true 57 | -l, [--log] # Log received data. 58 | # Default: true 59 | 60 | Start receive Make::Booth activity stream. 61 | 62 | server のヘルプ 63 | 64 | $ ./make_booth.rb help server 65 | Usage: 66 | make_booth.rb server 67 | 68 | Options: 69 | -i, [--interval=N] # Interval between send the json data. 70 | # Default: 10 71 | 72 | Emulate Make::Booth activity stream server 73 | 74 | ## コピーライト 75 | 76 | Released under the MIT license 77 | [http://www.opensource.org/licenses/MIT](http://www.opensource.org/licenses/MIT) 78 | -------------------------------------------------------------------------------- /lib/make_booth/stream.rb: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | require 'open-uri' 4 | 5 | module MakeBooth 6 | module Stream extend self 7 | ACTIVITY_URI = 'ws://ws.makebooth.com:5678' 8 | HOST = 'http://makebooth.com' 9 | IMAGE_HOST = 'http://img.makebooth.com' 10 | IMAGE_SMALL = IMAGE_HOST + '/scale/c.50x50.' 11 | 12 | DEBUG_URI = 'ws://localhost:5678' 13 | 14 | def start(options = {}) 15 | debug = options.fetch :debug, false 16 | save = options.fetch :save, true 17 | growl = options.fetch :growl, true 18 | log = options.fetch :log, true 19 | 20 | save = false if debug 21 | 22 | EventMachine.run do 23 | uri = debug ? DEBUG_URI : ACTIVITY_URI 24 | con = EventMachine::WebSocketClient.connect(uri) 25 | 26 | con.stream do |msg| 27 | $stdout.puts "MakeBooth#Stream: receive message" 28 | 29 | datum = JSON.parse(msg) 30 | 31 | save_datum(datum) if save 32 | growl_datum(datum) if growl 33 | log_datum(datum) if log 34 | end 35 | 36 | con.disconnect do 37 | $stderr.puts 'MakeBooth#Stream: disconnected' 38 | EventMachine.stop_event_loop 39 | end 40 | end 41 | end 42 | 43 | private 44 | def save_datum(datum) 45 | data = JSON.load(open(DATA_PATH)) rescue [] 46 | data << datum 47 | open(DATA_PATH, 'w') { |f| f.puts JSON.pretty_generate(data) } 48 | end 49 | 50 | def growl_datum(datum) 51 | text = strip_tags(datum['text']) 52 | 53 | if datum['user_image_file_name'] 54 | icon_name = datum['user_image_file_name'] 55 | image_uri = IMAGE_SMALL + icon_name 56 | else 57 | icon_name = 'default_icon.png' 58 | image_uri = HOST + '/img/' + icon_name 59 | end 60 | 61 | icon_path = File.join(ICON_DIR, icon_name) 62 | 63 | unless File.exists? icon_path 64 | open(icon_path, 'w') { |f| f.print open(image_uri).read } 65 | end 66 | 67 | Growl.notify text, :icon => icon_path 68 | end 69 | 70 | def log_datum(datum) 71 | text = strip_tags(datum['text']) 72 | date = DateTime.parse(datum['created_at']) 73 | $stdout.puts ' ' + text 74 | $stdout.puts ' link: ' + HOST + datum['image_file_link_path'] 75 | $stdout.puts ' date: ' + date.strftime('%Y/%m/%d %H:%M') 76 | end 77 | 78 | def strip_tags(text) 79 | text.gsub(/<\/?[^>]*>/, '') 80 | end 81 | end 82 | end 83 | --------------------------------------------------------------------------------