-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (132 loc) · 3.32 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2020 Changkun Ou. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
tg "github.com/go-telegram-bot-api/telegram-bot-api"
)
var (
token string
chatid int64
bot *tg.BotAPI
stores = []string{}
)
func init() {
token = os.Getenv("TG_BOTTOKEN")
chid := os.Getenv("TG_CHATID")
id, err := strconv.Atoi(chid)
if err != nil {
panic("chat id is not valid")
}
chatid = int64(id)
if token == "" || chatid == 0 {
panic("bot token or chat id is empty")
}
file, err := os.Open("stores.conf")
if err != nil {
panic("cannot open stores.conf")
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
l := scanner.Text()
if len(l) > 0 && l[0] != '#' {
stores = append(stores, l)
}
}
log.Println(stores)
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
bot, err = tg.NewBotAPI(token)
if err != nil {
panic("failed to connect bot")
}
bot.Debug = true
log.Printf("authorized on account %s", bot.Self.UserName)
}
func main() {
tick := time.NewTicker(time.Minute) // check every minute seems fine for me
log.Println("start checking...")
for {
select {
case <-tick.C:
slot, ok := available()
if !ok {
log.Println("cannot find appointment")
continue
}
msg := tg.NewMessage(chatid, fmt.Sprintf(msgTmpl, slot.Format(time.RFC822Z)))
bot.Send(msg)
}
}
}
const (
apAPI = "https://retail-pz.cdn-apple.com/product-zone-prod/availability/%d-%d-%d/%02d/availability.json"
msgTmpl = `Appointment avaliable!
time: %v
addr: https://www.apple.com/de/retail/instore-shopping-session/?anchorStore=rosenstrasse
`
)
type errorCode string
const (
errNotAvailiable errorCode = "NO_TIMESLOT_AVAILABLE"
errNotNeeded = "APPOINTMENT_NOT_NEEDED"
)
type entry struct {
StoreNumber string `json:"storeNumber"`
AppointmentsAvailable bool `json:"appointmentsAvailable"`
FirstAvailableAppointment int64 `json:"firstAvailableAppointment"`
ErrorCode errorCode `json:"errorCode"`
}
func available() (time.Time, bool) {
now := time.Now().UTC()
url := fmt.Sprintf(apAPI, now.Year(), now.Month(), now.Day(), now.Hour())
log.Println("check:", url)
resp, err := http.Get(url)
if err != nil {
log.Println("failed to request the appointment api:", err)
return time.Time{}, false
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Println("bad response code:", resp.StatusCode)
return time.Time{}, false
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("failed to read response body:", err)
return time.Time{}, false
}
var entries []*entry
err = json.Unmarshal(b, &entries)
if err != nil {
log.Println("failed to parse appointment entries:", err)
return time.Time{}, false
}
for _, e := range entries {
for _, i := range stores {
if strings.Compare(e.StoreNumber, i) != 0 {
continue
}
log.Println(e.StoreNumber, e.AppointmentsAvailable, e.FirstAvailableAppointment, e.ErrorCode)
if e.ErrorCode == errNotNeeded {
return time.Now(), true
}
if e.AppointmentsAvailable {
return time.Unix(e.FirstAvailableAppointment, 0), true
}
}
}
return time.Time{}, false
}