-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rb
73 lines (67 loc) · 2.35 KB
/
main.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
72
73
require 'bundler/setup'
require 'yaml'
require 'mastodon'
require 'ikku'
require 'sanitize'
config = YAML.load_file("./key.yml")
debug = true
unfollow_str = "俳句検出を停止してください"
stream = Mastodon::Streaming::Client.new(
base_url: "https://" + config["base_url"],
bearer_token: config["access_token"])
rest = Mastodon::REST::Client.new(
base_url: "https://" + config["base_url"],
bearer_token: config["access_token"])
reviewer = Ikku::Reviewer.new
reviewer_id = rest.verify_credentials().id
begin
stream.user() do |toot|
if toot.kind_of?(Mastodon::Status) then
content = Sanitize.clean(toot.content)
unfollow_request = false
toot.mentions.each do |mention|
if mention.id == reviewer_id
if !content.index(unfollow_str).nil?
unfollow_request = true
relationships = rest.relationships([toot.account.id])
relationships.each do |relationship|
if relationship.following?
rest.unfollow(toot.account.id)
p "unfollow"
end
end
end
end
end
if !unfollow_request && (toot.visibility == "public" || toot.visibility == "unlisted") then
if toot.in_reply_to_id.nil? && toot.attributes["reblog"].nil? then
p "@#{toot.account.acct}: #{content}" if debug
haiku = reviewer.find(content)
if haiku then
postcontent = "『#{haiku.phrases[0].join("")} #{haiku.phrases[1].join("")} #{haiku.phrases[2].join("")}』"
p "俳句検知: #{postcontent}" if debug
if toot.attributes["spoiler_text"].empty? then
rest.create_status("@#{toot.account.acct} 俳句を発見しました!\n" + postcontent, in_reply_to_id: toot.id)
else
rest.create_status("@#{toot.account.acct}\n" + postcontent, in_reply_to_id: toot.id, spoiler_text: "俳句を発見しました!")
end
p "post!" if debug
elsif debug
p "俳句なし"
end
elsif debug
p "BT or reply"
end
elsif debug
p "private toot"
end
elsif toot.kind_of?(Mastodon::Notification) then
p "#{toot.type} by #{toot.account.id}" if debug
rest.follow(toot.account.id) if toot.type == "follow"
end
end
rescue => e
p "error"
puts e
retry
end