repost/plugins/scrape.rb

53 lines
1.3 KiB
Ruby
Raw Normal View History

2014-03-09 18:40:32 +00:00
require 'open-uri'
require 'digest/md5'
2014-03-10 08:01:37 +00:00
require 'data_mapper'
2014-03-09 18:40:32 +00:00
2014-03-09 18:00:52 +00:00
include EventMachine::IRC::Commands
2014-03-10 07:40:39 +00:00
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!
2014-03-09 18:59:42 +00:00
Client.register_trigger("PRIVMSG") do |msg|
msg[:params].each do |word|
2014-03-10 07:40:39 +00:00
if word =~ /^https?:\/\// then
original_url = word
if word =~ /4cdn[.]org/ then
2014-03-10 08:06:17 +00:00
url = Scrape::download(word)
2014-03-10 07:40:39 +00:00
else
url = Digest::MD5.hexdigest(word)
2014-03-09 18:59:42 +00:00
end
2014-03-10 07:40:39 +00:00
Scrape::Link.create(
:url => url,
:original_url => original_url,
:created_at => Time.now,
:nick => msg[:prefix].split('!').first
)
2014-03-09 18:40:32 +00:00
end
2014-03-09 18:59:42 +00:00
end
end