-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
188 lines (160 loc) · 3.7 KB
/
parse.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
181
182
183
184
185
186
187
188
package main
import (
"golang.org/x/net/html"
// "fmt"
"net/http"
"strings"
"strconv"
)
type Image struct {
Url string
}
type Parse struct {
StartUrl string
Links []string
Images []Image
}
func (this *Parse) init(StartUrl string) {
this.StartUrl = StartUrl
this.Links = []string{}
this.Images = []Image{}
}
/*
* Parse
*
* Loops a http response, and applies a given function on it.
*/
func (this *Parse) Parse(resp *http.Response, needle string, fn func(html.Token)) {
z := html.NewTokenizer(resp.Body)
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
return
case tt == html.StartTagToken:
t := z.Token()
if isCatch := t.Data == needle; isCatch {
fn(t)
}
}
}
}
///////////////////////////////// Parsers ////////////////////////////////////////////
/*
* parser_fourchan_links
*
* Site: 4chan.org
* Analises a html token in order to retrieve a link.
*/
func (this *Parse) parser_fourchan_links() (func(token html.Token)) {
return func(token html.Token) {
var urlfound string = ""
var linkfound bool = false
for _, attr := range token.Attr {
if strings.Contains(attr.Val, "replylink") {
linkfound = true
}
if strings.Contains(attr.Val, "thread") {
urlfound = attr.Val
}
}
if linkfound {
if link := strings.Split(urlfound, "/"); len(link) > 2 {
this.Links = append(this.Links, this.StartUrl + urlfound)
}
}
}
}
/*
* parser_images
*
* Site: General
* Collects an image given a token.
*/
func (this *Parse) parser_images() (func(token html.Token)) {
return func(token html.Token) {
for _, attr := range token.Attr {
if strings.Contains(attr.Key, "src") {
this.Images = append(this.Images, Image{attr.Val})
}
}
}
}
/*
* parser_links
*
* Site: General
* Collects a link given a token.
*/
func (this *Parse) parser_links() (func(token html.Token)) {
// TODO: Remove dubbles
// TODO: Add start url
return func(token html.Token) {
for _, attr := range token.Attr {
if strings.Contains(attr.Key, "href") {
link := attr.Val
if ! strings.Contains(link, "htt") {
link = this.StartUrl + link
}
if ! this.link_exists(link) {
this.Links = append(this.Links, link)
}
}
}
}
}
///////////////////////////////// Special ////////////////////////////////////////////
/*
* fourchan_img_clean
*
* Site: 4chan
* Cleans image urls.
*/
func (this *Parse) fourchan_img_clean() {
var images []Image
for _,img := range this.Images {
img.Url = strings.Replace(img.Url, "s", "", -1)
images = append(images, img)
}
this.Images = images
}
func (this *Parse) get_wish_products(resp *http.Response) {
}
/*
* avanza_get_sellprice
*
* Site: Avanza
* Gets the current sellprice from a given httpResponse
*/
func (this *Parse) avanza_get_sellprice(resp *http.Response) float64 {
z := html.NewTokenizer(resp.Body)
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
return 0.0
case tt == html.StartTagToken:
t := z.Token()
if isCatch := t.Data == "span"; isCatch {
for _, attr := range t.Attr {
if strings.Contains(attr.Val, "sellPrice") {
z.Next();
tt := z.Token();
strval := strings.Replace(tt.String(), ",", ".", -1);
value, _ := strconv.ParseFloat(strval, 64)
return value;
}
}
}
}
}
}
///////////////////////////////// Help Functions ////////////////////////////////////////////
func (this *Parse) link_exists(link string) bool {
for _, l := range this.Links {
if l == link {
return true
}
}
return false
}