-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathrecvsms.go
113 lines (88 loc) · 2.45 KB
/
recvsms.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
package main
import (
"log"
"net/http"
"strings"
"time"
"github.com/anaskhan96/soup"
)
const (
pageURL = "https://receive-smss.com/"
cookieName = "__cfduid"
smsEndpoint = "sms/"
)
//ScrapeAvailableNumbers Extracts the list of phone-numbers from the page
func ScrapeAvailableNumbers() []Number {
response, err := soup.Get(pageURL)
if err != nil {
log.Fatalf("Failed to make HTTP request to %s\n", pageURL)
}
numbers := make([]Number, 0)
//scrape the page
document := soup.HTMLParse(response)
numbersContainer := document.Find("div", "class", "number-boxes")
numberBoxes := numbersContainer.FindAll("div", "class", "number-boxes-item")
for _, numberBox := range numberBoxes {
numberElement := numberBox.FindStrict("div", "class", "row")
if numberElement.Error == nil {
numberContainer := numberElement.FindStrict("h4")
countryContainer := numberElement.FindStrict("h5")
if numberContainer.Error == nil && countryContainer.Error == nil {
number := Number{
CreatedAt: time.Now().Format("2006-01-02 15:04:05 Monday"),
Number: numberContainer.Text(),
Country: countryContainer.Text(),
}
numbers = append(numbers, number)
}
}
}
return numbers
}
//ScrapeMessagesForNumber GET SMS from number
func ScrapeMessagesForNumber(number string) []Message {
//Get cookie first
resp, err := http.Get(pageURL)
if err != nil {
log.Fatalln("Failed to make GET request")
}
cookies := resp.Cookies()
cookieValue := ""
for _, cookie := range cookies {
if cookie.Name == cookieName {
cookieValue = cookie.Value
}
}
//now use that value to set the cookie in soup
soup.Cookie(cookieName, cookieValue)
requestURL := pageURL + smsEndpoint + strings.ReplaceAll(number, "+", "") + "/"
//make GET with soup:
response, err := soup.Get(requestURL)
if err != nil {
log.Fatalf("error fetching data: %s", err.Error())
}
document := soup.HTMLParse(response)
table := document.Find("table")
if table.Error != nil {
log.Fatalln("Failed to load messages")
}
tbody := table.Find("tbody")
if tbody.Error != nil {
log.Fatalln("Failed to load messages")
}
tableRows := tbody.FindAll("tr")
messages := make([]Message, 0)
for _, row := range tableRows {
cols := row.FindAll("td")
if len(cols) < 3 {
continue
}
message := Message{
Originator: cols[0].FullText(),
Body: cols[1].FullText(),
CreatedAt: cols[2].FullText(),
}
messages = append(messages, message)
}
return messages
}