From ce3c9efa5df696af70cb7b6f2eea454c22c89fa1 Mon Sep 17 00:00:00 2001
From: Alexandre Morignot <erdnaxeli@gmail.com>
Date: Sat, 5 Jan 2013 16:10:17 +0100
Subject: [PATCH] Database support, new config file location

Links are now registered in a database !
Its configurtion muste be
defined in the database section in the config file. The migration for
the database can be done simply using "rake".

The config file has moved !
He must be located at "~/.playbot/config".

Site Plugins return value has changed !
The hash must contain a new key "url". It allow the plugin to normalize
it if many different url can link to a same content.
---
 Gemfile                          |  4 ++++
 Gemfile.lock                     |  8 ++++++++
 Rakefile                         | 17 +++++++++++++++++
 lib/migrate/001_create_musics.rb | 20 ++++++++++++++++++++
 lib/music.rb                     |  6 ++++++
 lib/options.rb                   |  2 +-
 lib/playbot.rb                   | 11 +++++++++--
 plugins/soundcloud_plugin.rb     |  3 ++-
 plugins/youtube_plugin.rb        |  3 ++-
 run.rb                           |  4 +++-
 10 files changed, 72 insertions(+), 6 deletions(-)
 create mode 100644 Rakefile
 create mode 100644 lib/migrate/001_create_musics.rb
 create mode 100644 lib/music.rb

diff --git a/Gemfile b/Gemfile
index 0b7abcf..d75d969 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,5 +1,9 @@
 source "http://rubygems.org"
+gem "rake"
 gem "net-yail"
 gem "rspec"
+gem "activerecord"
+gem "sqlite3"
+
 gem "youtube_it"
 gem "soundcloud"
diff --git a/Gemfile.lock b/Gemfile.lock
index c531c05..f018815 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,6 +1,9 @@
 GEM
   remote: http://rubygems.org/
   specs:
+    activerecord (2.3.14)
+      activesupport (= 2.3.14)
+    activesupport (2.3.14)
     builder (3.1.4)
     diff-lcs (1.1.3)
     faraday (0.8.4)
@@ -28,6 +31,7 @@ GEM
       multi_json (~> 1.0)
       rack (~> 1.2)
     rack (1.4.1)
+    rake (0.9.2.2)
     rspec (2.12.0)
       rspec-core (~> 2.12.0)
       rspec-expectations (~> 2.12.0)
@@ -41,6 +45,7 @@ GEM
       hashie
       httmultiparty (>= 0.3)
       httparty (>= 0.7.3)
+    sqlite3 (1.3.6)
     youtube_it (2.1.8)
       builder
       faraday (~> 0.8)
@@ -53,7 +58,10 @@ PLATFORMS
   ruby
 
 DEPENDENCIES
+  activerecord
   net-yail
+  rake
   rspec
   soundcloud
+  sqlite3
   youtube_it
diff --git a/Rakefile b/Rakefile
new file mode 100644
index 0000000..18969ab
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,17 @@
+require 'active_record'
+require 'logger'
+
+require_relative 'lib/options.rb'
+
+task :default => :migrate
+
+desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
+task :migrate => :environment do
+    ActiveRecord::Migrator.migrate('lib/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
+end
+
+task :environment do
+    config = Options.new.read_file
+    ActiveRecord::Base.establish_connection(config[:database])
+    ActiveRecord::Base.logger = Logger.new(File.open('log/database.log', 'a'))
+end
diff --git a/lib/migrate/001_create_musics.rb b/lib/migrate/001_create_musics.rb
new file mode 100644
index 0000000..10aa16e
--- /dev/null
+++ b/lib/migrate/001_create_musics.rb
@@ -0,0 +1,20 @@
+require 'rubygems'
+require 'bundler/setup'
+
+class CreateMusics < ActiveRecord::Migration
+    def self.up
+        create_table :musics do |t|
+            t.string :title, :null => false
+            t.string :author, :null => false
+            t.string :sender, :null => false
+            t.string :url, :null => false
+            t.string :file
+        end
+
+        add_index :musics, :url, :unique
+    end
+
+    def self.down
+        drop_table :musics
+    end
+end
diff --git a/lib/music.rb b/lib/music.rb
new file mode 100644
index 0000000..bf9f6d8
--- /dev/null
+++ b/lib/music.rb
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'active_record'
+
+class Music < ActiveRecord::Base
+end
diff --git a/lib/options.rb b/lib/options.rb
index 2185f85..b942763 100644
--- a/lib/options.rb
+++ b/lib/options.rb
@@ -3,7 +3,7 @@ require 'yaml'
 
 # Allow us to get options, set by user or default ones.
 class Options
-    def initialize(file = "#{ENV['HOME']}/.playbot")
+    def initialize(file = "#{ENV['HOME']}/.playbot/config")
         @options = {}
         @file = file
     end
diff --git a/lib/playbot.rb b/lib/playbot.rb
index d49ea3a..96343be 100644
--- a/lib/playbot.rb
+++ b/lib/playbot.rb
@@ -5,6 +5,7 @@ require 'bundler/setup'
 require 'net/yail/irc_bot'
 
 require_relative 'site_plugin.rb'
+require_relative 'music.rb'
 
 
 # --
@@ -79,7 +80,13 @@ class PlayBot < IRCBot
         return if handler.nil?
 
         content = handler.new(@options).get(url)
-
-        msg(event.channel, "#{content[:title]} | #{content[:author]}")
+        music = Music.create(
+            :title  => content[:title],
+            :author => content[:author],
+            :sender => event.nick,
+            :url    => content[:url],
+            :file   => nil)
+        
+        msg(event.channel, "#{music.title} | #{music.author}")
     end
 end
diff --git a/plugins/soundcloud_plugin.rb b/plugins/soundcloud_plugin.rb
index aa2e7e1..ee82eb5 100644
--- a/plugins/soundcloud_plugin.rb
+++ b/plugins/soundcloud_plugin.rb
@@ -19,7 +19,8 @@ class SoundcloudPlugin < SitePlugin
 
     def get(url)
         track = @client.get('/resolve', :url => url)
+        url.gsub(/http:\/\//, 'https://')
 
-        {:title => track.title, :author => track.user.username}
+        {:title => track.title, :author => track.user.username, :url => url}
     end
 end
diff --git a/plugins/youtube_plugin.rb b/plugins/youtube_plugin.rb
index 1bbfa3c..bf0bdf0 100644
--- a/plugins/youtube_plugin.rb
+++ b/plugins/youtube_plugin.rb
@@ -17,6 +17,7 @@ class YoutubePlugin < SitePlugin
 
     def get(url)
         video = @client.video_by(url)
-        {:title => video.title, :author => video.author.name}
+        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
 end
diff --git a/run.rb b/run.rb
index 0a0b221..c98fbab 100755
--- a/run.rb
+++ b/run.rb
@@ -1,5 +1,6 @@
 #!/usr/bin/ruby
 
+require 'active_record'
 require 'logger'
 
 require_relative 'lib/playbot'
@@ -8,7 +9,8 @@ require_relative 'lib/options'
 # This code start the PlayBot with somes options.
 
 options = Options.new.read_all
-puts options
+
+ActiveRecord::Base.establish_connection(options[:database])
 
 bot = PlayBot.new(options)
 bot.irc_loop
-- 
GitLab