-
Notifications
You must be signed in to change notification settings - Fork 2
/
fedi_ebooks.rb
executable file
·71 lines (54 loc) · 1.8 KB
/
fedi_ebooks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require "rufus-scheduler"
require_relative "config"
require_relative "provider"
require_relative "mispy/model"
module FediEbooks
@model = nil
@instance = nil
@logger = FediEbooks::Logger.instance
@scheduler = Rufus::Scheduler.new
def self.init
FediEbooks::Config.from_file("config.yml")
@instance = FediEbooks::Provider.select_provider()
FediEbooks::Config.update_bot_username(@instance.get_username)
@logger.set_name("@#{FediEbooks::Config.bot_username}")
if FediEbooks::Config.bot_username.nil?
@logger.log("Unable to get the account's username! Check your credentials!")
exit(1)
end
model_path = "#{FediEbooks::Config.bot_username}.db"
@model = FediEbooks::Model.new(model_path)
if File.file?(model_path)
@logger.log("Database #{@model.path} loaded.")
else
@logger.log("Creating database #{@model.path}...")
@model.consume_all(FediEbooks::Config.corpus_files).save
end
@logger.log("Connected to #{FediEbooks::Config.instance_url} (#{@instance.name})")
end
init
# Prettier errors
def @scheduler.on_error(job, error)
logger = FediEbooks::Logger.instance
logger.log("Exception caught in scheduler thread #{error.inspect}!")
puts("\n------------ Backtrace Below ------------\n\n")
error.backtrace.each do |line|
puts(line.to_s)
end
puts("\n\n------------ End of Backtrace ------------\n\n")
end
# Post a random post every 1 hour
@scheduler.every "1h" do
status = @model.make_statement(FediEbooks::Config.reply_max_length)
@logger.log("Posting: #{status}")
@instance.create_status(status)
end
@scheduler.every "30s" do
@instance.reply(@model)
# Comment this out if you want timeline replies
# @instance.reply_timeline(@model)
end
loop do
sleep 1
end
end