-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
182 lines (159 loc) · 3.95 KB
/
util.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
package main
import (
"encoding/xml"
"fmt"
"net/http"
"net/http/httputil"
//"time"
"net/url"
"path/filepath"
"strings"
)
func Args(s string) (result []string) {
defer func() {
if recover() != nil {
result = make([]string, 0, 0)
}
}()
inStr := false
escape := false
return strings.FieldsFunc(s, func(r rune) bool {
if escape {
escape = false
return false
}
switch r {
case '\\':
escape = true
return false
case ' ', '\n', '\t':
return !inStr
case '"':
inStr = !inStr
return true
default:
return false
}
})
}
func NewSingleHostReverseProxy(target *url.URL) *httputil.ReverseProxy {
director := func(req *http.Request) {
targetQuery := target.RawQuery
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.Host = target.Host
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
}
return &httputil.ReverseProxy{Director: director}
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
type Ftypes struct {
WebAudio bool
Audio bool
Video bool
WebVideo bool
Flash bool
Image bool
DirectlyViewable bool
}
func Filetypes(filename string) Ftypes {
ext := filepath.Ext(filename)
ret := Ftypes{}
e := strings.ToLower(ext)
ret.Audio = StringIsOneOf(e, ".wma", ".mp3", ".aac", ".flac", ".ogg", ".opus")
ret.WebAudio = StringIsOneOf(e, ".mp3", ".ogg", ".wav", ".opus")
ret.Video = StringIsOneOf(e, ".mkv", ".mp4", ".wmv", ".webm", ".avi")
ret.WebVideo = StringIsOneOf(e, ".mp4", ".webm", ".ogv")
ret.Flash = StringIsOneOf(e, ".swf")
ret.Image = StringIsOneOf(e, ".jpg", ".gif", ".png", ".jpeg", ".tiff", ".tif", ".webp")
ret.DirectlyViewable = ret.Image || ret.Flash || ret.WebVideo || ret.WebAudio
return ret
}
func StringIsOneOf(str string, things ...string) bool {
for _, s := range things {
if str == s {
return true
}
}
return false
}
type Post struct {
File_url, Id, Url, Tags string
}
func GelbooruGet(tags string) (Posts []Post, err error) {
resp, err := http.Get("http://gelbooru.com/index.php?page=dapi&s=post&q=index&tags=" + strings.Replace(tags, " ", "+", -1))
if err != nil {
return nil, err
}
defer resp.Body.Close()
parser := xml.NewDecoder(resp.Body)
//var token xml.Token
entries := make([]Post, 0, 100)
depth := 0
for {
token, err := parser.Token()
if err != nil {
if err.Error() == "EOF" {
break
} else {
return nil, err
}
}
switch t := token.(type) {
case xml.StartElement:
elmt := xml.StartElement(t)
//name := elmt.Name.Local
if elmt.Name.Local == "post" {
Post := new(Post)
for i := 0; i < len(elmt.Attr); i++ {
switch elmt.Attr[i].Name.Local {
case "file_url":
Post.File_url = elmt.Attr[i].Value
break
case "id":
Post.Id = elmt.Attr[i].Value
Post.Url = "http://gelbooru.com/index.php?page=post&s=view&id=" + Post.Id
break
case "tags":
Post.Tags = elmt.Attr[i].Value
}
}
entries = append(entries, *Post)
}
//printElmt(name, depth)
depth++
case xml.EndElement:
depth--
//elmt := xml.EndElement(t)
//name := elmt.Name.Local
//printElmt(name, depth)
case xml.CharData:
//bytes := xml.CharData(t)
//printElmt("\"" + string([]byte(bytes)) + "\"", depth)
case xml.Comment:
//printElmt("Comment", depth)
case xml.ProcInst:
//printElmt("ProcInst", depth)
case xml.Directive:
//printElmt("Directive", depth)
default:
fmt.Println("Unknown")
}
}
return entries, nil
}