-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (146 loc) · 3.45 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"crypto/tls"
"encoding/json"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/gocolly/colly/v2"
"github.com/joho/godotenv"
)
type Scraper struct {
httpClient *http.Client
waitGroup sync.WaitGroup
done chan struct{}
}
func (s *Scraper) sendText(number string) {
defer s.waitGroup.Done()
key := os.Getenv("SMS_KEY")
message := "BUY DI TIKITZ!!!"
reqJSON, err := json.Marshal(map[string]string{
"phone": number,
"message": message,
"key": key,
})
if err != nil {
log.Println("Error encoding request body:", err)
return
}
req, err := http.NewRequest(
"POST",
"https://textbelt.com/text",
strings.NewReader(string(reqJSON)),
)
if err != nil {
log.Println("Error creating request:", err)
return
}
req.Header.Add("Content-Type", "application/json")
resp, err := s.httpClient.Do(req)
if err != nil {
log.Println("Error sending SMS:", err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
log.Printf("SMS sent: %s\n", number)
} else {
log.Printf("Failed to send SMS to %s\n", number)
}
}
func (s *Scraper) fetch() {
defer s.waitGroup.Done()
c := colly.NewCollector(
// Spoof user agent to avoid bot detection
colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/91.0.4472.124 Safari/537.36"),
)
c.WithTransport(&http.Transport{
DialContext: (&net.Dialer{}).DialContext,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
})
c.OnRequest(func(r *colly.Request) {
r.Headers.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9")
r.Headers.Set("Accept-Language", "en-US,en;q=0.9")
r.Headers.Set("Referer", "https://www.google.com/")
log.Println("Visiting", r.URL.String())
})
foundBuynow := false
c.OnHTML("a[id='buynow']", func(e *colly.HTMLElement) {
foundBuynow = true
s.success()
})
c.OnScraped(func(r *colly.Response) {
if !foundBuynow {
s.failure()
}
})
c.OnError(func(r *colly.Response, err error) {
log.Printf("Error fetching %s: %v\n", r.Request.URL, err)
})
c.Visit(os.Getenv("URL"))
}
func (s *Scraper) success() {
log.Println("BUY DI TIKITZ!!!")
phoneNumbers := strings.Split(os.Getenv("PHONE_NUMBERS"), ",")
for _, number := range phoneNumbers {
s.waitGroup.Add(1)
go s.sendText(number)
}
close(s.done)
}
func (s *Scraper) failure() {
log.Println("no tikz found...")
}
func (s *Scraper) scrapeLoop() {
intervalMS, err := strconv.Atoi(os.Getenv("INTERVAL_MS"))
if err != nil {
log.Fatalf("Error parsing INTERVAL_MS: %v", err)
}
ticker := time.NewTicker(time.Duration(intervalMS) * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-s.done:
return
case <-ticker.C:
s.waitGroup.Add(1)
go s.fetch()
}
}
}
func init() {
if err := godotenv.Load(); err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
}
func main() {
log.Println("Polling for da tikz...")
scraper := &Scraper{
httpClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
},
done: make(chan struct{}),
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go scraper.scrapeLoop()
select {
case <-signals:
log.Println("Received an interrupt, stopping service...")
case <-scraper.done:
log.Println("Success! Terminating gracefully...")
}
scraper.waitGroup.Wait()
log.Println("Shutting down gracefully")
}