From a8bddeb1284dc37e1f1e8cba6044f668da5efa87 Mon Sep 17 00:00:00 2001 From: SHIBATA Hiroshi Date: Fri, 24 Feb 2012 09:33:41 +0900 Subject: [PATCH 001/639] support bundler --- .gitignore | 1 + Gemfile | 7 +++++++ lib/environment.rb | 8 ++++++++ spec/spec_helper.rb | 2 +- 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 lib/environment.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68feb7d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Gemfile.lock \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..5320e3d --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source :rubygems + +gem 'gcalapi' + +group :test do + gem 'rspec' +end diff --git a/lib/environment.rb b/lib/environment.rb new file mode 100644 index 0000000..aeb7356 --- /dev/null +++ b/lib/environment.rb @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +require 'rubygems' + +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) + +require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) +Bundler.require :default if defined?(Bundler) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6ad0089..97d3073 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'plugins')) $LOAD_PATH.unshift(File.dirname(__FILE__)) -require 'rspec' +require 'environment' # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. From 1f93d1e6a010e5a8f80d350a835d131126ebf724 Mon Sep 17 00:00:00 2001 From: SHIBATA Hiroshi Date: Fri, 24 Feb 2012 09:34:47 +0900 Subject: [PATCH 002/639] added require for test group --- spec/spec_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 97d3073..5cee02c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'plugins')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'environment' +Bundler.require :test if defined?(Bundler) # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. From b3cc1e611ee7ea82df7601c7432825a09b1aeb29 Mon Sep 17 00:00:00 2001 From: Daic_h Date: Fri, 24 Feb 2012 11:30:44 +0900 Subject: [PATCH 003/639] rename class name and singleton_method for core --- .gitignore | 3 ++- Gemfile | 2 ++ automatic.rb | 15 ++++++++------- lib/core.rb | 12 ++++++------ 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 68feb7d..ba0996b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -Gemfile.lock \ No newline at end of file +Gemfile.lock +db/*.db \ No newline at end of file diff --git a/Gemfile b/Gemfile index 5320e3d..bc90c4b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,7 @@ source :rubygems +gem 'sqlite3-ruby' +gem 'activerecord' gem 'gcalapi' group :test do diff --git a/automatic.rb b/automatic.rb index 22b0386..8e45ced 100755 --- a/automatic.rb +++ b/automatic.rb @@ -4,14 +4,15 @@ # Author:: 774 # Version:: 12.02-devel # Created:: Feb 18, 2012 -# Updated:: Feb 22, 2012 +# Updated:: Feb 24, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. -basedir = File.dirname(__FILE__) -$:.unshift File.join(basedir, 'lib') -Dir.glob(basedir + '/lib/*.rb').each {|r| - require(File.basename(r, '.rb')) +lib = File.expand_path(File.dirname(__FILE__) + '/lib') +$:.unshift(lib) if File.directory?(lib) && !$:.include?(lib) + +Dir[lib + '/*.rb'].each {|r| + require File.basename(r, '.rb') } if __FILE__ == $0 @@ -32,11 +33,11 @@ begin parser.parse! - print "Loading #{recipe}\n" unless recipe == "" + print "Loading #{recipe}\n" unless recipe.empty? rescue OptionParser::ParseError => err $stderr.puts err.message $stderr.puts parser.help exit 1 end - Automatic.core(recipe) + Core.pipeline(recipe) end diff --git a/lib/core.rb b/lib/core.rb index c946a00..ec413a5 100644 --- a/lib/core.rb +++ b/lib/core.rb @@ -7,26 +7,26 @@ # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. -class Automatic +class Core require 'pathname' require 'yaml' - def self.core(recipe) + def self.pipeline(recipe) basedir = Pathname.new(__FILE__).parent.parent recipe = basedir + 'config/default.yml' if recipe.empty? - Pathname.glob(basedir + 'plugins/*/*.rb').each do |path| + Pathname.glob(basedir + 'plugins/*/*.rb').each {|path| klass_name = path.parent.basename.to_s.split('_').map(&:capitalize).join + path.basename('.rb').to_s.split('_').map(&:capitalize).join autoload klass_name.to_sym, path.to_s - end + } @pipeline = [] - YAML.load(File.read(recipe))['plugins'].each do |plugin| + YAML.load(File.read(recipe))['plugins'].each {|plugin| klass = eval(plugin['module']) @pipeline = klass.new(plugin['config'], @pipeline).run - end + } end end From dfd3e5eb96e1db2e57d61301ee6d6c4cc74ccef3 Mon Sep 17 00:00:00 2001 From: Satoshi Namai Date: Fri, 24 Feb 2012 18:15:23 +0900 Subject: [PATCH 004/639] add a namespace "Automatic" and relocation some files and add a recipe class. add two gems to Gemfile --- Gemfile | 2 + app.rb | 17 ++ automatic.rb | 43 ---- lib/environment.rb => environment.rb | 0 lib/automatic.rb | 59 ++++++ lib/{ => automatic}/feed_parser.rb | 28 +-- lib/{ => automatic}/log.rb | 13 +- lib/automatic/pipeline.rb | 32 +++ lib/automatic/recipe.rb | 30 +++ lib/core.rb | 32 --- plugins/filter/ignore.rb | 54 ++--- plugins/publish/console.rb | 34 ++-- plugins/publish/google_calendar.rb | 292 ++++++++++++++------------- plugins/publish/hatena_bookmark.rb | 144 ++++++------- plugins/store/bookmark.rb | 77 +++---- plugins/subscription/feed.rb | 38 ++-- 16 files changed, 488 insertions(+), 407 deletions(-) create mode 100755 app.rb delete mode 100755 automatic.rb rename lib/environment.rb => environment.rb (100%) create mode 100644 lib/automatic.rb rename lib/{ => automatic}/feed_parser.rb (52%) rename lib/{ => automatic}/log.rb (66%) create mode 100644 lib/automatic/pipeline.rb create mode 100644 lib/automatic/recipe.rb delete mode 100644 lib/core.rb diff --git a/Gemfile b/Gemfile index bc90c4b..502ba9b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,8 @@ source :rubygems gem 'sqlite3-ruby' +gem 'activesupport' +gem 'hashie' gem 'activerecord' gem 'gcalapi' diff --git a/app.rb b/app.rb new file mode 100755 index 0000000..bdbee7b --- /dev/null +++ b/app.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby +# -*- coding: utf-8 -*- +# Name:: Automatic::Ruby +# Author:: 774 +# Version:: 12.02-devel +# Created:: Feb 18, 2012 +# Updated:: Feb 22, 2012 +# Copyright:: 774 Copyright (c) 2012 +# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. + +root_dir = File.expand_path(File.dirname(__FILE__)) +$:.unshift root_dir +$:.unshift root_dir + '/lib' +require 'lib/automatic' + +Automatic.run(root_dir) + diff --git a/automatic.rb b/automatic.rb deleted file mode 100755 index 8e45ced..0000000 --- a/automatic.rb +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env ruby -# -*- coding: utf-8 -*- -# Name:: Automatic::Ruby -# Author:: 774 -# Version:: 12.02-devel -# Created:: Feb 18, 2012 -# Updated:: Feb 24, 2012 -# Copyright:: 774 Copyright (c) 2012 -# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. - -lib = File.expand_path(File.dirname(__FILE__) + '/lib') -$:.unshift(lib) if File.directory?(lib) && !$:.include?(lib) - -Dir[lib + '/*.rb'].each {|r| - require File.basename(r, '.rb') -} - -if __FILE__ == $0 - recipe = "" - require 'optparse' - parser = OptionParser.new do |parser| - parser.version = "12.02-devel" - parser.banner = "#{File.basename($0,".*")} - Usage: #{File.basename($0,".*")} [options] arg" - parser.separator "options:" - parser.on('-c', '--config FILE', String, - "recipe YAML file"){|c| recipe = c} - parser.on('-h', '--help', "show this message"){ - puts parser - exit - } - end - - begin - parser.parse! - print "Loading #{recipe}\n" unless recipe.empty? - rescue OptionParser::ParseError => err - $stderr.puts err.message - $stderr.puts parser.help - exit 1 - end - Core.pipeline(recipe) -end diff --git a/lib/environment.rb b/environment.rb similarity index 100% rename from lib/environment.rb rename to environment.rb diff --git a/lib/automatic.rb b/lib/automatic.rb new file mode 100644 index 0000000..3c6c609 --- /dev/null +++ b/lib/automatic.rb @@ -0,0 +1,59 @@ +#!/usr/bin/env ruby +# Name:: Automatic::Ruby +# Author:: 774 +# Version:: 12.02-devel +# Created:: Feb 18, 2012 +# Updated:: Feb 24, 2012 +# Copyright:: 774 Copyright (c) 2012 +# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. +module Automatic + require 'automatic/recipe' + require 'automatic/pipeline' + require 'automatic/log' + require 'automatic/feed_parser' + + VERSION = "12.02-devel" + + def self.root_dir + @root_dir + end + + def self.plugins_dir + @root_dir + "/plugins/" + end + + def self.config_dir + @root_dir + "/config/" + end + + def self.run(root_dir) + @root_dir = root_dir + recipe_path = "" + require 'optparse' + parser = OptionParser.new do |parser| + parser.banner = "Usage: autorb [options] arg" + parser.separator "options:" + parser.on('-c', '--config FILE', String, + "recipe YAML file"){|c| recipe_path = c} + parser.on('-h', '--help', "show this message") do + puts parser + exit + end + end + + begin + parser.parse! + print "Loading #{recipe_path}\n" unless recipe_path == "" + rescue OptionParser::ParseError => err + $stderr.puts err.message + $stderr.puts parser.help + exit 1 + end + + # recipe treat as an object. + recipe = Automatic::Recipe.new(recipe_path) + Automatic::Pipeline.run(recipe) + end +end + + diff --git a/lib/feed_parser.rb b/lib/automatic/feed_parser.rb similarity index 52% rename from lib/feed_parser.rb rename to lib/automatic/feed_parser.rb index 75e12a5..7892b97 100644 --- a/lib/feed_parser.rb +++ b/lib/automatic/feed_parser.rb @@ -7,28 +7,30 @@ # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. -class FeedParser - require 'rss' - require 'uri' +module Automatic + module FeedParser + require 'rss' + require 'uri' - def self.get_rss(url) - begin - unless url.nil? - feed = URI.parse(url).normalize - open(feed) do |http| - response = http.read - RSS::Parser.parse(response, false) + def self.get_rss(url) + begin + unless url.nil? + feed = URI.parse(url).normalize + open(feed) do |http| + response = http.read + RSS::Parser.parse(response, false) + end end + rescue => e + raise e end - rescue => e - raise e end end end if __FILE__ == $0 url = ARGV.shift || abort("Usage: feed_parser.rb ") - rss_results = FeedParser.get_rss(url) + rss_results = Automatic::FeedParser.get_rss(url) require 'pp' pp links end diff --git a/lib/log.rb b/lib/automatic/log.rb similarity index 66% rename from lib/log.rb rename to lib/automatic/log.rb index 6813551..1f37b33 100644 --- a/lib/log.rb +++ b/lib/automatic/log.rb @@ -7,15 +7,18 @@ # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. -class Log - def self.puts(level, message) - t = Time.now.strftime("%Y/%m/%d %X") - print "#{t} [#{level}] #{message}\n" +module Automatic + module Log + def self.puts(level, message) + t = Time.now.strftime("%Y/%m/%d %X") + print "#{t} [#{level}] #{message}\n" + end end end if __FILE__ == $0 level = ARGV.shift || abort("Usage: log.rb ") message = ARGV.shift - Log.puts(level, message) + Automatic::Log.puts(level, message) end + diff --git a/lib/automatic/pipeline.rb b/lib/automatic/pipeline.rb new file mode 100644 index 0000000..f3eb59f --- /dev/null +++ b/lib/automatic/pipeline.rb @@ -0,0 +1,32 @@ +#!/usr/bin/env ruby +# -*- coding: utf-8 -*- +# Name:: Automatic::Core +# Author:: 774 +# Created:: Feb 22, 2012 +# Updated:: Feb 22, 2012 +# Copyright:: 774 Copyright (c) 2012 +# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. +require 'active_support/core_ext' + +module Automatic + module Plugin end + + module Pipeline + def self.plugin_load(module_name) + type, filename = module_name.underscore.split('_', 2) + path = Automatic.plugins_dir + "#{type}/#{filename}.rb" + Automatic::Plugin.autoload module_name.to_sym, path + end + + def self.run(recipe) + raise "NoRecipeError" if recipe.nil? + pipeline = [] + recipe.each_plugin do |plugin| + plugin_load(plugin.module) + klass = Automatic::Plugin.const_get(plugin.module) + pipeline = klass.new(plugin.config, pipeline).run + end + end + end +end + diff --git a/lib/automatic/recipe.rb b/lib/automatic/recipe.rb new file mode 100644 index 0000000..baa7f19 --- /dev/null +++ b/lib/automatic/recipe.rb @@ -0,0 +1,30 @@ +# Name:: Automatic::Ruby +# Author:: 774 +# Version:: 12.02-devel +# Created:: Feb 18, 2012 +# Updated:: Feb 24, 2012 +# Copyright:: 774 Copyright (c) 2012 +# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. +require 'hashie' +require 'yaml' + +module Automatic + class Recipe + attr_reader :procedure + + def initialize(path) + path = Automatic.config_dir + 'default.yml' if path.to_s.empty? + load_recipe(path) + end + + def load_recipe(path) + @procedure = Hashie::Mash.new(YAML.load(File.read(path))) + end + + def each_plugin + @procedure.plugins.each do |plugin| + yield plugin + end + end + end +end diff --git a/lib/core.rb b/lib/core.rb deleted file mode 100644 index ec413a5..0000000 --- a/lib/core.rb +++ /dev/null @@ -1,32 +0,0 @@ -#!/usr/bin/env ruby -# -*- coding: utf-8 -*- -# Name:: Automatic::Core -# Author:: 774 -# Created:: Feb 22, 2012 -# Updated:: Feb 22, 2012 -# Copyright:: 774 Copyright (c) 2012 -# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. - -class Core - require 'pathname' - require 'yaml' - - def self.pipeline(recipe) - basedir = Pathname.new(__FILE__).parent.parent - recipe = basedir + 'config/default.yml' if recipe.empty? - - Pathname.glob(basedir + 'plugins/*/*.rb').each {|path| - klass_name = - path.parent.basename.to_s.split('_').map(&:capitalize).join + - path.basename('.rb').to_s.split('_').map(&:capitalize).join - - autoload klass_name.to_sym, path.to_s - } - - @pipeline = [] - YAML.load(File.read(recipe))['plugins'].each {|plugin| - klass = eval(plugin['module']) - @pipeline = klass.new(plugin['config'], @pipeline).run - } - end -end diff --git a/plugins/filter/ignore.rb b/plugins/filter/ignore.rb index d243203..0e9fa25 100644 --- a/plugins/filter/ignore.rb +++ b/plugins/filter/ignore.rb @@ -7,34 +7,36 @@ # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. -class FilterIgnore - def initialize(config, pipeline=[]) - @config = config - @pipeline = pipeline - end - - def exclude(link) - detection = false - @config['exclude'].each {|e| - detection = true if link.include?(e.chomp) - } - if detection - Log.puts("info", "Excluded: #{link}") +module Automatic::Plugin + class FilterIgnore + def initialize(config, pipeline=[]) + @config = config + @pipeline = pipeline end - detection - end - def run - return_feeds = [] - @pipeline.each {|feeds| - ignore = false - unless feeds.nil? - feeds.items.each {|feed| - ignore = true if exclude(feed.link) - } + def exclude(link) + detection = false + @config['exclude'].each {|e| + detection = true if link.include?(e.chomp) + } + if detection + Automatic::Plugin::Log.puts("info", "Excluded: #{link}") end - return_feeds << feeds unless ignore - } - return_feeds + detection + end + + def run + return_feeds = [] + @pipeline.each {|feeds| + ignore = false + unless feeds.nil? + feeds.items.each {|feed| + ignore = true if exclude(feed.link) + } + end + return_feeds << feeds unless ignore + } + return_feeds + end end end diff --git a/plugins/publish/console.rb b/plugins/publish/console.rb index c4bbf05..06ba73c 100644 --- a/plugins/publish/console.rb +++ b/plugins/publish/console.rb @@ -7,23 +7,25 @@ # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. -class PublishConsole - require 'pp' - attr_accessor :hb +module Automatic::Plugin + class PublishConsole + require 'pp' + attr_accessor :hb - def initialize(config, pipeline=[]) - @config = config - @pipeline = pipeline - end + def initialize(config, pipeline=[]) + @config = config + @pipeline = pipeline + end - def run - @pipeline.each {|feeds| - unless feeds.nil? - feeds.items.each {|feed| - pp feed - } - end - } - @pipeline + def run + @pipeline.each {|feeds| + unless feeds.nil? + feeds.items.each {|feed| + pp feed + } + end + } + @pipeline + end end end diff --git a/plugins/publish/google_calendar.rb b/plugins/publish/google_calendar.rb index 6cd2eaa..a754feb 100644 --- a/plugins/publish/google_calendar.rb +++ b/plugins/publish/google_calendar.rb @@ -7,171 +7,173 @@ # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. -class Googlecalendar - attr_accessor :user, :feed +module Automatic::Plugin + class Googlecalendar + attr_accessor :user, :feed - def initialize - @user = { - "username" => "", - "password" => "" - } - @feed = 'http://www.google.com/calendar/feeds/default/private/full' - end + def initialize + @user = { + "username" => "", + "password" => "" + } + @feed = 'http://www.google.com/calendar/feeds/default/private/full' + end - def add(arg) - weekdays = [ '日', '月', '火', '水', '木', '金', '土' ] - date = nil - time_st = nil - time_en = nil - text = '' - location = '' + def add(arg) + weekdays = [ '日', '月', '火', '水', '木', '金', '土' ] + date = nil + time_st = nil + time_en = nil + text = '' + location = '' - # Parse Date - require 'date' - today = Date.today - case arg - when /^今日\s*/ - date = today - text = $' - when /^(明日|あした|あす)\s*/ - date = today + 1 - text = $' - when /^(明後日|あさって)\s*/ - date = today + 2 - text = $' - when /^(日|月|火|水|木|金|土)曜(日)?\s*/ - date_offset = (weekdays.index($1) - today.wday + 7) % 7 - date_offset += 7 if date_offset == 0 - date = today + date_offset - text = $' - when /^([0-9]+\/[0-9]+\/[0-9]+)\s*/ - # yyyy/mm/dd - datestr = $1 - text = $' - begin - date = Date.parse(datestr) - rescue ArgumentError - puts "不正な日付形式-1: [#{datestr}]" - exit - end - when /^([0-9]+\/[0-9]+)\s*/ - # mm/dd - datestr = $1 - text = $' - begin - date = Date.parse(datestr) - rescue ArgumentError - puts "不正な日付形式-2: [#{datestr}]" - exit - end - while date < today - date = date >> 12 - end - when /^([0-9]+)\s*/ - datestr = $1 - text = $' - case datestr.length - when 2 - datestr = datestr.slice(0..0) + "/" + datestr.slice(1..1) - when 3 - datestr = datestr.slice(0..0) + "/" + datestr.slice(1..2) - when 4 - datestr = datestr.slice(0..1) + "/" + datestr.slice(2..3) - else - puts "不正な日付形式-3: [#{datestr}]" - exit + # Parse Date + require 'date' + today = Date.today + case arg + when /^今日\s*/ + date = today + text = $' + when /^(明日|あした|あす)\s*/ + date = today + 1 + text = $' + when /^(明後日|あさって)\s*/ + date = today + 2 + text = $' + when /^(日|月|火|水|木|金|土)曜(日)?\s*/ + date_offset = (weekdays.index($1) - today.wday + 7) % 7 + date_offset += 7 if date_offset == 0 + date = today + date_offset + text = $' + when /^([0-9]+\/[0-9]+\/[0-9]+)\s*/ + # yyyy/mm/dd + datestr = $1 + text = $' + begin + date = Date.parse(datestr) + rescue ArgumentError + puts "不正な日付形式-1: [#{datestr}]" + exit + end + when /^([0-9]+\/[0-9]+)\s*/ + # mm/dd + datestr = $1 + text = $' + begin + date = Date.parse(datestr) + rescue ArgumentError + puts "不正な日付形式-2: [#{datestr}]" + exit + end + while date < today + date = date >> 12 + end + when /^([0-9]+)\s*/ + datestr = $1 + text = $' + case datestr.length + when 2 + datestr = datestr.slice(0..0) + "/" + datestr.slice(1..1) + when 3 + datestr = datestr.slice(0..0) + "/" + datestr.slice(1..2) + when 4 + datestr = datestr.slice(0..1) + "/" + datestr.slice(2..3) + else + puts "不正な日付形式-3: [#{datestr}]" + exit + end + begin + date = Date.parse(datestr) + rescue ArgumentError + puts "不正な日付形式-4: [#{datestr}]" + exit + end + while date < today + date = date >> 12 + end end + + # Parse Time + require 'time' begin - date = Date.parse(datestr) + case text + when /^([0-9]+):([0-9]+)-([0-9]+):([0-9]+)\s*/ + time_st = Time.mktime(date.year, date.month, date.day, $1.to_i, $2.to_i, 0, 0) + time_en = Time.mktime(date.year, date.month, date.day, $3.to_i, $4.to_i, 0, 0) + text = $' + when /^([0-9]+):([0-9]+)\s*/ + time_st = Time.mktime(date.year, date.month, date.day, $1.to_i, $2.to_i, 0, 0) + time_en = time_st + 3600 + text = $' + end rescue ArgumentError - puts "不正な日付形式-4: [#{datestr}]" + puts "時刻が範囲外?: #{text}" exit end - while date < today - date = date >> 12 - end - end - # Parse Time - require 'time' - begin - case text - when /^([0-9]+):([0-9]+)-([0-9]+):([0-9]+)\s*/ - time_st = Time.mktime(date.year, date.month, date.day, $1.to_i, $2.to_i, 0, 0) - time_en = Time.mktime(date.year, date.month, date.day, $3.to_i, $4.to_i, 0, 0) - text = $' - when /^([0-9]+):([0-9]+)\s*/ - time_st = Time.mktime(date.year, date.month, date.day, $1.to_i, $2.to_i, 0, 0) - time_en = time_st + 3600 - text = $' + # Parse Location + if text =~ /@/ + text = $` + location = $' end - rescue ArgumentError - puts "時刻が範囲外?: #{text}" - exit - end - - # Parse Location - if text =~ /@/ - text = $` - location = $' - end - puts "日付 : #{date}" - puts "開始時刻 : #{time_st}" - puts "開始時刻 : #{time_en}" - puts "タイトル : #{text}" - puts "場所 : #{location}" + puts "日付 : #{date}" + puts "開始時刻 : #{time_st}" + puts "開始時刻 : #{time_en}" + puts "タイトル : #{text}" + puts "場所 : #{location}" - # Register to calendar - require 'rubygems' - require 'gcalapi' + # Register to calendar + require 'rubygems' + require 'gcalapi' - cal = GoogleCalendar::Calendar.new(GoogleCalendar::Service.new(@user["username"], @user["password"]), @feed) + cal = GoogleCalendar::Calendar.new(GoogleCalendar::Service.new(@user["username"], @user["password"]), @feed) - if time_st && time_en - event = cal.create_event - event.title = text - event.where = location - event.st = time_st - event.en = time_en - event.save! - else - # All day - event = cal.create_event - event.title = text - event.where = location - event.st = Time.mktime(date.year, date.month, date.day) - event.en = event.st - event.allday = true - event.save! + if time_st && time_en + event = cal.create_event + event.title = text + event.where = location + event.st = time_st + event.en = time_en + event.save! + else + # All day + event = cal.create_event + event.title = text + event.where = location + event.st = Time.mktime(date.year, date.month, date.day) + event.en = event.st + event.allday = true + event.save! + end end end -end -class PublishGoogleCalendar - attr_accessor :hb + class PublishGoogleCalendar + attr_accessor :hb - def initialize(config, pipeline=[]) - @config = config - @pipeline = pipeline + def initialize(config, pipeline=[]) + @config = config + @pipeline = pipeline - @gc = Googlecalendar.new - @gc.user = { - "hatena_id" => @config['username'], - "password" => @config['password'] - } - end + @gc = Googlecalendar.new + @gc.user = { + "hatena_id" => @config['username'], + "password" => @config['password'] + } + end - def run - @pipeline.each {|feeds| - unless feeds.nil? - feeds.items.each {|feed| - @gc.add('今日 ' + feed.title) - sleep @config['interval'].to_i - } - end - } - @pipeline + def run + @pipeline.each {|feeds| + unless feeds.nil? + feeds.items.each {|feed| + @gc.add('今日 ' + feed.title) + sleep @config['interval'].to_i + } + end + } + @pipeline + end end end diff --git a/plugins/publish/hatena_bookmark.rb b/plugins/publish/hatena_bookmark.rb index 74173e2..4e30b3f 100644 --- a/plugins/publish/hatena_bookmark.rb +++ b/plugins/publish/hatena_bookmark.rb @@ -7,102 +7,104 @@ # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. -class HatenaBookmark - require 'rubygems' - require 'time' - require 'digest/sha1' - require 'net/http' - require 'uri' - #require 'nkf' +module Automatic::Plugin + class HatenaBookmark + require 'rubygems' + require 'time' + require 'digest/sha1' + require 'net/http' + require 'uri' + #require 'nkf' - attr_accessor :user + attr_accessor :user - def initialize - @user = { - "hatena_id" => "", - "password" => "" - } - end + def initialize + @user = { + "hatena_id" => "", + "password" => "" + } + end - def wsse(hatena_id, password) - # Unique value - nonce = [Time.now.to_i.to_s].pack('m').gsub(/\n/, '') - now = Time.now.utc.iso8601 - - # Base64 encoding for SHA1 Digested strings - digest = [Digest::SHA1.digest(nonce + now + password)].pack("m").gsub(/\n/, '') - - {'X-WSSE' => sprintf( - %Q, - hatena_id, digest, nonce, now) - } - end + def wsse(hatena_id, password) + # Unique value + nonce = [Time.now.to_i.to_s].pack('m').gsub(/\n/, '') + now = Time.now.utc.iso8601 + + # Base64 encoding for SHA1 Digested strings + digest = [Digest::SHA1.digest(nonce + now + password)].pack("m").gsub(/\n/, '') + + {'X-WSSE' => sprintf( + %Q, + hatena_id, digest, nonce, now) + } + end - def toXml(link, summary) - %Q( + def toXml(link, summary) + %Q( dummy #{summary} ) - end + end - def post(b_url, b_comment) - url = "http://b.hatena.ne.jp/atom/post" - header = wsse(@user["hatena_id"], @user["password"]) - uri = URI.parse(url) - proxy_class = Net::HTTP::Proxy(ENV["PROXY"], 8080) - http = proxy_class.new(uri.host) - http.start do |http| - # b_url = NKF.nkf('-w', b_url) - # b_comment = NKF.nkf('-w', b_comment) - res = http.post(uri.path, toXml(b_url, b_comment), header) - t = Time.now.strftime("%Y/%m/%d %X") - if res.code == "201" then - unless b_comment.nil? - print "#{t} [info] Success: #{b_url} Comment: #{b_comment}\n" + def post(b_url, b_comment) + url = "http://b.hatena.ne.jp/atom/post" + header = wsse(@user["hatena_id"], @user["password"]) + uri = URI.parse(url) + proxy_class = Net::HTTP::Proxy(ENV["PROXY"], 8080) + http = proxy_class.new(uri.host) + http.start do |http| + # b_url = NKF.nkf('-w', b_url) + # b_comment = NKF.nkf('-w', b_comment) + res = http.post(uri.path, toXml(b_url, b_comment), header) + t = Time.now.strftime("%Y/%m/%d %X") + if res.code == "201" then + unless b_comment.nil? + print "#{t} [info] Success: #{b_url} Comment: #{b_comment}\n" + else + print "#{t} [info] Success: #{b_url}\n" + end else - print "#{t} [info] Success: #{b_url}\n" + print "#{t} [error] #{res.code} Error: #{b_url}\n" end - else - print "#{t} [error] #{res.code} Error: #{b_url}\n" end end end -end -class PublishHatenaBookmark - attr_accessor :hb + class PublishHatenaBookmark + attr_accessor :hb - def initialize(config, pipeline=[]) - @config = config - @pipeline = pipeline + def initialize(config, pipeline=[]) + @config = config + @pipeline = pipeline - @hb = HatenaBookmark.new - @hb.user = { - "hatena_id" => @config['username'], - "password" => @config['password'] - } - end + @hb = HatenaBookmark.new + @hb.user = { + "hatena_id" => @config['username'], + "password" => @config['password'] + } + end - def run - @pipeline.each {|feeds| - unless feeds.nil? - feeds.items.each {|feed| - Log.puts("info", "Bookmarking: #{feed.link}") - hb.post(feed.link, nil) - sleep @config['interval'].to_i - } - end - } - @pipeline + def run + @pipeline.each {|feeds| + unless feeds.nil? + feeds.items.each {|feed| + Automatic::Log.puts("info", "Bookmarking: #{feed.link}") + hb.post(feed.link, nil) + sleep @config['interval'].to_i + } + end + } + @pipeline + end end end if __FILE__ == $0 b_url = ARGV.shift || abort("Usage: hatenabookmark.rb ") b_comment = ARGV.shift - hb = HatenaBookmark.new + hb = Automatic::Plugin::HatenaBookmark.new hb.post(b_url, b_comment) end diff --git a/plugins/store/bookmark.rb b/plugins/store/bookmark.rb index acbd4fe..813da57 100644 --- a/plugins/store/bookmark.rb +++ b/plugins/store/bookmark.rb @@ -6,51 +6,52 @@ # Updated:: Feb 22, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. - require 'active_record' -class Bookmark < ActiveRecord::Base -end +module Automatic::Plugin + class Bookmark < ActiveRecord::Base + end -class StoreBookmark - attr_accessor :hb + class StoreBookmark + attr_accessor :hb - def initialize(config, pipeline=[]) - @config = config - @pipeline = pipeline - end + def initialize(config, pipeline=[]) + @config = config + @pipeline = pipeline + end - def create_table - ActiveRecord::Migration.create_table :bookmarks do |t| - t.column :url, :string - t.column :created_at, :string + def create_table + ActiveRecord::Migration.create_table :bookmarks do |t| + t.column :url, :string + t.column :created_at, :string + end end - end - def run - ActiveRecord::Base.establish_connection( - :adapter => "sqlite3", - :database => (File.join(File.dirname(__FILE__), - '..', '..', 'db', @config['db'])) - ) - create_table unless Bookmark.table_exists?() + def run + ActiveRecord::Base.establish_connection( + :adapter => "sqlite3", + :database => (File.join(File.dirname(__FILE__), + '..', '..', 'db', @config['db'])) + ) + create_table unless Bookmark.table_exists?() - bookmarks = Bookmark.find(:all) - return_feeds = [] - @pipeline.each {|feeds| - unless feeds.nil? - new_feed = false - feeds.items.each {|feed| - unless bookmarks.detect {|b|b.url == feed.link} - new_bookmark = Bookmark.new(:url => feed.link, - :created_at => Time.now.strftime("%Y/%m/%d %X")) - new_bookmark.save - new_feed = true - end - } - return_feeds << feeds if new_feed - end - } - return_feeds + bookmarks = Bookmark.find(:all) + return_feeds = [] + @pipeline.each {|feeds| + unless feeds.nil? + new_feed = false + feeds.items.each {|feed| + unless bookmarks.detect {|b|b.url == feed.link} + new_bookmark = Bookmark.new(:url => feed.link, + :created_at => Time.now.strftime("%Y/%m/%d %X")) + new_bookmark.save + new_feed = true + end + } + return_feeds << feeds if new_feed + end + } + return_feeds + end end end diff --git a/plugins/subscription/feed.rb b/plugins/subscription/feed.rb index 1f06373..b5f6dca 100644 --- a/plugins/subscription/feed.rb +++ b/plugins/subscription/feed.rb @@ -1,28 +1,30 @@ #!/usr/bin/env ruby # -*- coding: utf-8 -*- -# Name:: Automatic::Plugin::Subscription::Feed +# Name:: Automatic::Plugin::SubscriptionFeed # Author:: 774 # Created:: Feb 22, 2012 # Updated:: Feb 22, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. -class SubscriptionFeed - def initialize(config, pipeline=[]) - @config = config - @pipeline = pipeline - end - - def run - @config['feeds'].each {|feed| - begin - Log.puts("info", "Parsing: #{feed}") - rss = FeedParser.get_rss(feed) - @pipeline << rss - rescue - Log.puts("error", "Fault in parsing: #{feed}") - end - } - @pipeline +module Automatic::Plugin + class SubscriptionFeed + def initialize(config, pipeline=[]) + @config = config + @pipeline = pipeline + end + + def run + @config['feeds'].each {|feed| + begin + Automatic::Log.puts("info", "Parsing: #{feed}") + rss = Automatic::FeedParser.get_rss(feed) + @pipeline << rss + rescue + Automatic::Log.puts("error", "Fault in parsing: #{feed}") + end + } + @pipeline + end end end From 409327b449fd6c7113a04ed335e4d444fe9b44c1 Mon Sep 17 00:00:00 2001 From: id774 Date: Fri, 24 Feb 2012 22:13:03 +0900 Subject: [PATCH 005/639] experimental-1 add new test recipes --- test/integration/test_activerecord.yml | 24 ++++++++++++++++++++++ test/integration/test_hatenabookmark.yml | 26 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 test/integration/test_activerecord.yml create mode 100644 test/integration/test_hatenabookmark.yml diff --git a/test/integration/test_activerecord.yml b/test/integration/test_activerecord.yml new file mode 100644 index 0000000..40e0672 --- /dev/null +++ b/test/integration/test_activerecord.yml @@ -0,0 +1,24 @@ +global: + timezone: Asia/Tokyo + cache: + base: /tmp + log: + level: info + +plugins: + - module: SubscriptionFeed + config: + feeds: + - http://id774.net/blog/feed/ + + - module: FilterIgnore + config: + exclude: + - hoge + + - module: StoreBookmark + config: + db: bookmark.db + + - module: PublishConsole + diff --git a/test/integration/test_hatenabookmark.yml b/test/integration/test_hatenabookmark.yml new file mode 100644 index 0000000..34ba574 --- /dev/null +++ b/test/integration/test_hatenabookmark.yml @@ -0,0 +1,26 @@ +global: + timezone: Asia/Tokyo + cache: + base: /tmp + log: + level: info + +plugins: + - module: SubscriptionFeed + config: + feeds: + - http://id774.net/blog/feed/ + + - module: FilterIgnore + config: + exclude: + - hoge + + - module: PublishHatenaBookmark + config: + username: username + password: password + interval: 1 + + - module: PublishConsole + From 44791f57f12f9c4386b07aa44275f876dbbbf163 Mon Sep 17 00:00:00 2001 From: id774 Date: Fri, 24 Feb 2012 22:55:03 +0900 Subject: [PATCH 006/639] 40 Bundler simple_xlsx_writer added. Resolve this problem https://github.com/id774/automaticruby/pull/19 --- Gemfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Gemfile b/Gemfile index 502ba9b..3ee5e7d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,12 @@ source :rubygems +gem 'builder' # Explicit require before simple_xlsx_writer +gem 'simple_xlsx_writer', :require => 'simple_xlsx' + gem 'sqlite3-ruby' gem 'activesupport' gem 'hashie' + gem 'activerecord' gem 'gcalapi' From c3118f2094bcf761446dc7fbb800166a01d43db7 Mon Sep 17 00:00:00 2001 From: id774 Date: Fri, 24 Feb 2012 23:05:17 +0900 Subject: [PATCH 007/639] 41 A few refactoring --- lib/automatic/pipeline.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/automatic/pipeline.rb b/lib/automatic/pipeline.rb index f3eb59f..053f214 100644 --- a/lib/automatic/pipeline.rb +++ b/lib/automatic/pipeline.rb @@ -12,7 +12,7 @@ module Automatic module Plugin end module Pipeline - def self.plugin_load(module_name) + def self.load_plugin(module_name) type, filename = module_name.underscore.split('_', 2) path = Automatic.plugins_dir + "#{type}/#{filename}.rb" Automatic::Plugin.autoload module_name.to_sym, path @@ -22,7 +22,7 @@ def self.run(recipe) raise "NoRecipeError" if recipe.nil? pipeline = [] recipe.each_plugin do |plugin| - plugin_load(plugin.module) + load_plugin(plugin.module) klass = Automatic::Plugin.const_get(plugin.module) pipeline = klass.new(plugin.config, pipeline).run end From 19c37ab5c162d3d688a5aee8c7a2cad90eb6538c Mon Sep 17 00:00:00 2001 From: id774 Date: Fri, 24 Feb 2012 23:12:57 +0900 Subject: [PATCH 008/639] 42 RDoc updated, fix a newline and space --- app.rb | 3 +-- lib/automatic.rb | 9 ++++----- lib/automatic/feed_parser.rb | 2 +- lib/automatic/log.rb | 2 +- lib/automatic/pipeline.rb | 5 +++-- lib/automatic/recipe.rb | 1 + plugins/filter/ignore.rb | 2 +- plugins/publish/console.rb | 2 +- plugins/publish/google_calendar.rb | 6 ------ plugins/publish/hatena_bookmark.rb | 9 +-------- plugins/subscription/feed.rb | 4 ++-- 11 files changed, 16 insertions(+), 29 deletions(-) diff --git a/app.rb b/app.rb index bdbee7b..c45fba9 100755 --- a/app.rb +++ b/app.rb @@ -4,7 +4,7 @@ # Author:: 774 # Version:: 12.02-devel # Created:: Feb 18, 2012 -# Updated:: Feb 22, 2012 +# Updated:: Feb 24, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. @@ -14,4 +14,3 @@ require 'lib/automatic' Automatic.run(root_dir) - diff --git a/lib/automatic.rb b/lib/automatic.rb index 3c6c609..1833ec5 100644 --- a/lib/automatic.rb +++ b/lib/automatic.rb @@ -6,6 +6,7 @@ # Updated:: Feb 24, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. + module Automatic require 'automatic/recipe' require 'automatic/pipeline' @@ -25,7 +26,7 @@ def self.plugins_dir def self.config_dir @root_dir + "/config/" end - + def self.run(root_dir) @root_dir = root_dir recipe_path = "" @@ -40,7 +41,7 @@ def self.run(root_dir) exit end end - + begin parser.parse! print "Loading #{recipe_path}\n" unless recipe_path == "" @@ -49,11 +50,9 @@ def self.run(root_dir) $stderr.puts parser.help exit 1 end - + # recipe treat as an object. recipe = Automatic::Recipe.new(recipe_path) Automatic::Pipeline.run(recipe) end end - - diff --git a/lib/automatic/feed_parser.rb b/lib/automatic/feed_parser.rb index 7892b97..57b4f5f 100644 --- a/lib/automatic/feed_parser.rb +++ b/lib/automatic/feed_parser.rb @@ -3,7 +3,7 @@ # Name:: Automatic::FeedParser # Author:: 774 # Created:: Feb 19, 2012 -# Updated:: Feb 22, 2012 +# Updated:: Feb 24, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. diff --git a/lib/automatic/log.rb b/lib/automatic/log.rb index 1f37b33..6274b00 100644 --- a/lib/automatic/log.rb +++ b/lib/automatic/log.rb @@ -3,7 +3,7 @@ # Name:: Automatic::Log # Author:: 774 # Created:: Feb 20, 2012 -# Updated:: Feb 22, 2012 +# Updated:: Feb 24, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. diff --git a/lib/automatic/pipeline.rb b/lib/automatic/pipeline.rb index 053f214..b7ae8e3 100644 --- a/lib/automatic/pipeline.rb +++ b/lib/automatic/pipeline.rb @@ -3,9 +3,10 @@ # Name:: Automatic::Core # Author:: 774 # Created:: Feb 22, 2012 -# Updated:: Feb 22, 2012 +# Updated:: Feb 24, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. + require 'active_support/core_ext' module Automatic @@ -17,7 +18,7 @@ def self.load_plugin(module_name) path = Automatic.plugins_dir + "#{type}/#{filename}.rb" Automatic::Plugin.autoload module_name.to_sym, path end - + def self.run(recipe) raise "NoRecipeError" if recipe.nil? pipeline = [] diff --git a/lib/automatic/recipe.rb b/lib/automatic/recipe.rb index baa7f19..5f00315 100644 --- a/lib/automatic/recipe.rb +++ b/lib/automatic/recipe.rb @@ -5,6 +5,7 @@ # Updated:: Feb 24, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. + require 'hashie' require 'yaml' diff --git a/plugins/filter/ignore.rb b/plugins/filter/ignore.rb index 0e9fa25..3cd69d3 100644 --- a/plugins/filter/ignore.rb +++ b/plugins/filter/ignore.rb @@ -3,7 +3,7 @@ # Name:: Automatic::Plugin::Filter::Ignore # Author:: 774 # Created:: Feb 22, 2012 -# Updated:: Feb 22, 2012 +# Updated:: Feb 24, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. diff --git a/plugins/publish/console.rb b/plugins/publish/console.rb index 06ba73c..e03abeb 100644 --- a/plugins/publish/console.rb +++ b/plugins/publish/console.rb @@ -3,7 +3,7 @@ # Name:: Automatic::Plugin::Publish::Console # Author:: 774 # Created:: Feb 23, 2012 -# Updated:: Feb 23, 2012 +# Updated:: Feb 24, 2012 # Copyright:: 774 Copyright (c) 2012 # License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. diff --git a/plugins/publish/google_calendar.rb b/plugins/publish/google_calendar.rb index a754feb..09d5745 100644 --- a/plugins/publish/google_calendar.rb +++ b/plugins/publish/google_calendar.rb @@ -176,9 +176,3 @@ def run end end end - -if __FILE__ == $0 - abort("Usage: google_calendar.rb