Compare commits

..

1 commit
master ... rss

Author SHA1 Message Date
Alexander Yakovlev 81321d07df RSS plugin WIP 2017-10-19 15:06:58 +07:00
6 changed files with 141 additions and 100 deletions

1
.gitignore vendored
View file

@ -1 +0,0 @@
config.yml

59
bot.rb
View file

@ -1,15 +1,8 @@
require 'cinch'
require 'cinch-dicebag'
require 'net/http'
require 'uri'
require 'json'
require 'formdata'
require 'nokogiri'
require 'digest'
require 'yaml'
require_relative 'plugins/link_info'
require_relative 'plugins/help'
require_relative 'plugins/questbook'
require_relative 'plugins/rss'
def translit (text)
table = {
@ -18,6 +11,16 @@ def translit (text)
"b"=>["Б","б"],
"c"=>["Ц", "ц"],
"ch"=>["Ч", "ч"],
"cha"=>["ча"],
"chu"=>["чу"],
"chi"=>["чи"],
"cho"=>["чо"],
"che"=>["че"],
"chk"=>["чк"],
"cht"=>["чт"],
"chn"=>["чн"],
"chs"=>["чс"],
"chl"=>["чл"],
"d"=>["д"],
"e"=>["е"],
"j" => ["й"],
@ -36,6 +39,8 @@ def translit (text)
"p"=>["п"],
"r"=>["р"],
"s"=>["з", "с"],
"sch"=>["ш"],
"schtsch"=>["щ"],
"t"=>["т"],
"u"=>["у"],
"ü"=>["ю"],
@ -43,16 +48,13 @@ def translit (text)
"w"=>["в"],
"x"=>["кс"],
"y"=>["ы"],
"sch"=>["ш"],
"schtsch"=>["щ"],
"z"=>["ж"],
"zh"=>["ж"],
"zsch"=>["ч"],
"tzsch"=>["чш"],
"'"=>["ь"],
"q"=>["я"]
}
map = table.sort_by {|k,_| k.to_str.length }.reverse
map = table.sort_by {|k,v| v.length <=> k.length}
map.each do |translit_key, translit_value|
text.gsub!(translit_key.capitalize, translit_value.first)
text.gsub!(translit_key, translit_value.last)
@ -64,20 +66,12 @@ bot = Cinch::Bot.new do
configure do |c|
c.server = "irc.forestnet.org"
c.port = 6667
c.channels = [
"#fireurq",
"#urq",
"#instead",
"#qsp",
"#ifrus",
"#questbook"
]
c.nick = 'РобоВера'
c.channels = ["#urq", "#ifrus"]
c.nick = 'Попугай'
c.plugins.plugins = [
Cinch::Plugins::Dicebag,
Cinch::Help,
Cinch::LinkInfo,
QuestBook
]
c.plugins.options[Cinch::LinkInfo] = {
:blacklist => [/\.xz$/i, /\.zip$/i, /\.rar$/i],
@ -86,31 +80,12 @@ bot = Cinch::Bot.new do
end
on :message do |m|
if m.user.nick == 'MAlischka' or m.user.nick == 'MA' or m.user.nick == 'figwama' then
if m.user.nick == 'MAlischka' or m.user.nick == 'MA' then
m.reply( translit(m.message) )
end
end
on :message, /^!translit (.+)/ do |m, query|
m.reply translit(query)
end
end
bot.loggers.level = :info
class QuestBookChat
def initialize(bot)
@bot = bot
end
def start
while true
sleep 5
@bot.handlers.dispatch(:questbook)
end
end
end
Thread.new {
QuestBookChat.new(bot).start
}
bot.start

View file

@ -1,2 +0,0 @@
discord_webhook: ""
discord_token: ""

View file

@ -1,55 +0,0 @@
class QuestBook
include Cinch::Plugin
listen_to :questbook
def initialize(m)
@config = YAML.load_file('config.yml')
super(m)
end
def listen(m)
params = FormData.new
params.append('msg', '')
params.append('from', "#{Time.now.to_i - 5}00")
params.append('away', 0)
req = params.post_request("/forum/chat/message.php")
http = Net::HTTP.new('quest-book.ru', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req.add_field 'Referer', 'https://quest-book.ru/forum/chat/'
req.add_field 'X-Requested-With', 'XMLHttpRequest'
response = http.request(req)
response = JSON.parse response.body
begin
if response['msgs'] then
for msg in response['msgs'] do
print = false
if msg[1] != '' then
print = true
end
color = Digest::MD5.hexdigest(msg[1]).to_i(16) % 14 + 2
who = "[\x03#{color}#{msg[1]}\x0f]"
message = msg[2]
a = Nokogiri::HTML.parse message
message = a.text
if message != @last and print then
Channel('#questbook').send("#{who}: #{message}")
@last = message
# discord webhook
http = Net::HTTP.new('discordapp.com', 443)
http.use_ssl = true
params = FormData.new
params.append('content', message)
params.append('username', msg[1])
req = params.post_request("/api/webhooks/#{@config['discord_webhook']}/#{@config['discord_token']}")
http.request(req)
end
end
end
rescue NoMethodError
puts "No method error:\n"
puts response['msgs'].inspect
end
end
end

35
plugins/rss.rb Normal file
View file

@ -0,0 +1,35 @@
require 'rss'
require 'yaml'
class RssEmitter
def initialize(bot)
@bot = bot
end
def start
cache = YAML.load_file('cache.yml')
while true
sleep 10
cache.each do |feed|
rss = RSS::Parser.parse(feed.url)
rss.items.each do |item|
if item.date > feed.date then
@send_link(rss, item)
end
end
end
File.write('cache.yml', cache.to_yaml)
end
end
def send_link(rss, item)
@bot.handlers.dispatch(:rss, rss.channel.title, item.link)
end
end
class Rss
include Cinch::Plugin
listen_to :rss
def listen(m, title, link)
print "#{title}: #{link}"
end
end

89
questbook.rb Normal file
View file

@ -0,0 +1,89 @@
require 'cinch'
require 'cinch-dicebag'
require 'net/http'
require 'uri'
require 'json'
require 'formdata'
require_relative 'plugins/link_info'
require_relative 'plugins/help'
class QuestBookChat
def initialize(bot)
@bot = bot
end
def start
while true
sleep 5
@bot.handlers.dispatch(:questbook)
end
end
end
class QuestBook
include Cinch::Plugin
listen_to :questbook
def listen(m)
params = FormData.new
params.append('msg', '')
params.append('from', "#{Time.now.to_i - 5}00")
params.append('away', 0)
req = params.post_request("/forum/chat/message.php")
http = Net::HTTP.new('quest-book.ru', 443)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
req.add_field 'Referer', 'https://quest-book.ru/forum/chat/'
req.add_field 'X-Requested-With', 'XMLHttpRequest'
response = http.request(req)
response = JSON.parse response.body
begin
if response['msgs'] then
for msg in response['msgs'] do
who = '[Система]'
print = false
if msg[1] != '' then
who = "[#{msg[1]}]"
print = true
end
msg = msg[2]
if msg[2] != @last and print then
Channel('#questbook').send("#{who}: #{msg}")
@last = msg[2]
end
end
end
rescue NoMethodError
puts response['msgs'].inspect
end
end
end
bot = Cinch::Bot.new do
configure do |c|
c.server = "irc.forestnet.org"
c.port = 6667
c.channels = ["#questbook"]
c.nick = 'Квестбот'
c.plugins.plugins = [
Cinch::Plugins::Dicebag,
Cinch::Help,
Cinch::LinkInfo,
QuestBook
]
c.plugins.options[Cinch::LinkInfo] = {
:blacklist => [/\.xz$/i, /\.zip$/i, /\.rar$/i],
:no_description => true,
}
end
on :message do |m|
end
end
bot.loggers.level = :info
Thread.new {
QuestBookChat.new(bot).start
}
bot.start