-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (62 loc) · 1.6 KB
/
Copy pathmain.go
File metadata and controls
75 lines (62 loc) · 1.6 KB
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
package main
import (
"context"
"log"
"net/http"
"strings"
"os"
"github.com/PuerkitoBio/goquery"
"github.com/shomali11/slacker"
"github.com/joho/godotenv"
)
func main() {
botKey := getVarFromENV("SLACK_BOT_TOKEN")
bot := slacker.NewClient(botKey)
definition := &slacker.CommandDefinition{
Description: "Get job postings from greenhouse boards",
Example: "tarantula crawl github",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
board := request.Param("word")
postings, err := getJobPostings("https://boards.greenhouse.io/" + board)
if err != nil {
log.Fatal(err)
}
response.Reply(postings, slacker.WithThreadReply(true))
},
}
bot.Command("tarantula crawl <word>", definition)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
}
func getVarFromENV(key string) string {
// load .env file
err := godotenv.Load(".env")
if err != nil {
log.Fatalf("Error loading .env file")
}
return os.Getenv(key)
}
// getJobPostings gets the latest jobs given and returns them as a list
func getJobPostings(url string) (string, error) {
// Get the HTML
resp, err := http.Get(url)
if err != nil {
return "", err
}
// Convert HTML into goquery document
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return "", err
}
// Save each .opening as a list
openings := ""
doc.Find(".opening").Each(func(i int, s *goquery.Selection) {
temp := strings.Trim(s.Text(), "\n \t")
openings += "- " + temp
})
return openings, nil
}