From abff24f33fc86b79249c5d5207ae51a54d384469 Mon Sep 17 00:00:00 2001 From: Robert Gerus Date: Mon, 10 Mar 2014 08:40:39 +0100 Subject: [PATCH] Possibly, it'll work now. --- plugins/scrape.rb | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/plugins/scrape.rb b/plugins/scrape.rb index 0f9e82a..f2e7da8 100644 --- a/plugins/scrape.rb +++ b/plugins/scrape.rb @@ -3,15 +3,49 @@ require 'digest/md5' include EventMachine::IRC::Commands +DataMapper.setup(:default, Config[:scrape][:db]) + +module Scrape + def download(uri) + ext = "." + word.sub(/.*[.]([^.]*)/, '\1') if word.match(/[^.]+[.][^.]+/) + content = open(word).read + md5 = Digest::MD5.hexdigest(content) + File.open(Config[:scrape][:basepath] + md5 + ext, 'w') do |file| + file.write(content) + end + + return md5 + ext + end + + class Link + include DataMapper::Resource + + property :id, Serial + property :url, String + property :original_url, String + property :created_at, DateTime + property :nick, String + end +end + +DataMapper.auto_upgrade! + Client.register_trigger("PRIVMSG") do |msg| msg[:params].each do |word| - if word =~ /4cdn[.]org/ then - ext = "." + word.sub(/.*[.]([^.]*)/, '\1') if word.match(/[^.]+[.][^.]+/) - content = open(word).read - md5 = Digest::MD5.hexdigest(content) - File.open(Config[:scrape][:basepath] + md5 + ext, 'w') do |file| - file.write(content) + if word =~ /^https?:\/\// then + original_url = word + if word =~ /4cdn[.]org/ then + url = download(word) + else + url = Digest::MD5.hexdigest(word) end + + Scrape::Link.create( + :url => url, + :original_url => original_url, + :created_at => Time.now, + :nick => msg[:prefix].split('!').first + ) end end end