-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (82 loc) · 2.07 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
package main
import (
"flag"
"log"
"net/http"
"net/url"
"os"
"github.com/PuerkitoBio/goquery"
"github.com/nlopes/slack"
)
type MercariItem struct {
name string
price string
image string
url string
}
type NotifyArgs struct {
token string
channel string
mercariItem MercariItem
}
// Scrape mercari item list
func Scrape(keyword string) []MercariItem {
targetURL := "https://www.mercari.com/jp/search/?keyword=" + url.QueryEscape(keyword)
res, err := http.Get(targetURL)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
var mercariItems []MercariItem
doc.Find(".items-box-content .items-box").Each(func(i int, s *goquery.Selection) {
itemName := s.Find(".items-box-name").Text()
itemPrice := s.Find(".items-box-price").Text()
itemImage, _ := s.Find("img").Attr("data-src")
itemURL, _ := s.Find("a").Attr("href")
mercariItems = append(mercariItems, MercariItem{
name: itemName,
price: itemPrice,
image: itemImage,
url: itemURL,
})
})
return mercariItems
}
// Notify to Slack channel
func Notify(notifyArgs NotifyArgs) {
api := slack.New(notifyArgs.token)
mercariItem := notifyArgs.mercariItem
attachment := slack.MsgOptionAttachments(slack.Attachment{
Title: mercariItem.price + ": " + mercariItem.name,
ImageURL: mercariItem.image,
TitleLink: mercariItem.url,
})
_, respTimestamp, err := api.PostMessage(notifyArgs.channel, attachment)
if err != nil {
log.Fatal(err)
} else {
log.Printf("Succeed: %s => %s", respTimestamp, mercariItem.name)
}
}
func main() {
keyword := flag.String("keyword", "", "Search item keyword")
flag.Parse()
items := Scrape(*keyword)
slackToken := os.Getenv("SLACK_TOKEN")
slackChannel := os.Getenv("SLACK_CHANNEL")
for i := 0; i < len(items); i++ {
notifyArgs := NotifyArgs{
token: slackToken,
channel: slackChannel,
mercariItem: items[i],
}
Notify(notifyArgs)
}
}