-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsing.go
107 lines (84 loc) · 2.5 KB
/
parsing.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
package main
import (
"fmt"
"io"
"regexp"
"strconv"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
)
// Parsing the stuff
type OvercastParams struct {
SpaceAvailible int64
MaxFileCount int
MaxFileSize int64
PostData map[string]string
UploadURL string
DataKeyPrefix string
}
// extracts limitations from the /uploads page
func parseInfo(input *goquery.Selection) (avalible int64, maxFiles int, maxFile int64) {
var err error
avalibleStr, found := input.Attr("data-free-bytes")
if found {
avalible, err = strconv.ParseInt(avalibleStr, 10, 64)
}
if err != nil || !found {
avalible = -1
fmt.Println("[WARN] Failed to get space limit, upload might fail")
}
maxFileStr, found := input.Attr("data-max-bytes")
if found {
maxFile, err = strconv.ParseInt(maxFileStr, 10, 64)
}
if err != nil || !found {
maxFile = -1
fmt.Println("[WARN] Failed to get file size limit, upload might fail")
}
maxFiles = -1
info := input.NextFiltered("div.caption2").Text()
reMaxFiles := regexp.MustCompile(`up\s+to\s+(\d+)`)
maxFilesStrs := reMaxFiles.FindStringSubmatch(info)
if len(maxFilesStrs) == 2 {
maxFiles, err = strconv.Atoi(maxFilesStrs[1])
if err != nil {
maxFiles = -1
}
}
if maxFile == -1 {
fmt.Println("[WARN] Failed to get total files limit, upload might fail")
}
return
}
func parseUploadsPage(body io.ReadCloser) (params *OvercastParams, err error) {
var overcastParams OvercastParams
uploadsPage, err := goquery.NewDocumentFromReader(body)
if err != nil {
err = errors.Wrap(err, "Error while parsing /uploads page")
return
}
form := uploadsPage.Find("form#upload_form")
prefix, found := form.Attr("data-key-prefix")
if !found {
err = errors.New("Failed to parse upload form: no data-key-prefix found")
return
}
overcastParams.DataKeyPrefix = prefix
overcastParams.PostData = make(map[string]string)
form.Find(`input[type="hidden"]`).Each(func(i int, s *goquery.Selection) {
name, nameFound := s.Attr("name")
val, valueFound := s.Attr("value")
if nameFound && valueFound {
overcastParams.PostData[name] = val
}
})
uploadURL, uploadURLFound := form.Attr("action")
if form.Length() != 1 || len(overcastParams.PostData) == 0 || !uploadURLFound {
err = errors.New("Failed to find the upload form")
return
}
overcastParams.UploadURL = uploadURL
input := uploadsPage.Find("input#upload_file")
overcastParams.SpaceAvailible, overcastParams.MaxFileCount, overcastParams.MaxFileSize = parseInfo(input)
return &overcastParams, nil
}