-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
128 lines (113 loc) · 3.31 KB
/
main.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
121
122
123
124
125
126
127
128
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"regexp"
"github.com/atotto/clipboard"
)
type items struct {
Items []*item `json:"items"`
}
type outlineItems struct {
OutlineItems []*outlineItem `json:"items"`
}
type item struct {
Title string `json:"title"`
Snippet string `json:"snippet"`
}
type outlineItem struct {
Title string `json:"title"`
FormattedURL string `json:"formattedUrl"`
}
func main() {
apiKey := os.Getenv("GOOGLE_APIKEY")
cseID := os.Getenv("GOOGLE_CSE_ID")
var companyName string
if len(os.Args) < 2 {
fmt.Println("引数が省略されたため、クリップボードの内容を用いて判定します。")
text, err := clipboard.ReadAll()
if err != nil {
log.Fatalf("Failed to read from clipboard : %v\n", err)
} else {
companyName = text
}
} else {
companyName = os.Args[1]
}
maekabuPattern := "株式会社" + companyName
mae := 0
ushirokabuPattern := companyName + "株式会社"
ushiro := 0
response, err := http.Get("https://www.googleapis.com/customsearch/v1?key=" + apiKey + "&cx=" + cseID + "&q=" + companyName + "+株式会社")
if err != nil {
log.Fatalf("Failed to search : %v\n", err)
}
var results items
defer response.Body.Close()
if err := json.NewDecoder(response.Body).Decode(&results); err != nil {
log.Fatalf("Failed to decode search result : %v\n", err)
}
for _, result := range results.Items {
check(maekabuPattern, result.Title, &mae)
check(maekabuPattern, result.Snippet, &mae)
check(ushirokabuPattern, result.Title, &ushiro)
check(ushirokabuPattern, result.Snippet, &ushiro)
}
fmt.Printf("前株マッチ数:%d\n", mae)
fmt.Printf("後株マッチ数:%d\n", ushiro)
if mae > ushiro {
fmt.Println("前株です!")
fmt.Println(maekabuPattern)
if err := clipboard.WriteAll(maekabuPattern); err != nil {
log.Fatalf("Failed to copy to clipboard : %v\n", err)
}
} else if ushiro > mae {
fmt.Println("後株です!")
fmt.Println(ushirokabuPattern)
if err := clipboard.WriteAll(ushirokabuPattern); err != nil {
log.Fatalf("Failed to copy to clipboard : %v\n", err)
}
} else {
fmt.Println("わかりません!")
}
// 会社概要URLの取得
outlineResponse, err := http.Get("https://www.googleapis.com/customsearch/v1?key=" + apiKey + "&cx=" + cseID + "&q=" + companyName + "+株式会社+概要")
if err != nil {
log.Fatalf("Failed to search : %v\n", err)
}
var outlineResults outlineItems
defer outlineResponse.Body.Close()
if err := json.NewDecoder(outlineResponse.Body).Decode(&outlineResults); err != nil {
log.Fatalf("Failed to decode search result : %v\n", err)
}
var outlineURL string
for _, outlineItem := range outlineResults.OutlineItems {
matched, err := regexp.MatchString("概要", outlineItem.Title)
if err != nil {
log.Fatalf("Failed to regexp.MatchString : %v\n", err)
break
}
if matched {
outlineURL = outlineItem.FormattedURL
break
}
}
if outlineURL != "" {
fmt.Println(fmt.Sprintf("会社概要URL: %s", outlineURL))
if err := clipboard.WriteAll(outlineURL); err != nil {
log.Fatalf("Failed to copy to clipboard : %v\n", err)
}
}
}
func check(pattern string, str string, cnt *int) {
matched, err := regexp.MatchString(pattern, str)
if err != nil {
log.Fatalf("Failed to regexp.MatchString : %v\n", err)
}
if matched {
*cnt++
}
}