-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
198 lines (166 loc) · 5.15 KB
/
service.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
)
// fetchQuestData makes an HTTP GET request to the specified URL with headers and returns the response body.
func fetchQuestData(url string) ([]byte, error) {
// GET apiNonce, apiSign, apiTs
apiNonce, apiSign, apiTs := getDebankQuestAPIHeaders()
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("accept", "*/*")
req.Header.Set("accept-language", "en,th-TH;q=0.9,th;q=0.8")
req.Header.Set("cache-control", "no-cache")
req.Header.Set("origin", "https://debank.com")
req.Header.Set("pragma", "no-cache")
req.Header.Set("priority", "u=1, i")
req.Header.Set("referer", "https://debank.com/")
req.Header.Set("sec-ch-ua", `"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
req.Header.Set("sec-fetch-dest", "empty")
req.Header.Set("sec-fetch-mode", "cors")
req.Header.Set("sec-fetch-site", "same-site")
req.Header.Set("source", "web")
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36")
req.Header.Set("x-api-nonce", apiNonce)
req.Header.Set("x-api-sign", apiSign)
req.Header.Set("x-api-ts", apiTs)
req.Header.Set("x-api-ver", "v2")
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
}
return body, nil
}
func getDebankQuestAPIHeaders() (string, string, string) {
// Create a new context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// Set a timeout to prevent the context from running indefinitely
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
// Slice to store the headers
var apiNonce, apiSign, apiTs string
// Set up a listener for network requests
chromedp.ListenTarget(ctx, func(ev interface{}) {
if ev, ok := ev.(*network.EventRequestWillBeSent); ok {
// Check if the request is for the desired API endpoint
if ev.Request.URL == "https://api.debank.com/quest/list?limit=50&status=hot" {
// Extract the desired headers
for headerName, headerVal := range ev.Request.Headers {
// fmt.Println("headerName: ", headerName)
// fmt.Println("headerVal: ", headerVal)
val, ok := headerVal.(string)
if !ok {
continue
}
switch headerName {
case "x-api-nonce":
apiNonce = val
case "x-api-sign":
apiSign = val
case "x-api-ts":
apiTs = val
}
}
}
}
})
// Run the Chrome process
err := chromedp.Run(ctx,
// Enable network events
network.Enable(),
// Navigate to the page
chromedp.Navigate("https://debank.com/quest"),
// Wait for the API request to be sent
chromedp.WaitVisible(`div[class^="QuestCard_title__"]`),
)
if err != nil {
log.Fatal(err)
}
// Print the captured headers
// fmt.Println("x-api-nonce:", apiNonce)
// fmt.Println("x-api-sign:", apiSign)
// fmt.Println("x-api-ts:", apiTs)
return apiNonce, apiSign, apiTs
}
func initSeenQuest(url, botToken, channelID string) (map[int]struct{}, error) {
fmt.Println("init.....")
seenQuestIDs := make(map[int]struct{})
body, err := fetchQuestData(url)
if err != nil {
return nil, err
}
var questResponse QuestResponse
err = json.Unmarshal(body, &questResponse)
if err != nil {
return nil, err
}
for _, quest := range questResponse.Data.Quests {
questID := quest.Article.ID
_, seen := seenQuestIDs[questID]
if !seen {
// New quest found, print it
fmt.Printf("New Quest: %+v\n", quest.Article.Quest.Name)
fmt.Printf("Link: %s\n", createQuestURL(questID))
seenQuestIDs[questID] = struct{}{}
}
}
return seenQuestIDs, nil
}
func createQuestURL(id int) string {
r := "91299"
return fmt.Sprintf("https://debank.com/stream/%d?r=%s", id, r)
}
// SendMessage sends a message to a specified Telegram chat ID
func SendMessage(botToken, text, textButton, url, chatID string) error {
// Create the API URL for sending a message
apiURL := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken)
button := InlineKeyboardButton{
Text: textButton,
URL: url,
}
inlineKeyboard := InlineKeyboardMarkup{
InlineKeyboard: [][]InlineKeyboardButton{
{button},
},
}
payload := map[string]interface{}{
"chat_id": chatID,
"text": text,
"reply_markup": inlineKeyboard,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("failed to marshal payload: %w", err)
}
// Send the request
resp, err := http.Post(apiURL, "application/json", bytes.NewBuffer(payloadBytes))
if err != nil {
return fmt.Errorf("failed to send message: %w", err)
}
defer resp.Body.Close()
// Check the response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received non-OK response status: %s", resp.Status)
}
return nil
}