-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponses.go
78 lines (64 loc) · 1.9 KB
/
responses.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 gokhttp_advanced_responses
import (
"fmt"
gokhttp_responses "github.com/BRUHItsABunny/gOkHttp/responses"
"github.com/Jeffail/gabs"
"github.com/PuerkitoBio/goquery"
"github.com/t14raptor/go-fast/ast"
"github.com/t14raptor/go-fast/parser"
"net/http"
"strings"
)
func ResponseHTML(resp *http.Response) (*goquery.Document, error) {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("goquery.NewDocumentFromReader: %w", err)
}
err = resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("resp.Body.Close: %w", err)
}
return doc, err
}
func ResponseDynamicJSON(resp *http.Response) (*gabs.Container, error) {
respBytes, err := gokhttp_responses.ResponseBytes(resp)
if err != nil {
return nil, fmt.Errorf("gokhttp_responses.ResponseBytes: %w", err)
}
container, err := gabs.ParseJSON(respBytes)
if err != nil {
return nil, fmt.Errorf("gabs.ParseJSON: %w", err)
}
return container, err
}
func ResponseAST(resp *http.Response) (*ast.Program, error) {
respText, err := gokhttp_responses.ResponseText(resp)
if err != nil {
return nil, fmt.Errorf("gokhttp_responses.ResponseText: %w", err)
}
astParser, err := parser.ParseFile(respText)
if err != nil {
return nil, fmt.Errorf("parser.ParseFile: %w", err)
}
return astParser, nil
}
func ResponseASTFromHTML(resp *http.Response) (*ast.Program, error) {
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return nil, fmt.Errorf("goquery.NewDocumentFromReader: %w", err)
}
err = resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("resp.Body.Close: %w", err)
}
jsSB := strings.Builder{}
doc.Find("script").Each(func(index int, element *goquery.Selection) {
jsSB.WriteString(element.Text())
jsSB.WriteString("\n")
})
astParser, err := parser.ParseFile(jsSB.String())
if err != nil {
return nil, fmt.Errorf("parser.ParseFile: %w", err)
}
return astParser, nil
}