-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (103 loc) · 2.63 KB
/
main.go
File metadata and controls
125 lines (103 loc) · 2.63 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
)
type LockerCodes struct {
Result struct {
Data struct {
AllLockerCodes struct {
Edges []struct {
Node struct {
ID string `json:"id"`
Title string `json:"title"`
Version int `json:"version"`
Slug string `json:"slug"`
TweetID string `json:"tweetId"`
DateAdded string `json:"dateAdded"`
LockerCode string `json:"lockerCode"`
Expiration string `json:"expiration"`
LimitedTime interface{} `json:"limitedTime"`
Reward string `json:"reward"`
} `json:"node"`
} `json:"edges"`
} `json:"allLockerCodes"`
} `json:"data"`
} `json:"result"`
}
func main() {
telegramChatID := os.Getenv("TELEGRAM_ID")
telegramToken := os.Getenv("TELEGRAM_TOKEN")
lockerCodeHost := os.Getenv("LOCKERCODES_HOST")
lockerCodePath := os.Getenv("LOCKERCODES_PATH")
var version int
flag.IntVar(&version, "version", 24, "NBA 2K Version")
flag.Parse()
if version == 0 {
version = 24
}
jkt, err := time.LoadLocation("Asia/Jakarta")
if err != nil {
panic(err)
}
mtn, err := time.LoadLocation("US/Mountain")
if err != nil {
panic(err)
}
link := fmt.Sprintf("%s/%d/%s", lockerCodeHost, version, lockerCodePath)
resp, err := http.Get(link)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var lockerCodes LockerCodes
if err := json.Unmarshal(body, &lockerCodes); err != nil {
panic(err)
}
var hasData bool
var msg []string
msg = append(msg, fmt.Sprintf("NBA 2K%d", version))
for _, edge := range lockerCodes.Result.Data.AllLockerCodes.Edges {
code := edge.Node.LockerCode
title := edge.Node.Title
createdAt := edge.Node.DateAdded
createdTime, err := time.Parse(time.RFC3339, createdAt)
if err != nil {
panic(err)
}
now := time.Now().In(mtn)
if now.Sub(createdTime).Hours() > 24 {
continue
}
expire, err := time.Parse(time.RFC3339, edge.Node.Expiration)
if err != nil {
continue
}
if now.After(expire) {
continue
}
msg = append(msg, fmt.Sprintf("Title: %s\nCode: %s\nCreated At: %s\nExpire At: %s\n\n", title, code, createdTime.In(jkt).String(), expire.In(jkt).String()))
hasData = true
}
if !hasData {
msg = append(msg, "No code today :(")
}
v := url.Values{}
v.Set("chat_id", telegramChatID)
v.Set("text", strings.Join(msg, "\n"))
_, err = http.PostForm(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", telegramToken), v)
if err != nil {
panic(err)
}
}