This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dilbert.go
87 lines (73 loc) · 2.39 KB
/
dilbert.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
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func getDilbertPostedDir() string {
dilbertDir := fmt.Sprintf("%s/dilbertPosted", getHomeEtc())
err := createDirIfMissing(dilbertDir, 0750)
if err != nil {
log.Fatal(fmt.Sprintf("Error creating [%s]: %s", dilbertDir, err))
}
return dilbertDir
}
func getTodaysDilbertFile() string {
timeNow := time.Now()
year, month, day := timeNow.Date()
curDate := fmt.Sprintf("%d-%s-%d", year, month, day)
return fmt.Sprintf("%s/%s", getDilbertPostedDir(), curDate)
}
func signifyDilbertPostedToday() {
dilbertSignifyFile := getTodaysDilbertFile()
err := createFile(dilbertSignifyFile)
if err != nil {
log.Fatalf(fmt.Sprintf("##Error creating [%s]: %s", dilbertSignifyFile, err))
}
}
func dilbertRoutine(wsClient websocketData) {
// if we arent connected atm give up now
if !wsClient.ws.IsClientConn() {
logDebug("dilbertRoutine() wsClient.ws.IsClientConn says we arent connected! Bailing..")
return
}
timeNow := time.Now()
// bail now if we've already posted today
todaysDilbert := getTodaysDilbertFile()
if pathExists(todaysDilbert) {
return
}
// only start looking for new comics at 7am
if timeNow.Hour() >= 7 {
// back off now if instructed
if !store.DilbertBackOffUntil.IsZero() {
if !timeNow.After(store.DilbertBackOffUntil) {
return
}
}
// define assumed url for todays comic
year, month, day := time.Now().Date()
dateString := fmt.Sprintf("%d-%s-%d", year, month, day)
// define assumed url for todays comic
comic := fmt.Sprintf("http://dilbert.com/strip/%s", dateString)
// verify comic exists or bail
httpClient := &http.Client{Timeout: time.Duration(time.Duration(3) * time.Second)}
resp, err := httpClient.Get(comic)
defer resp.Body.Close()
if resp.StatusCode != 200 || err != nil {
// dilbert isnt yet present (or some error); oh noes!
// Tell future dilbert routines to not try another HTTP request
// for another 10 minutes
logDebug(fmt.Sprintf("Error response from dilbert.com: code [%s] err [%s]", resp.StatusCode, err))
store.DilbertBackOffUntil = timeNow.Add(time.Duration(600) * time.Second)
return
}
wsClient.createSlackPost(fmt.Sprintf("%s\n", comic), config.SlackDilbertChannel)
logDebug(fmt.Sprintf("Dilbert posted: %s", comic))
// update records that we posted today
signifyDilbertPostedToday()
// reset back to zero time
store.DilbertBackOffUntil = time.Time{}
}
}