forked from advancedlogic/GoOse
/
helpers.go
55 lines (50 loc) · 1.06 KB
/
helpers.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
package goose
import (
"crypto/md5"
"fmt"
"github.com/bjarneh/latinx"
"io"
"strings"
"unicode/utf8"
)
type Helper struct {
urlString string
url string
linkHash string
}
func NewRawHelper(url string, rawHtml string) Helper {
if !utf8.ValidString(rawHtml) {
converter := latinx.Get(latinx.ISO_8859_1)
rawHtmlBytes, err := converter.Decode([]byte(rawHtml))
if err != nil {
fmt.Println(err.Error())
}
rawHtml = string(rawHtmlBytes)
}
h := md5.New()
io.WriteString(h, url)
bytes := h.Sum(nil)
helper := Helper{
urlString: url,
url: url,
linkHash: fmt.Sprintf("%s.%d", string(bytes), TimeInNanoseconds()),
}
return helper
}
func NewUrlHelper(url string) Helper {
finalUrl := ""
if strings.Contains(url, "#!") {
finalUrl = strings.Replace(url, "#!", "?_escaped_fragment_=", -1)
} else {
finalUrl = url
}
h := md5.New()
io.WriteString(h, finalUrl)
bytes := h.Sum(nil)
helper := Helper{
urlString: finalUrl,
url: finalUrl,
linkHash: fmt.Sprintf("%s.%d", string(bytes), TimeInNanoseconds()),
}
return helper
}