forked from sundowndev/phoneinfoga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
numverify.go
78 lines (65 loc) · 2.16 KB
/
numverify.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
package scanners
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"github.com/PuerkitoBio/goquery"
)
// NumverifyScannerResponse REST API response
type NumverifyScannerResponse struct {
Valid bool `json:"valid"`
Number string `json:"number"`
LocalFormat string `json:"local_format"`
InternationalFormat string `json:"international_format"`
CountryPrefix string `json:"country_prefix"`
CountryCode string `json:"country_code"`
CountryName string `json:"country_name"`
Location string `json:"location"`
Carrier string `json:"carrier"`
LineType string `json:"line_type"`
}
// NumverifyScan fetches Numverify's API
func NumverifyScan(number *Number) (res *NumverifyScannerResponse, err error) {
html, err := http.Get("http://numverify.com/")
if err != nil {
return nil, err
}
defer html.Body.Close()
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(html.Body)
if err != nil {
return nil, err
}
secret, _ := doc.Find("[name=\"scl_request_secret\"]").Attr("value")
// Then fetch REST API
safeNumber := number.International
apiKey := md5.Sum([]byte(safeNumber + secret))
url := fmt.Sprintf("https://numverify.com/php_helper_scripts/phone_api.php?secret_key=%s&number=%s", hex.EncodeToString(apiKey[:]), safeNumber)
// Build the request
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
// Fill the response with the data from the JSON
var result NumverifyScannerResponse
// Use json.Decode for reading streams of JSON data
if err := json.NewDecoder(response.Body).Decode(&result); err != nil {
return nil, err
}
res = &NumverifyScannerResponse{
Valid: result.Valid,
Number: result.Number,
LocalFormat: result.LocalFormat,
InternationalFormat: result.InternationalFormat,
CountryPrefix: result.CountryPrefix,
CountryCode: result.CountryCode,
CountryName: result.CountryName,
Location: result.Location,
Carrier: result.Carrier,
LineType: result.LineType,
}
return res, nil
}