/
commands.go
120 lines (105 loc) · 2.3 KB
/
commands.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"log"
"net/url"
"os"
"regexp"
"time"
"github.com/ChimeraCoder/anaconda"
"github.com/codegangsta/cli"
"github.com/darkhelmet/twitterstream"
"github.com/ikawaha/kagome/dic"
"github.com/ikawaha/kagome/tokenizer"
"github.com/joho/godotenv"
)
var YURIKO_ID int64 = 2725436402
var Commands = []cli.Command{
commandLoves,
}
var commandLoves = cli.Command{
Name: "loves",
Usage: "",
Description: `
`,
Action: loves,
}
func loves(c *cli.Context) {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
client := twitterstream.NewClient(
os.Getenv("TW_CONSUMER_KEY"),
os.Getenv("TW_CONSUMER_SECRET"),
os.Getenv("TW_CLIENT_KEY"),
os.Getenv("TW_CLIENT_SECRET"),
)
for {
conn, err := client.Follow(string(YURIKO_ID))
if err != nil {
time.Sleep(1 * time.Minute)
continue
}
kanojoru(conn)
}
}
func kanojoru(conn *twitterstream.Connection) {
for {
tweet, err := conn.Next()
if err != nil {
return
}
if !canKanojonize(tweet) {
continue
}
text := tweet.Text
kanojonized := kanojonize(text)
post(kanojonized)
}
}
func kanojonize(text string) string {
t := tokenizer.NewTokenizer()
morphs, err := t.Tokenize(text)
if err != nil {
return ""
}
var strings []string
for _, val := range morphs {
var curr dic.Content
curr, err = val.Content()
if err != nil {
continue
} else if (curr.Pos == "名詞") && (curr.Pos1 == "一般") {
// 置き換える単語を保持
strings = append(strings[:], val.Surface)
}
}
kanojonized := replaceToHisaichi(strings, text)
return kanojonized
}
func replaceToHisaichi(strings []string, text string) string {
for _, val := range strings {
text = regexp.MustCompile(val).ReplaceAllString(text, "ひさいち")
}
return text
}
func canKanojonize(tweet *twitterstream.Tweet) bool {
if len(tweet.Entities.Mentions) >= 1 {
// メンションがあるときは無視
return false
} else if tweet.User.Id != YURIKO_ID {
// 吉高由里子以外無視
return false
}
return true
}
func post(kanojonized string) {
anaconda.SetConsumerKey(os.Getenv("TW_CONSUMER_KEY"))
anaconda.SetConsumerSecret(os.Getenv("TW_CONSUMER_SECRET"))
api := anaconda.NewTwitterApi(
os.Getenv("TW_CLIENT_KEY"),
os.Getenv("TW_CLIENT_SECRET"),
)
v := url.Values{}
api.PostTweet(kanojonized, v)
}