-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
68 lines (56 loc) · 1.52 KB
/
Copy pathmain.go
File metadata and controls
68 lines (56 loc) · 1.52 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
package main
import (
"io/ioutil"
"log"
"net/http"
"regexp"
"time"
"github.com/gorilla/mux"
"github.com/kelvins/sunrisesunset"
"github.com/xanderstrike/plexhooks"
"github.com/xanderstrike/plexlights/handler"
)
func hook(w http.ResponseWriter, r *http.Request) {
log.Println("Serving request...")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
re := regexp.MustCompile("({.*})")
match := re.FindStringSubmatch(string(body))
response, err := plexhooks.ParseWebhook([]byte(match[0]))
if err != nil {
panic(err)
}
h := handler.New()
h.HandleEvent(response.Account.Title, response.Player.Uuid, response.Event)
}
func index(w http.ResponseWriter, r *http.Request) {
loc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
panic(err)
}
now := time.Now()
p := sunrisesunset.Parameters{
Latitude: -33.960,
Longitude: -118.351,
UtcOffset: -8.0,
Date: now,
}
sunrise, sunset, err := p.GetSunriseSunset()
if err != nil {
panic(err)
}
// what the actual fuck is going on with timezones
eight, _ := time.ParseDuration("-8h")
log.Println("Sunrise: ", sunrise.In(loc).Format("Mon 3:04PM"), "Sunset: ", sunset.In(loc).Format("Mon 3:04PM"), "Now: ", now.Add(eight).Format("Mon 3:04PM"))
_, _ = w.Write([]byte("OK"))
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/hook", hook).Methods("POST")
router.HandleFunc("/", index)
log.Println("Now serving on 0.0.0.0:8080/hook")
log.Println("Version 0.0.2")
log.Fatal(http.ListenAndServe("0.0.0.0:8080", router))
}