From f962cbd91bc0def199b1edac8a675cd602188911 Mon Sep 17 00:00:00 2001 From: Alexandre Morignot <erdnaxeli@gmail.com> Date: Mon, 1 Apr 2013 20:09:31 +0200 Subject: [PATCH] =?UTF-8?q?D=C3=A9but=20de=20la=20migration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 +- Gemfile.lock | 63 ++++++++++++++++++------------------ lib/options.rb | 13 +++++--- plugins/soundcloud_plugin.rb | 19 +++++------ plugins/youtube_plugin.rb | 22 ++++++------- run.rb | 49 +++++++++++++++++++++++++--- 6 files changed, 107 insertions(+), 61 deletions(-) diff --git a/Gemfile b/Gemfile index 4fba743..d1801ea 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source "http://rubygems.org" gem "rake" -gem "net-yail" +gem "cinch" gem "rspec" gem "activerecord" gem "activesupport" diff --git a/Gemfile.lock b/Gemfile.lock index 6fe9f93..efd7111 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,59 +2,60 @@ GEM remote: http://rubygems.org/ specs: activerecord (1.6.0) - activesupport (3.2.11) - i18n (~> 0.6) + activesupport (3.2.13) + i18n (= 0.6.1) multi_json (~> 1.0) - builder (3.1.4) - diff-lcs (1.1.3) - faraday (0.8.4) + builder (3.2.0) + cinch (2.0.4) + diff-lcs (1.2.1) + faraday (0.8.7) multipart-post (~> 1.1) - hashie (1.2.0) + hashie (2.0.3) httmultiparty (0.3.8) httparty (>= 0.7.3) multipart-post - httparty (0.9.0) + httparty (0.10.2) multi_json (~> 1.0) - multi_xml + multi_xml (>= 0.5.2) httpauth (0.2.0) i18n (0.6.1) - jwt (0.1.5) - multi_json (>= 1.0) - multi_json (1.5.0) - multi_xml (0.5.1) - multipart-post (1.1.5) - net-yail (1.6.0) - nokogiri (1.5.6) + jwt (0.1.8) + multi_json (>= 1.5) + multi_json (1.7.2) + multi_xml (0.5.3) + multipart-post (1.2.0) + nokogiri (1.5.9) oauth (0.4.7) - oauth2 (0.8.0) + oauth2 (0.9.1) faraday (~> 0.8) httpauth (~> 0.1) jwt (~> 0.1.4) multi_json (~> 1.0) + multi_xml (~> 0.5) rack (~> 1.2) - rack (1.4.3) - rake (10.0.3) - rspec (2.12.0) - rspec-core (~> 2.12.0) - rspec-expectations (~> 2.12.0) - rspec-mocks (~> 2.12.0) - rspec-core (2.12.2) - rspec-expectations (2.12.1) - diff-lcs (~> 1.1.3) - rspec-mocks (2.12.1) - simple_oauth (0.1.9) + rack (1.5.2) + rake (10.0.4) + rspec (2.13.0) + rspec-core (~> 2.13.0) + rspec-expectations (~> 2.13.0) + rspec-mocks (~> 2.13.0) + rspec-core (2.13.1) + rspec-expectations (2.13.0) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.13.0) + simple_oauth (0.2.0) soundcloud (0.3.1) hashie httmultiparty (>= 0.3) httparty (>= 0.7.3) - sqlite3 (1.3.6) - youtube_it (2.1.8) + sqlite3 (1.3.7) + youtube_it (2.1.13) builder faraday (~> 0.8) nokogiri (~> 1.5.2) oauth (~> 0.4.4) oauth2 (~> 0.6) - simple_oauth (~> 0.1.5) + simple_oauth (>= 0.1.5) PLATFORMS ruby @@ -62,7 +63,7 @@ PLATFORMS DEPENDENCIES activerecord activesupport - net-yail + cinch rake rspec soundcloud diff --git a/lib/options.rb b/lib/options.rb index 69367af..5ea90a6 100644 --- a/lib/options.rb +++ b/lib/options.rb @@ -20,8 +20,8 @@ class Options exit end - opts.on('-s', '--silent', 'set log to FATAL') do - @options[:silent] = true + opts.on('-v', '--verbose', 'enable verbose mode') do + @options[:verbose] = true end opts.on('-a', '--admin', 'admin nick') do @@ -29,7 +29,7 @@ class Options end opts.on('-n', '--network', 'server address') do - @options[:address] = arg + @options[:server] = arg end opts.on('-p', '--port', 'server port') do @@ -49,8 +49,11 @@ class Options end end - @options[:silent] ||= false - @options[:nicknames] ||= ['PlayBot', 'Play_Bot', 'Play__Bot', 'Play___Bot'] + @options[:verbose] ||= false + @options[:nick] ||= "PlayBot" + @options[:nicks] ||= ['Play_Bot', 'Play__Bot', 'Play___Bot'] + @options[:realname] || = "PlayBot" + @options[:user] ||= "PlayBot" @options[:channels] ||= ['#hormone'] @options end diff --git a/plugins/soundcloud_plugin.rb b/plugins/soundcloud_plugin.rb index ee82eb5..32d618c 100644 --- a/plugins/soundcloud_plugin.rb +++ b/plugins/soundcloud_plugin.rb @@ -7,20 +7,21 @@ require 'soundcloud' # SitePlugin for Soundcloud # # Need an client ID (soundcloud_client_id). -class SoundcloudPlugin < SitePlugin - def self.can_handle?(site) - site =~ /^https?:\/\/(www\.)?soundcloud\.com\/[a-zA-Z0-9\/_-]+$/ - end +class SoundCloud + include Cinch::Plugin - public - def initialize(options) - @client = Soundcloud.new(:client_id => options[:soundcloud_client_id]) - end + match /^https?:\/\/(www\.)?soundcloud\.com\/[a-zA-Z0-9\/_-]+$/ def get(url) - track = @client.get('/resolve', :url => url) + client = Soundcloud.new(:client_id => config[:client_id]) + track = client.get('/resolve', :url => url) url.gsub(/http:\/\//, 'https://') {:title => track.title, :author => track.user.username, :url => url} end + + def execute(m, url) + infos = get(url) + m.reply("#{infos.title} | #{infos.author}") + end end diff --git a/plugins/youtube_plugin.rb b/plugins/youtube_plugin.rb index bf0bdf0..4ff48b4 100644 --- a/plugins/youtube_plugin.rb +++ b/plugins/youtube_plugin.rb @@ -1,23 +1,23 @@ -require_relative '../lib/site_plugin.rb' - require 'rubygems' require 'bundler/setup' +require 'cinch' require 'youtube_it' # SitePlugin for YouTube -class YoutubePlugin < SitePlugin - def self.can_handle?(site) - site =~ /^https?:\/\/((www.)?youtube.(fr|com)\/watch\?v=|youtu\.be\/)[a-zA-Z0-9-]+$/ - end +class Youtube + include Cinch::Plugin - public - def initialize(options) - @client = YouTubeIt::Client.new - end + match /^https?:\/\/((www.)?youtube.(fr|com)\/watch\?v=|youtu\.be\/)[a-zA-Z0-9-]+$/ def get(url) - video = @client.video_by(url) + client = YouTubeIt::Client.new + video = client.video_by(url) url.gsub(/https?:\/\/(www.)?youtube.(fr|com)\/watch\?v=|youtu\.be/, 'https://www.youtube.com') {:title => video.title, :author => video.author.name, :url => url} end + + def execute(m, url) + infos = get(url) + m.reply("#{infos.title} | #{infos.author}") + end end diff --git a/run.rb b/run.rb index 109a762..740bc7a 100755 --- a/run.rb +++ b/run.rb @@ -1,17 +1,58 @@ #!/usr/bin/ruby +require 'rubygems' +require 'bundler/setup' +require 'cinch' require 'active_support/core_ext/hash/except.rb' require 'logger' -require_relative 'lib/playbot' require_relative 'lib/options' -# This code start the PlayBot with somes options. +# -- +# Changing working directory so the inclusion of plugin can be done correctly. +# I don't complety know why, but this is necessary. +Dir.chdir(File.expand_path File.dirname(__FILE__)) + +# -- +# Add plugins folder to LOAD_PATH and subsequently require all plugins. +Dir[File.join('plugins', '*.rb')].each { |file| require_relative file } + + +# Monkey patch for String class +class String + def to_class + chain = self.split "::" + klass = Kernel + chain.each do |klass_string| + klass = klass.const_get klass_string + end + klass.is_a?(Class) ? klass : nil + rescue NameError + nil + end +end + +# This code start the PlayBot with somes options. options = Options.new.read_all ActiveRecord::Base.establish_connection(options[:database]) ActiveRecord::Base.logger = Logger.new(File.open(options[:database][:log], 'a')) -bot = PlayBot.new(options) -bot.irc_loop + +bot = Cinch::Bot.new do + configure do |c| + c = options + + # we add the plugins + c.plugins.plugins = [] + options[:plugins].each do |plugin| + c.plugins.plugins << plugin.to_class + c.plugins.options[plugin.to_class] = options[plugin.to_s]. + end + + c.plugins.prefix = nil + end +end + +bot.start -- GitLab