forked from Osuka42g/SGo-Scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.go
115 lines (97 loc) · 2 KB
/
crawler.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
package main
import (
// "fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"strings"
"golang.org/x/net/html"
)
func crawlImages(rawContents io.Reader) []string {
z := html.NewTokenizer(rawContents)
imagesFound := []string{}
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
return imagesFound
case tt == html.StartTagToken:
t := z.Token()
isAnchor := t.Data == "a"
if !isAnchor {
continue
}
link := getValueFromAttribute(t, "href")
if link == "" {
continue
}
hasProto := strings.Index(link, "https://") == 0 && strings.HasSuffix(link, ".jpg") == true
if hasProto {
imagesFound = append(imagesFound, link)
}
}
}
}
func getAlbumInfo(rawContents io.Reader) (modelName string, albumName string) {
title := getTitle(rawContents)
s := strings.Split(title, " Photo Album: ")
ss := strings.Split(s[1], " | SuicideGirls")
modelName = s[0]
albumName = ss[0]
return
}
func getTitle(rawContents io.Reader) string {
z := html.NewTokenizer(rawContents)
defaultTitle := ""
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
return defaultTitle
case tt == html.StartTagToken:
t := z.Token()
isTitle := t.Data == "title"
if !isTitle {
continue
}
z.Next()
title := z.Token()
return title.Data
}
}
}
func getContents(link string) io.Reader {
sessionidCookie := os.Getenv("SESSIONIDTOKEN")
jar, _ := cookiejar.New(nil)
var cookies []*http.Cookie
cookie := &http.Cookie{
Name: "sessid",
Value: sessionidCookie,
Path: "/",
Domain: "www.suicidegirls.com",
}
cookies = append(cookies, cookie)
u, _ := url.Parse(link)
jar.SetCookies(u, cookies)
// fmt.Println(jar.Cookies(u))
client := &http.Client{
Jar: jar,
}
req, _ := http.NewRequest("GET", link, nil)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
return resp.Body
}
func getValueFromAttribute(t html.Token, attr string) string {
val := ""
for _, a := range t.Attr {
if a.Key == attr {
val = a.Val
}
}
return val
}