-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
152 lines (131 loc) · 3.96 KB
/
router.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
package main
import (
"context"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/chromedp"
"github.com/julienschmidt/httprouter"
)
func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Not protected!\n")
}
func urlContent(ctx context.Context, cl Geter) func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var url string = `{"error" : "require url param"}`
var callback string = "callback"
if len(r.URL.RawQuery) > 0 {
queryValues := r.URL.Query()
url = queryValues.Get("url")
if url == "" {
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "%s", url)
return
}
callback = queryValues.Get("jsonp")
if callback == "" {
callback = "callback"
}
} else {
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "%s", url)
return
}
var res string
err := chromedp.Run(ctx,
emulation.SetUserAgentOverride("WebScraper 1.0"),
chromedp.Navigate(url),
chromedp.Sleep(2*time.Second),
chromedp.ActionFunc(func(ctx context.Context) error {
node, err := dom.GetDocument().Do(ctx)
if err != nil {
return err
}
res, err = dom.GetOuterHTML().WithNodeID(node.NodeID).Do(ctx)
return err
}),
)
if err != nil {
log.Fatal(err)
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "errGet %s", url)
return
}
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
w.WriteHeader(200)
var htmlString string
//htmlString = res
htmlString = strings.ReplaceAll(res, "\"", "'")
htmlString = strings.ReplaceAll(htmlString, "\r\n", "")
htmlString = strings.ReplaceAll(htmlString, "\t", "")
htmlString = strings.ReplaceAll(htmlString, "\n", "")
htmlString = removeScriptsLansana(htmlString)
fmt.Fprintf(w, `%s({"res":"%s","url":"%s"})`, callback, htmlString, url)
//fmt.Fprintf(w, htmlString)
}
}
func urlData(cl Geter) func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var url string = `{"error" : "require url param"}`
var callback string = "callback"
if len(r.URL.RawQuery) > 0 {
queryValues := r.URL.Query()
url = queryValues.Get("url")
if url == "" {
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "%s", url)
return
}
callback = queryValues.Get("jsonp")
if callback == "" {
callback = "callback"
}
} else {
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "%s", url)
return
}
res, errGet := cl.Get(url)
if errGet != nil {
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "errGet %s", url)
return
}
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
w.WriteHeader(200)
htmlString := strings.ReplaceAll(*res, "\"", "'")
htmlString = strings.ReplaceAll(htmlString, "\r\n", "")
htmlString = strings.ReplaceAll(htmlString, "\t", "")
htmlString = strings.ReplaceAll(htmlString, "\n", "")
/*
doc, err := html.Parse(strings.NewReader(htmlString))
if err != nil {
log.Fatal(err)
}
buf := bytes.NewBuffer([]byte{})
if err := html.Render(buf, doc); err != nil {
log.Fatal(err)
}
log.Println(buf.String())
*/
htmlString = removeScriptsLansana(htmlString)
fmt.Fprintf(w, `%s({"res":"%s","url":"%s"})`, callback, htmlString, url)
}
}
func router(ctx context.Context, cl Geter) *httprouter.Router {
router := httprouter.New()
router.GET("/", index)
router.GET("/data", urlData(cl))
router.GET("/content", urlContent(ctx, cl))
return router
}